You are here

function metatag_token_info in Metatag 8

Same name and namespace in other branches
  1. 7 metatag.tokens.inc \metatag_token_info()

Implements hook_token_info().

File

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

Code

function metatag_token_info() {
  $info = [];
  $group_manager = \Drupal::service('plugin.manager.metatag.group');
  $tag_manager = \Drupal::service('plugin.manager.metatag.tag');
  $tag_definitions = $tag_manager
    ->getDefinitions();
  $info['types']['metatag'] = [
    'name' => t('Metatags'),
    'description' => t('Tokens related to Metatags.'),
    'needs-data' => 'metatag',
  ];
  foreach ($tag_definitions as $tag_id => $tag_definition) {
    $label = $tag_definition['label'];
    $description = $tag_definition['description'];
    $multiple = $tag_definition['multiple'];
    $metatag_token_name = 'metatag-' . $tag_id;
    $group = $group_manager
      ->getDefinition($tag_definition['group']);
    if ($group) {
      $label = $group['label'] . ': ' . $label;
    }
    $info['tokens']['current-page']['metatag'] = [
      'name' => t('Metatags'),
      'description' => t('Metatag values for the current page.'),
      'type' => 'metatag',
    ];
    $info['tokens']['metatag'][$tag_id] = [
      'name' => Html::escape($label),
      'description' => $description,
      'type' => $multiple ? "list<{$metatag_token_name}>" : $metatag_token_name,
    ];
    $info['types'][$metatag_token_name] = [
      'name' => Html::escape($label),
      'description' => t('@label tokens.', [
        '@label' => Html::escape($label),
      ]),
      'needs-data' => $metatag_token_name,
      'nested' => TRUE,
    ];
    $info['tokens'][$metatag_token_name]['value'] = [
      'name' => Html::escape($label),
      'module' => 'metatag',
      'description' => $description,
    ];

    // Tag list token type.
    if ($multiple) {
      $info['types']["list<{$metatag_token_name}>"] = [
        'name' => t('List of @type values', [
          '@type' => Html::escape($label),
        ]),
        'description' => t('Tokens for lists of @type values.', [
          '@type' => Html::escape($label),
        ]),
        'needs-data' => "list<{$metatag_token_name}>",
        'nested' => TRUE,
      ];

      // Show a different token for each tag delta.
      // Since we don't know how many there will be, we will just show 3.
      for ($delta = 0; $delta < 3; $delta++) {
        $info['tokens']["list<{$metatag_token_name}>"][$delta] = [
          'name' => t('@type type with delta @delta', [
            '@type' => Html::escape($label),
            '@delta' => $delta,
          ]),
          'module' => 'token',
          'type' => $metatag_token_name,
        ];
      }
    }
  }
  return $info;
}