node.tokens.inc 7.0 KB

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