Problem/Motivation

When using a Reference View to display autocomplete results, the HTML tags are escaped and therefore display as string text instead of HTML. This prevents the ability to create custom formatted autocomplete results.

Steps to reproduce

Create a reference view
Create an entity reference field and select the reference view as reference type.
On form settings select Select2 with Autocomplete checkbox
The autocomplete result will contain Escaped HTML tags

Proposed resolution

Remove the escaped function to allow the rendering of HTML Tags.

Issue fork select2-3217934

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

ChuckSimply created an issue. See original summary.

Tuuuukka made their first commit to this issue’s fork.

giorgosk’s picture

Workaround: If one updates all view fields and changes in the style section

Customize field and label wrapper HTML
Customize label HTML
Customize field HTML

and sets the Element to none, no HTML is passed to select2 and there is no need for this patch.

chr.fritsch’s picture

Status: Active » Fixed

I think the workaround from @GiorgosK is fine. If you think select2 should do some additional work, feel free to reopen the issue.

giorgosk’s picture

Status: Fixed » Closed (works as designed)

So technically this should be works as designed

kostiantyn’s picture

@tuuuukka When I apply the patch, I encounter the error "TypeError: undefined is not an object (evaluating 'drupalSettings.select2.htmlAsText')". The error disappears when I set the configuration for the field, but it's not very convenient to set these settings for every field, even for those where these values are not needed.

duwid’s picture

StatusFileSize
new5.06 KB

I'm working on a case where I need to keep and render the html from select2 results, instead of removing it as described in @GiorgosK workaround. Therefore I will open this issue again, because I think the implemented solution from @Tuuuukka is a great improvement. I just found a little issue in select2.js:
if (drupalSettings.select2.htmlAsText === false) {
htmlAsText is int 0, so it should be:
if (drupalSettings.select2.htmlAsText === 0) {

duwid’s picture

Status: Closed (works as designed) » Needs review
kostiantyn’s picture

@Duwid, thank you for your improvement, I am testing it right now.

kostiantyn’s picture

Status: Needs review » Reviewed & tested by the community
duwid’s picture

StatusFileSize
new5.11 KB

This patch robustifys implementation if htmlAsText is not available because select2 widget is programmatically implemented via form api. For example this is the case social group invite module.