function metatag_tokens in Metatag 7
Same name and namespace in other branches
- 8 metatag.tokens.inc \metatag_tokens()
Implements hook_tokens().
File
- ./
metatag.tokens.inc, line 100 - Custom tokens for Metatag.
Code
function metatag_tokens($type, $tokens, array $data = array(), array $options = array()) {
$replacements = array();
$sanitize = !empty($options['sanitize']);
// Metatag tokens.
if ($type == 'metatag' && !empty($data['metatag'])) {
$metatag = $data['metatag'];
foreach ($tokens as $name => $original) {
if (isset($metatag[$name])) {
$replacements[$original] = $sanitize ? filter_xss($metatag[$name]) : $metatag[$name];
}
}
}
// Token tokens.
if ($type == 'term' && !empty($data['term'])) {
$term = $data['term'];
if ($metatag_tokens = token_find_with_prefix($tokens, 'metatag')) {
$result = metatag_token_generate_array($term, 'taxonomy_term', $term->vocabulary_machine_name);
$replacements += token_generate('metatag', $metatag_tokens, array(
'metatag' => $result,
), $options);
}
}
// Node tokens.
if ($type == 'node' && !empty($data['node'])) {
$node = $data['node'];
if ($metatag_tokens = token_find_with_prefix($tokens, 'metatag')) {
$result = metatag_token_generate_array($node, 'node', $node->type);
$replacements += token_generate('metatag', $metatag_tokens, array(
'metatag' => $result,
), $options);
}
}
// User tokens.
if ($type == 'user' && !empty($data['user'])) {
$account = $data['user'];
if ($metatag_tokens = token_find_with_prefix($tokens, 'metatag')) {
$result = metatag_token_generate_array($account, 'user', 'user');
$replacements += token_generate('metatag', $metatag_tokens, array(
'metatag' => $result,
), $options);
}
}
// Custom pager.
if ($type == 'current-page') {
foreach ($tokens as $name => $original) {
switch ($name) {
case 'pager':
$pager = metatag_get_current_pager();
if (!empty($pager)) {
$replacements[$original] = $pager;
}
break;
}
}
}
// Summary tokens. It is safe to rely upon an "entity" token type here because
// it is added by the Token module.
if ($type == 'entity' && isset($options['entity'], $options['entity_type'])) {
$trim_length = variable_get('metatag_summary_length', 200);
foreach ($tokens as $name => $original) {
if (substr($name, -8) == '-summary') {
// The field name is the token name minus "-summary".
$field_name = substr($name, 0, -8);
// Work out the requested language, if appropriate.
if (isset($options['language'])) {
$language_code = $options['language']->language;
}
else {
$language_code = NULL;
}
$entity = $options['entity'];
$entity_type = $options['entity_type'];
list($entity_id, $revision_id, $bundle) = entity_extract_ids($entity_type, $entity);
$items = field_get_items($entity_type, $entity, $field_name, $language_code);
if (!empty($items)) {
$instance = field_info_instance($entity_type, $field_name, $bundle);
$field_langcode = field_language($entity_type, $entity, $field_name, $language_code);
// If the summary was filled in it is not empty, use it.
if (!empty($items[0]['summary'])) {
$output = $sanitize ? _text_sanitize($instance, $field_langcode, $items[0], 'summary') : $items[0]['summary'];
}
else {
$output = $sanitize ? _text_sanitize($instance, $field_langcode, $items[0], 'value') : $items[0]['value'];
}
if (!empty($output)) {
// Trim the summary.
$output = text_summary($output, $instance['settings']['text_processing'] ? $items[0]['format'] : NULL, $trim_length);
if (!empty($output)) {
$replacements[$original] = $output;
}
}
}
}
}
}
return $replacements;
}