You are here

function metatag_token_info in Metatag 7

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

Implements hook_token_info().

File

./metatag.tokens.inc, line 11
Custom tokens for Metatag.

Code

function metatag_token_info() {
  $info = array();
  $info['types']['metatag'] = array(
    'name' => t('Meta tags'),
    'description' => t('Generated by the Metatag module, may not be used to fill in other meta tags.'),
  );
  $metatag_info = metatag_get_info();
  foreach ($metatag_info['tags'] as $value) {
    if (isset($value['group'], $metatag_info['groups'][$value['group']], $metatag_info['groups'][$value['group']]['label'])) {
      $label = $metatag_info['groups'][$value['group']]['label'] . ': ' . $value['label'];
    }
    else {
      $label = t('Basic tags: @label', array(
        '@label' => $value['label'],
      ));
    }
    $info['tokens']['metatag'][$value['name']] = array(
      'name' => $label,
      'description' => isset($value['description']) ? $value['description'] : '',
    );
  }
  if (module_exists('taxonomy')) {
    $info['tokens']['term']['metatag'] = array(
      'name' => t('Meta tags'),
      'description' => t('Meta tags for this taxonomy term.'),
      'type' => 'metatag',
    );
  }
  if (module_exists('node')) {
    $info['tokens']['node']['metatag'] = array(
      'name' => t('Meta tags'),
      'description' => t('Meta tags for this node.'),
      'type' => 'metatag',
    );
  }
  if (module_exists('user')) {
    $info['tokens']['user']['metatag'] = array(
      'name' => t('Meta tags'),
      'description' => t('Meta tags for this user.'),
      'type' => 'metatag',
    );
  }

  // A custom pager.
  $pager = variable_get('metatag_pager_string', 'Page PAGER | ');
  $page = str_replace('PAGER', 12, $pager);
  $info['tokens']['current-page']['pager'] = array(
    'name' => t('Custom pager'),
    'description' => t('A custom pager (from the Metatag module). Currently set to "@pager" which would be output as e.g. "@page".', array(
      '@pager' => $pager,
      '@page' => $page,
    )),
  );

  // Custom summary tokens for each long text field.
  $supported_types = array(
    'text_long',
    'text_with_summary',
  );
  if (variable_get('metatag_summary_text_field', FALSE)) {
    $supported_types[] = 'text';
  }
  $trim_length = variable_get('metatag_summary_length', 200);
  foreach (field_read_fields() as $field_name => $field) {
    if (!empty($field['module'])) {
      if (in_array($field['type'], $supported_types)) {
        $instances = field_read_instances(array(
          'field_name' => $field_name,
        ));
        foreach ($instances as $instance) {
          if (!empty($instance['entity_type']) && !empty($instance['label'])) {
            $info['tokens'][$instance['entity_type']][$field_name . '-summary'] = array(
              'name' => t('@label (summary)', array(
                '@label' => $instance['label'],
              )),
              'description' => t('A summary of the @label field, trimmed to @length characters.', array(
                '@label' => $instance['label'],
                '@length' => $trim_length,
              )),
            );
          }
        }
      }
    }
  }
  return $info;
}