diff --git a/core/modules/node/node.tokens.inc b/core/modules/node/node.tokens.inc index 30bd1c6b958f81cd2bd5c4ec2e0c0d6f8245729b..5855d0ac599f887456de6883c8150e6f7fa0c272 100644 --- a/core/modules/node/node.tokens.inc +++ b/core/modules/node/node.tokens.inc @@ -51,6 +51,10 @@ function node_token_info() { 'name' => t('Language code'), 'description' => t('The language code of the language the node is written in.'), ]; + $node['published_status'] = [ + 'name' => t('Published'), + 'description' => t('The publication status of the node ("Published" or "Unpublished").'), + ]; $node['url'] = [ 'name' => t("URL"), 'description' => t("The URL of the node."), @@ -167,6 +171,10 @@ function node_tokens($type, $tokens, array $data, array $options, BubbleableMeta $replacements[$original] = $node->language()->getId(); break; + case 'published_status': + $replacements[$original] = $node->isPublished() ? t('Published') : t('Unpublished'); + break; + case 'url': $replacements[$original] = $node->toUrl('canonical', $url_options)->toString(); break; diff --git a/core/modules/node/tests/src/Kernel/NodeTokenReplaceTest.php b/core/modules/node/tests/src/Kernel/NodeTokenReplaceTest.php index a18faf871e8cf242568b4f470d4baf7bdfc82ddf..e1c431db01e463eb9fe844f7689584d8866a7eb7 100644 --- a/core/modules/node/tests/src/Kernel/NodeTokenReplaceTest.php +++ b/core/modules/node/tests/src/Kernel/NodeTokenReplaceTest.php @@ -3,6 +3,7 @@ namespace Drupal\Tests\node\Kernel; use Drupal\Component\Utility\Html; +use Drupal\Component\Render\FormattableMarkup; use Drupal\Core\Render\BubbleableMetadata; use Drupal\node\Entity\Node; use Drupal\node\Entity\NodeType; @@ -65,6 +66,7 @@ public function testNodeTokenReplacement() { $tests['[node:body]'] = $node->body->processed; $tests['[node:summary]'] = $node->body->summary_processed; $tests['[node:langcode]'] = $node->language()->getId(); + $tests['[node:published_status]'] = 'Published'; $tests['[node:url]'] = $node->toUrl('canonical', $url_options)->toString(); $tests['[node:edit-url]'] = $node->toUrl('edit-form', $url_options)->toString(); $tests['[node:author]'] = $account->getAccountName(); @@ -86,6 +88,7 @@ public function testNodeTokenReplacement() { $metadata_tests['[node:body]'] = $base_bubbleable_metadata; $metadata_tests['[node:summary]'] = $base_bubbleable_metadata; $metadata_tests['[node:langcode]'] = $base_bubbleable_metadata; + $metadata_tests['[node:published_status]'] = $base_bubbleable_metadata; $metadata_tests['[node:url]'] = $base_bubbleable_metadata; $metadata_tests['[node:edit-url]'] = $base_bubbleable_metadata; $bubbleable_metadata = clone $base_bubbleable_metadata; @@ -106,6 +109,27 @@ public function testNodeTokenReplacement() { $this->assertEquals($metadata_tests[$input], $bubbleable_metadata); } + // Repeat for an unpublished node. + $node = Node::create([ + 'type' => 'article', + 'uid' => $account->id(), + 'title' => '<blink>Blinking Text</blink>', + ]); + $node->setUnpublished(); + $node->save(); + + // Generate and test tokens. + $tests = []; + $tests['[node:published_status]'] = 'Unpublished'; + + // Test to make sure that we generated something for each token. + $this->assertFalse(in_array(0, array_map('strlen', $tests)), 'No empty tokens generated for unpublished node.'); + + foreach ($tests as $input => $expected) { + $output = $this->tokenService->replace($input, ['node' => $node], ['language' => $this->interfaceLanguage]); + $this->assertEquals($output, $expected, new FormattableMarkup('Node token %token replaced for unpublished node.', ['%token' => $input])); + } + // Repeat for a node without a summary. $node = Node::create([ 'type' => 'article',