-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmetatag.tokens.inc
118 lines (98 loc) · 3.2 KB
/
metatag.tokens.inc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
<?php
/**
* @file
* Custom tokens for Metatag.
*/
/**
* Implements hook_token_info().
*/
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) {
$info['tokens']['metatag'][$value['name']] = array(
'name' => $value['label'],
'description' => $value['description']
);
}
if (\Drupal::moduleHandler()->moduleExists('taxonomy')) {
$info['tokens']['term']['metatag'] = array(
'name' => t('Metatag.'),
'description' => t('Metatag.'),
'type' => 'metatag'
);
}
if (\Drupal::moduleHandler()->moduleExists('node')) {
$info['tokens']['node']['metatag'] = array(
'name' => t('Metatag.'),
'description' => t('Metatag.'),
'type' => 'metatag'
);
}
return $info;
}
/**
* Implements hook_tokens().
*/
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);
}
}
return $replacements;
}
/**
* Generate an array of meta tags for a given entity.
*/
function metatag_token_generate_array($entity, $entity_type, $bundle) {
if (metatag_entity_supports_metatags($entity_type, $bundle)) {
$token_type = token_get_entity_mapping('entity', $entity_type);
$instance = "{$entity_type}:{$bundle}";
$options = array();
$options['token data'][$token_type] = $entity;
$options['entity'] = $entity;
$metatags = array();
if (!empty($entity->metatags)) {
$language = metatag_entity_get_language($entity_type, $entity);
if (!empty($entity->metatags[$language])) {
$metatags = $entity->metatags[$language];
}
}
$metatags += metatag_config_load_with_defaults($instance);
$result = array();
foreach ($metatags as $metatag => $data) {
if ($metatag_instance = metatag_get_instance($metatag, $data)) {
$result[$metatag] = $metatag_instance->getValue($options);
}
}
return $result;
}
return NULL;
}