Skip to content
node.tokens.inc 6.78 KiB
Newer Older
<?php

/**
 * @file
 * Builds placeholder replacement tokens for node-related data.
 */

use Drupal\Core\Datetime\Entity\DateFormat;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\Render\BubbleableMetadata;
    'description' => t('Tokens related to individual content items, or "nodes".'),
    'name' => t("Content ID"),
    'description' => t('The unique ID of the content item, or "node".'),
    'name' => t("Revision ID"),
    'description' => t("The unique ID of the node's latest revision."),
    'name' => t("Content type name"),
    'description' => t("The human-readable name of the node type."),
    'name' => t("Body"),
    'description' => t("The main body text of the node."),
    'name' => t("Summary"),
    'description' => t("The summary of the node's main body text."),
    'name' => t('Language code'),
    'description' => t('The language code of the language the node is written in.'),
    'name' => t("URL"),
    'description' => t("The URL of the node."),
    'name' => t("Edit URL"),
    'description' => t("The URL of the node's edit page."),
    'name' => t("Date changed"),
    'description' => t("The date the node was most recently updated."),
    'type' => 'date',
  return [
    'types' => ['node' => $type],
    'tokens' => ['node' => $node],
  ];
function node_tokens($type, $tokens, array $data, array $options, BubbleableMetadata $bubbleable_metadata) {
  $url_options = ['absolute' => TRUE];
    $url_options['language'] = \Drupal::languageManager()->getLanguage($options['langcode']);
    $langcode = LanguageInterface::LANGCODE_DEFAULT;
    /** @var \Drupal\node\NodeInterface $node */
    $node = $data['node'];

    foreach ($tokens as $name => $original) {
      switch ($name) {
        // Simple key values on the node.
        case 'nid':
          $replacements[$original] = $node->id();
          $replacements[$original] = $node->getRevisionId();
          $replacements[$original] = $node->getType();
          $type_name = node_get_type_label($node);
          $replacements[$original] = $type_name;
          $replacements[$original] = $node->getTitle();
          $translation = \Drupal::entityManager()->getTranslationFromContext($node, $langcode, ['operation' => 'node_tokens']);
          if ($translation->hasField('body') && ($items = $translation->get('body')) && !$items->isEmpty()) {
            $item = $items[0];
            // If the summary was requested and is not empty, use it.
            if ($name == 'summary' && !empty($item->summary)) {
              $output = $item->summary_processed;
            }
            // Attempt to provide a suitable version of the 'body' field.
            else {
              // A summary was requested.
              if ($name == 'summary') {
                // Generate an optionally trimmed summary of the body field.

                // Get the 'trim_length' size used for the 'teaser' mode, if
                // present, or use the default trim_length size.
                $display_options = entity_get_display('node', $node->getType(), 'teaser')->getComponent('body');
                if (isset($display_options['settings']['trim_length'])) {
                  $length = $display_options['settings']['trim_length'];
                }
                else {
                  $settings = \Drupal::service('plugin.manager.field.formatter')->getDefaultSettings('text_summary_or_trimmed');
                $output = text_summary($output, $item->format, $length);
            // "processed" returns a \Drupal\Component\Render\MarkupInterface
            // via check_markup().
            $replacements[$original] = $output;
          $replacements[$original] = $node->language()->getId();
          $replacements[$original] = $node->url('canonical', $url_options);
          $replacements[$original] = $node->url('edit-form', $url_options);
          break;

        // Default values for the chained tokens handled below.
        case 'author':
          $account = $node->getOwner() ? $node->getOwner() : User::load(0);
          $bubbleable_metadata->addCacheableDependency($account);
          $replacements[$original] = $account->label();
          $date_format = DateFormat::load('medium');
          $bubbleable_metadata->addCacheableDependency($date_format);
          $replacements[$original] = format_date($node->getCreatedTime(), 'medium', '', NULL, $langcode);
          $date_format = DateFormat::load('medium');
          $bubbleable_metadata->addCacheableDependency($date_format);
          $replacements[$original] = format_date($node->getChangedTime(), 'medium', '', NULL, $langcode);
    if ($author_tokens = $token_service->findWithPrefix($tokens, 'author')) {
      $replacements += $token_service->generate('user', $author_tokens, ['user' => $node->getOwner()], $options, $bubbleable_metadata);
    if ($created_tokens = $token_service->findWithPrefix($tokens, 'created')) {
      $replacements += $token_service->generate('date', $created_tokens, ['date' => $node->getCreatedTime()], $options, $bubbleable_metadata);
    if ($changed_tokens = $token_service->findWithPrefix($tokens, 'changed')) {
      $replacements += $token_service->generate('date', $changed_tokens, ['date' => $node->getChangedTime()], $options, $bubbleable_metadata);