You are here

function metatag_token_info_alter in Metatag 8

Implements hook_token_info_alter().

File

./metatag.tokens.inc, line 93
Metatag token integration.

Code

function metatag_token_info_alter(&$info) {
  foreach (\Drupal::entityTypeManager()
    ->getDefinitions() as $entity_type_id => $entity_type) {
    if (!$entity_type
      ->entityClassImplements(ContentEntityInterface::class)) {
      continue;
    }

    // Make sure a token type exists for this entity.
    $token_type = \Drupal::service('token.entity_mapper')
      ->getTokenTypeForEntityType($entity_type_id);
    if (empty($token_type) || !isset($info['types'][$token_type])) {
      continue;
    }
    $fields = \Drupal::entityTypeManager()
      ->getStorage('field_storage_config')
      ->loadByProperties([
      'entity_type' => $entity_type_id,
      'type' => 'metatag',
    ]);
    foreach ($fields as $field) {
      $field_token_name = $token_type . '-' . $field
        ->getName();
      $info['types'][$field_token_name] = [
        'name' => Html::escape($field
          ->getName()),
        'description' => t('@label tokens.', [
          '@label' => Html::escape($field
            ->getName()),
        ]),
        'needs-data' => $field_token_name,
        'nested' => TRUE,
        'type' => 'metatag',
        'module' => 'metatag',
      ];
    }
  }
}