Generate tokens values method does not take into account the entity language

Created on 19 February 2024, 4 months ago
Updated 27 February 2024, 4 months ago

Problem/Motivation

The metatags of content translated are getting the same value that the original languages.

Steps to reproduce

Add tags using tokens to translatable content, for example [node:title], and review the metatag value.
Example to check:

$node = \Drupal::entityTypeManager()->getStorage('node')->load(6);
$translation = $node->getTranslation('es');
$metatag_manager = \Drupal::service('metatag.manager');
$tags = $metatag_manager->tagsFromEntityWithDefaults($translation;
$metatags = $metatag_manager->generateTokenValues($tags, $translation);
print($metatags['title']);

Proposed resolution

The problem comes from the method \Drupal\metatag\MetatagManager::generateTokenValues
The langcode is defined, but never used.

    // Use the entity's language code, if one is defined.
    $langcode = NULL;
    if ($entity) {
      $langcode = $entity->language()->getId();
    }

The langcode should be a parameter of the method processTagValue:

$processed_value = $this->processTagValue($tag, $value, $token_replacements, TRUE, $langcode);
πŸ› Bug report
Status

Needs work

Version

2.0

Component

Code

Created by

πŸ‡ͺπŸ‡ΈSpain Eduardo Morales Alberti Spain, πŸ‡ͺπŸ‡Ί

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.

Sign in to follow issues

Merge Requests

Comments & Activities

Production build 0.69.0 2024