The code in the AllTokens class wrongly assumes that how to replace tokens changes when the Token module is installed

Created on 3 November 2023, about 1 year ago

The Drupal\display_fields\AllTokens class contains the following code.

  private function coreReplace($string, $data, $settings = []) {
    // @TODO: Remove this temp code.
    // This is just here as a way to see all available tokens in debugger.
    $tokens = $this->coreToken->getInfo();

    $options = ['clear' => TRUE];

    // Replace tokens with core Token service.
    $replaced = $this->coreToken->replace($string, $data, $options);

    // Ensure that there are no double-slash sequences due to empty token values.
    $replaced = preg_replace('/(?<!:)\/+\//', '/', $replaced);

    return $replaced;
  }

  /**
   * Replace tokens with their values using the contributed token module.
   *
   * @param $string
   * @param $data
   * @param array $settings
   *
   * @return mixed|string
   */
  private function contribReplace($string, $data, $settings = []) {
    // @TODO: Add contrib Token integration when it is ready.
    // For now, just redirect to the core replacement to avoid breaking sites
    // where Token is installed.
    return $this->coreReplace($string, $data, $settings);
  }

That code is wrong: Whenever the Token module is installed, or not, the tokens in a string are replaced by calling Drupal\Core\Utility\Token::replace().

What the contributed module does is defining more token, altering tokens defined by core modules, and providing a theme hook to show a list of all the available tokens. It does not change how tokens are replaced, since that part of the Token module has been moved to Drupal core years ago.

🐛 Bug report
Status

Fixed

Version

9.1

Component

Code

Created by

🇮🇹Italy apaderno Brescia, 🇮🇹

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

Comments & Activities

Production build 0.71.5 2024