diff --git a/core/modules/taxonomy/taxonomy.tokens.inc b/core/modules/taxonomy/taxonomy.tokens.inc index f6cfc6292ac5caa932b3aba80f2ffa8bf2a677e1..f80c83abb13ce4d72bba94ab90f70bf323e92f55 100644 --- a/core/modules/taxonomy/taxonomy.tokens.inc +++ b/core/modules/taxonomy/taxonomy.tokens.inc @@ -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'])) { diff --git a/core/modules/taxonomy/tests/src/Kernel/TokenReplaceTest.php b/core/modules/taxonomy/tests/src/Kernel/TokenReplaceTest.php index 70682693073df6969c44544a463bfe66fef43cb2..f59ae6b9b90340ccb2b8e82593f27b4dcf2e3776 100644 --- a/core/modules/taxonomy/tests/src/Kernel/TokenReplaceTest.php +++ b/core/modules/taxonomy/tests/src/Kernel/TokenReplaceTest.php @@ -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.