comparison core/modules/node/node.tokens.inc @ 0:c75dbcec494b

Initial commit from drush-created site
author Chris Cannam
date Thu, 05 Jul 2018 14:24:15 +0000
parents
children 12f9dff5fda9
comparison
equal deleted inserted replaced
-1:000000000000 0:c75dbcec494b
1 <?php
2
3 /**
4 * @file
5 * Builds placeholder replacement tokens for node-related data.
6 */
7
8 use Drupal\Core\Datetime\Entity\DateFormat;
9 use Drupal\Core\Language\LanguageInterface;
10 use Drupal\Core\Render\BubbleableMetadata;
11 use Drupal\user\Entity\User;
12
13 /**
14 * Implements hook_token_info().
15 */
16 function node_token_info() {
17 $type = [
18 'name' => t('Nodes'),
19 'description' => t('Tokens related to individual content items, or "nodes".'),
20 'needs-data' => 'node',
21 ];
22
23 // Core tokens for nodes.
24 $node['nid'] = [
25 'name' => t("Content ID"),
26 'description' => t('The unique ID of the content item, or "node".'),
27 ];
28 $node['vid'] = [
29 'name' => t("Revision ID"),
30 'description' => t("The unique ID of the node's latest revision."),
31 ];
32 $node['type'] = [
33 'name' => t("Content type"),
34 ];
35 $node['type-name'] = [
36 'name' => t("Content type name"),
37 'description' => t("The human-readable name of the node type."),
38 ];
39 $node['title'] = [
40 'name' => t("Title"),
41 ];
42 $node['body'] = [
43 'name' => t("Body"),
44 'description' => t("The main body text of the node."),
45 ];
46 $node['summary'] = [
47 'name' => t("Summary"),
48 'description' => t("The summary of the node's main body text."),
49 ];
50 $node['langcode'] = [
51 'name' => t('Language code'),
52 'description' => t('The language code of the language the node is written in.'),
53 ];
54 $node['url'] = [
55 'name' => t("URL"),
56 'description' => t("The URL of the node."),
57 ];
58 $node['edit-url'] = [
59 'name' => t("Edit URL"),
60 'description' => t("The URL of the node's edit page."),
61 ];
62
63 // Chained tokens for nodes.
64 $node['created'] = [
65 'name' => t("Date created"),
66 'type' => 'date',
67 ];
68 $node['changed'] = [
69 'name' => t("Date changed"),
70 'description' => t("The date the node was most recently updated."),
71 'type' => 'date',
72 ];
73 $node['author'] = [
74 'name' => t("Author"),
75 'type' => 'user',
76 ];
77
78 return [
79 'types' => ['node' => $type],
80 'tokens' => ['node' => $node],
81 ];
82 }
83
84 /**
85 * Implements hook_tokens().
86 */
87 function node_tokens($type, $tokens, array $data, array $options, BubbleableMetadata $bubbleable_metadata) {
88 $token_service = \Drupal::token();
89
90 $url_options = ['absolute' => TRUE];
91 if (isset($options['langcode'])) {
92 $url_options['language'] = \Drupal::languageManager()->getLanguage($options['langcode']);
93 $langcode = $options['langcode'];
94 }
95 else {
96 $langcode = LanguageInterface::LANGCODE_DEFAULT;
97 }
98 $replacements = [];
99
100 if ($type == 'node' && !empty($data['node'])) {
101 /** @var \Drupal\node\NodeInterface $node */
102 $node = $data['node'];
103
104 foreach ($tokens as $name => $original) {
105 switch ($name) {
106 // Simple key values on the node.
107 case 'nid':
108 $replacements[$original] = $node->id();
109 break;
110
111 case 'vid':
112 $replacements[$original] = $node->getRevisionId();
113 break;
114
115 case 'type':
116 $replacements[$original] = $node->getType();
117 break;
118
119 case 'type-name':
120 $type_name = node_get_type_label($node);
121 $replacements[$original] = $type_name;
122 break;
123
124 case 'title':
125 $replacements[$original] = $node->getTitle();
126 break;
127
128 case 'body':
129 case 'summary':
130 $translation = \Drupal::entityManager()->getTranslationFromContext($node, $langcode, ['operation' => 'node_tokens']);
131 if ($translation->hasField('body') && ($items = $translation->get('body')) && !$items->isEmpty()) {
132 $item = $items[0];
133 // If the summary was requested and is not empty, use it.
134 if ($name == 'summary' && !empty($item->summary)) {
135 $output = $item->summary_processed;
136 }
137 // Attempt to provide a suitable version of the 'body' field.
138 else {
139 $output = $item->processed;
140 // A summary was requested.
141 if ($name == 'summary') {
142 // Generate an optionally trimmed summary of the body field.
143
144 // Get the 'trim_length' size used for the 'teaser' mode, if
145 // present, or use the default trim_length size.
146 $display_options = entity_get_display('node', $node->getType(), 'teaser')->getComponent('body');
147 if (isset($display_options['settings']['trim_length'])) {
148 $length = $display_options['settings']['trim_length'];
149 }
150 else {
151 $settings = \Drupal::service('plugin.manager.field.formatter')->getDefaultSettings('text_summary_or_trimmed');
152 $length = $settings['trim_length'];
153 }
154
155 $output = text_summary($output, $item->format, $length);
156 }
157 }
158 // "processed" returns a \Drupal\Component\Render\MarkupInterface
159 // via check_markup().
160 $replacements[$original] = $output;
161 }
162 break;
163
164 case 'langcode':
165 $replacements[$original] = $node->language()->getId();
166 break;
167
168 case 'url':
169 $replacements[$original] = $node->url('canonical', $url_options);
170 break;
171
172 case 'edit-url':
173 $replacements[$original] = $node->url('edit-form', $url_options);
174 break;
175
176 // Default values for the chained tokens handled below.
177 case 'author':
178 $account = $node->getOwner() ? $node->getOwner() : User::load(0);
179 $bubbleable_metadata->addCacheableDependency($account);
180 $replacements[$original] = $account->label();
181 break;
182
183 case 'created':
184 $date_format = DateFormat::load('medium');
185 $bubbleable_metadata->addCacheableDependency($date_format);
186 $replacements[$original] = format_date($node->getCreatedTime(), 'medium', '', NULL, $langcode);
187 break;
188
189 case 'changed':
190 $date_format = DateFormat::load('medium');
191 $bubbleable_metadata->addCacheableDependency($date_format);
192 $replacements[$original] = format_date($node->getChangedTime(), 'medium', '', NULL, $langcode);
193 break;
194 }
195 }
196
197 if ($author_tokens = $token_service->findWithPrefix($tokens, 'author')) {
198 $replacements += $token_service->generate('user', $author_tokens, ['user' => $node->getOwner()], $options, $bubbleable_metadata);
199 }
200
201 if ($created_tokens = $token_service->findWithPrefix($tokens, 'created')) {
202 $replacements += $token_service->generate('date', $created_tokens, ['date' => $node->getCreatedTime()], $options, $bubbleable_metadata);
203 }
204
205 if ($changed_tokens = $token_service->findWithPrefix($tokens, 'changed')) {
206 $replacements += $token_service->generate('date', $changed_tokens, ['date' => $node->getChangedTime()], $options, $bubbleable_metadata);
207 }
208 }
209
210 return $replacements;
211 }