Verified Commit b23fad55 authored by Lauri Timmanee's avatar Lauri Timmanee
Browse files

Issue #3073554 by mpp, ravi.shankar, smustgrave, mark_fullmer, alexpott,...

Issue #3073554 by mpp, ravi.shankar, smustgrave, mark_fullmer, alexpott, acbramley, lukus, Gábor Hojtsy: Add a token for publication status
parent d6ac6d30
Loading
Loading
Loading
Loading
+8 −0
Original line number Diff line number Diff line
@@ -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;
+24 −0
Original line number Diff line number Diff line
@@ -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',