Apostrophe in realname token is escaped upon selection of any user reference

Created on 22 March 2023, over 1 year ago
Updated 23 March 2023, over 1 year ago

Problem/Motivation

Names with apostrophes are escaped during the token conversion.

Steps to reproduce

* add custom last name field
* go to /admin/config/people/realname and add token to field e.g. [user:field_last]
* create a new user with last name with apostrophe like "O'brien"
* go to any node creation form and change author to new user.

Note: that the look up values are not escaped but when a selection is made and the text field is populated it is.

Proposed resolution

Could be related to another contrib issue https://www.drupal.org/project/search_api_saved_searches/issues/3197431 β†’
https://www.drupal.org/files/issues/2021-05-15/3197431-4--email_tokens_h... β†’

Remaining tasks

User interface changes

API changes

Data model changes

πŸ› Bug report
Status

Needs review

Version

2.0

Component

Code

Created by

πŸ‡ΊπŸ‡ΈUnited States robpowell Boston

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @robpowell
  • πŸ‡ΊπŸ‡ΈUnited States robpowell Boston
  • πŸ‡ΊπŸ‡ΈUnited States robpowell Boston

    Looks like this issue is related to realname/src/Controller/RealnameAutocompleteController.php

     /** @var \Drupal\user\Entity\User $account */
        foreach ($accounts as $account) {
          $matches[] = [
            'value' => $this->t('@realname (@id)',
                    [
                      '@realname' => $account->getDisplayName(),
                      '@id' => $account->id(),
                    ]),
            'label' => $this->t('@realname (@username)',
                     [
                       '@realname' => $account->getDisplayName(),
                       '@username' => $account->getAccountName(),
                     ]),
          ];
        }
    

    t() will escape special characters. It looks like we could leverage some of the code from the controller this overrides core/lib/Drupal/Core/Entity/EntityAutocompleteMatcher.php

    // Loop through the entities and convert them into autocomplete output.
          foreach ($entity_labels as $values) {
            foreach ($values as $entity_id => $label) {
              $key = "$label ($entity_id)";
              // Strip things like starting/trailing white spaces, line breaks and
              // tags.
              $key = preg_replace('/\s\s+/', ' ', str_replace("\n", '', trim(Html::decodeEntities(strip_tags($key)))));
              // Names containing commas or quotes must be wrapped in quotes.
              $key = Tags::encode($key);
              $matches[] = ['value' => $key, 'label' => $label];
            }
          }
    
  • Status changed to Needs review over 1 year ago
  • πŸ‡ΊπŸ‡ΈUnited States robpowell Boston
  • @robpowell opened merge request.
Production build 0.71.5 2024