Verified Commit 900ea3f6 authored by Dave Long's avatar Dave Long
Browse files

Issue #3161892 by Abhijith S, purushotam.rai, smustgrave: Taxonomy term...

Issue #3161892 by Abhijith S, purushotam.rai, smustgrave: Taxonomy term [term:changed] token not available
parent f50c6a35
Loading
Loading
Loading
Loading
Loading
+25 −0
Changes for core/modules/taxonomy/taxonomy.tokens.inc: 25 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -5,6 +5,8 @@
 * Builds placeholder replacement tokens for taxonomy terms and vocabularies.
 */

use Drupal\Core\Datetime\Entity\DateFormat;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\Render\BubbleableMetadata;
use Drupal\taxonomy\Entity\Vocabulary;

@@ -78,6 +80,11 @@ function taxonomy_token_info() {
    'description' => t("The parent term of the taxonomy term, if one exists."),
    'type' => 'term',
  ];
  $term['changed'] = [
    'name' => t("Date changed"),
    'description' => t("The date the taxonomy was most recently updated."),
    'type' => 'date',
  ];

  return [
    'types' => $types,
@@ -94,6 +101,14 @@ function taxonomy_token_info() {
function taxonomy_tokens($type, $tokens, array $data, array $options, BubbleableMetadata $bubbleable_metadata) {
  $token_service = \Drupal::token();

  if (isset($options['langcode'])) {
    $url_options['language'] = \Drupal::languageManager()->getLanguage($options['langcode']);
    $langcode = $options['langcode'];
  }
  else {
    $langcode = LanguageInterface::LANGCODE_DEFAULT;
  }

  $replacements = [];
  if ($type == 'term' && !empty($data['term'])) {
    $term = $data['term'];
@@ -140,6 +155,12 @@ function taxonomy_tokens($type, $tokens, array $data, array $options, Bubbleable
            $replacements[$original] = $parent->getName();
          }
          break;

        case 'changed':
          $date_format = DateFormat::load('medium');
          $bubbleable_metadata->addCacheableDependency($date_format);
          $replacements[$original] = \Drupal::service('date.formatter')->format($term->getChangedTime(), 'medium', '', NULL, $langcode);
          break;
      }
    }

@@ -155,6 +176,10 @@ function taxonomy_tokens($type, $tokens, array $data, array $options, Bubbleable
        $replacements += $token_service->generate('term', $vocabulary_tokens, ['term' => $parent], $options, $bubbleable_metadata);
      }
    }

    if ($changed_tokens = $token_service->findWithPrefix($tokens, 'changed')) {
      $replacements += $token_service->generate('date', $changed_tokens, ['date' => $term->getChangedTime()], $options, $bubbleable_metadata);
    }
  }

  elseif ($type == 'vocabulary' && !empty($data['vocabulary'])) {
+6 −0
Changes for core/modules/taxonomy/tests/src/Kernel/TokenReplaceTest.php: 6 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -120,6 +120,9 @@ public function testTaxonomyTokenReplacement() {
    $tests['[term:url]'] = $term1->toUrl('canonical', ['absolute' => TRUE])->toString();
    $tests['[term:node-count]'] = 0;
    $tests['[term:parent:name]'] = '[term:parent:name]';
    /** @var \Drupal\Core\Datetime\DateFormatterInterface $date_formatter */
    $date_formatter = $this->container->get('date.formatter');
    $tests['[term:changed:since]'] = $date_formatter->formatTimeDiffSince($term1->getChangedTime(), ['langcode' => $language_interface->getId()]);
    $tests['[term:vocabulary:name]'] = $this->vocabulary->label();
    $tests['[term:vocabulary]'] = $this->vocabulary->label();

@@ -135,6 +138,8 @@ public function testTaxonomyTokenReplacement() {
    $bubbleable_metadata = clone $base_bubbleable_metadata;
    $metadata_tests['[term:vocabulary:name]'] = $bubbleable_metadata->addCacheTags($this->vocabulary->getCacheTags());
    $metadata_tests['[term:vocabulary]'] = $bubbleable_metadata->addCacheTags($this->vocabulary->getCacheTags());
    $bubbleable_metadata = clone $base_bubbleable_metadata;
    $metadata_tests['[term:changed:since]'] = $bubbleable_metadata->setCacheMaxAge(0);

    foreach ($tests as $input => $expected) {
      $bubbleable_metadata = new BubbleableMetadata();
@@ -153,6 +158,7 @@ public function testTaxonomyTokenReplacement() {
    $tests['[term:parent:name]'] = $term1->getName();
    $tests['[term:parent:url]'] = $term1->toUrl('canonical', ['absolute' => TRUE])->toString();
    $tests['[term:parent:parent:name]'] = '[term:parent:parent:name]';
    $tests['[term:changed:since]'] = $date_formatter->formatTimeDiffSince($term2->getChangedTime(), ['langcode' => $language_interface->getId()]);
    $tests['[term:vocabulary:name]'] = $this->vocabulary->label();

    // Test to make sure that we generated something for each token.