token_entity_render_token_info_alter() is using a not existing service

Created on 24 October 2022, over 2 years ago
Updated 7 June 2024, 11 months ago

token_entity_render_token_info_alter() is using the entity.manager, which doesn't exist in Drupal 9. Instead, it should use the entity_type.manager service, which is already used by that hook implementation.

  // Load all possible view modes.
  $all_view_modes = \Drupal::service('entity.manager')->getStorage('entity_view_mode')->loadMultiple();

  // Traverse all view modes.
  foreach ($all_view_modes as $mode) {

    // Leave if no entity type is set.
    if (!isset($data['tokens'][$mode->getTargetType()])) {
      continue;
    }

    // Load the entity type definition.
    $entity_type_definition = \Drupal::service('entity_type.manager')->getDefinition($mode->getTargetType());

    // Remove the prefix from the view mode name.
    $mode_name = preg_replace('/(.*?\.)/', '', $mode->id());

    // Add token information.
    $data['tokens'][$mode->getTargetType()]['render:' . $mode_name] = [
      'name' => $mode->label(),
      'description' => t('Muestra la entidad @entity_type usando el modo de visualización @view_mode.', [
        '@entity_type' => $entity_type_definition->getLabel(),
        '@view_mode' => $mode->label(),
      ]),
    ];
  }
🐛 Bug report
Status

Closed: duplicate

Version

2.0

Component

Code

Created by

Live updates comments and jobs are added and updated live.
  • Novice

    It would make a good project for someone who is new to the Drupal contribution process. It's preferred over Newbie.

Sign in to follow issues

Comments & Activities

Not all content is available!

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

Production build 0.71.5 2024