From b23fad55a8c8aee4757984b9ac75f0a0b8cfe629 Mon Sep 17 00:00:00 2001
From: Lauri Eskola <lauri.eskola@acquia.com>
Date: Tue, 4 Jul 2023 11:56:33 +0300
Subject: [PATCH] =?UTF-8?q?Issue=20#3073554=20by=20mpp,=20ravi.shankar,=20?=
 =?UTF-8?q?smustgrave,=20mark=5Ffullmer,=20alexpott,=20acbramley,=20lukus,?=
 =?UTF-8?q?=20G=C3=A1bor=20Hojtsy:=20Add=20a=20token=20for=20publication?=
 =?UTF-8?q?=20status?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 core/modules/node/node.tokens.inc             |  8 +++++++
 .../tests/src/Kernel/NodeTokenReplaceTest.php | 24 +++++++++++++++++++
 2 files changed, 32 insertions(+)

diff --git a/core/modules/node/node.tokens.inc b/core/modules/node/node.tokens.inc
index 30bd1c6b958f..5855d0ac599f 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 a18faf871e8c..e1c431db01e4 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',
-- 
GitLab