TermSelection returning HTML characters in select list

Created on 10 November 2016, about 8 years ago
Updated 5 February 2024, 11 months ago

Problem/Motivation

$selection_handler_options = [
  "target_type" => "taxonomy_term",
  "handler" => "default:taxonomy_term",
  "handler_settings" => [
    "target_bundles" => [
      "departments" => "departments"
    ],
  ],
];

    $selection_manager = \Drupal::service('plugin.manager.entity_reference_selection');
    $handler = $selection_manager->getInstance($selection_handler_options);
    $referenceable_entities = $handler->getReferenceableEntities();

dump($referenceable_entities); die();

Will output HTML characters from Taxonomy Term names:

array:1 [▼
  "departments" => array:21 [▼
    78 => "Academy"
    79 => "Billing & Insurance"
    80 => "Charity Care"
    81 => "Circle of Caring Program"
    82 => "Donnelley Ethics Program"
    83 => "Early Intervention Program"
    84 => "Fitness Center & Sports Program"
    85 => "Giving & Advancement"
    86 => "Global Patient Services"
    87 => "Human Resources"
    89 => "Marketing & PR"
    90 => "Medical Records"
    91 => "Military Patient Services"
    92 => "Neurourology"
    93 => "Referrer Center"
    94 => "Rehabilitation Measure"
    95 => "Second Look"
    96 => "Vocational Rehabilitation"
    97 => "Worker's Compensation Injuries"
  ]
]

Seems to be HTML::escape from line 65 of /core/modules/taxonomy/src/Plugin/EntityReferenceSelection/TermSelection.php

Not sure why we're escaping term names. The above code comes from yamlform module, at /src/Element/YamlFormEntityTrait.php, seems like a core problem, or perhaps yaml form should not be using plugin.manager.entity_reference_selection service.

Steps to reproduce

1) add some taxonomy terms in the tag vocabulary as follows:

a) Billing&Insurance
b) edit < me
c) hello

2) In my Case I have call this code for a select list of taxonomy terms based on vocabulary, so I added the following code to retrieve the list of all terms in the tags vocabulary:

    $selection_handler_options = [
      "target_type" => "taxonomy_term",
      "handler" => "default:taxonomy_term",
      "handler_settings" => [
        "target_bundles" => [
          "departments" => "tags"
        ],
      ],
    ];
    
    $selection_manager = \Drupal::service('plugin.manager.entity_reference_selection');
    $handler = $selection_manager->getInstance($selection_handler_options);
    $referenceable_entities = $handler->getReferenceableEntities();

3) The output is displaying HTML tags for special characters in option list.

 array:1 [▼
  "tags" => array:3 [▼
    3 => "Billing&amp;Insurance"
    2 => "edit &lt; me"
    1 => "hello"
  ]
]

Proposed resolution

TBD

Remaining tasks

Agree on solution

User interface changes

TBD

API changes

TBD

Data model changes

TBD

Release notes snippet

TBD

🐛 Bug report
Status

Needs work

Version

11.0 🔥

Component
Taxonomy 

Last updated 30 minutes ago

  • Maintained by
  • 🇺🇸United States @xjm
  • 🇬🇧United Kingdom @catch
Created by

🇺🇸United States oknate Greater New York City Area

Live updates comments and jobs are added and updated live.
  • Needs tests

    The change is currently missing an automated test that fails when run with the original code, and succeeds when the bug has been fixed.

  • Needs issue summary update

    Issue summaries save everyone time if they are kept up-to-date. See Update issue summary task instructions.

Sign in to follow issues

Merge Requests

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • First commit to issue fork.
  • Status changed to Needs review 11 months ago
  • 🇮🇳India shalini_jha

    Hello everyone,
    I was able to replicate this issue in version 11.x. Here are the steps to reproduce:

    Step 1) Create a taxonomy term in the tag vocabulary as follows:

    a) Billing&Insurance
    b) edit < me
    c) hello

    Step 2) In my Case I have call this code for a select list of taxonomy term based on vocabulary, so I added the following code to retrieve the list of all terms in the tags vocabulary:

        $selection_handler_options = [
          "target_type" => "taxonomy_term",
          "handler" => "default:taxonomy_term",
          "handler_settings" => [
            "target_bundles" => [
              "departments" => "tags"
            ],
          ],
        ];
        
        $selection_manager = \Drupal::service('plugin.manager.entity_reference_selection');
        $handler = $selection_manager->getInstance($selection_handler_options);
        $referenceable_entities = $handler->getReferenceableEntities();
    

    Step 3) The output is displaying HTML tags for special characters in option list.

     array:1 [▼
      "tags" => array:3 [▼
        3 => "Billing&amp;Insurance"
        2 => "edit &lt; me"
        1 => "hello"
      ]
    ]

    Upon debugging the code, I found that Html::escape is added in the getReferenceableEntities method for rendering the options. Once I removed this Html::escape, the output displayed without HTML tags.

     array:1 [▼
      "tags" => array:3 [▼
        3 => "Billing&Insurance"
        2 => "edit < me"
        1 => "hello"
      ]
    ]

    I couldn't identify the purpose of the changes in the test file. I've submitted a merge request for this. Please review

  • Status changed to Needs work 11 months ago
  • 🇺🇸United States smustgrave

    Not sure about the solution.

    Regardless need a test case showing the issue.

  • 🇺🇸United States smustgrave

    Will also need issue summary update

Production build 0.71.5 2024