Background

Drupal core's "node" module (node.tokens.inc) registers replacement tokens for most base node data (content id, revision id, type, title, created, changed, author, langcode). It does not include a replacement token for the node status (published/unpublished).

Problem/Motivation

There are a number of contexts where sites might want to render, via a token, the node status:
1. The LinkIt module (and soon, Drupal core via #3317769: Add support for linking to entities in CKEditor 5) provides an autocomplete listing of linkable nodes whose display can be customized by tokens; in this context, it is useful to content editors to know whether the node they are linking is published or not.
2. The Rules module supports the ability to create logic decisions based on token values, and a common rule involves whether or not a node is published.
3. Content moderation notification systems (e.g., drupal.org/project/content_moderation_notifications) send information about changing node status/state to users, using tokens.

Proposed resolution

Add token for publication status: [node:published_status] (Published, Unpublished)

Architectural rationale for putting it in core, as opposed to a contrib module, is that the node entity is provided by core, node status (published/unpublished) is a paradigm originating in core, and the node module in core already has a number of similar tokens defined.

Alternatives considered/contraindications

Folks might want to change the label of the "publication_status" output. For example, if they wanted "Unpublished" to render as a more comprehensible "Not published." In that case, this would be better as site-specific customization.

Comments

mpp created an issue. See original summary.

mpp’s picture

Status: Active » Needs review
StatusFileSize
new1.3 KB

Added both the status as integer (0 or 1) and as a string (Publisher or Unpublished).

mpp’s picture

StatusFileSize
new1.31 KB

Seems 'publised' is in use for the publication date, renaming to 'published_status'

mpp’s picture

StatusFileSize
new2.81 KB

Added tests.

ducktape’s picture

Status: Needs review » Reviewed & tested by the community

Tested both tokens with Linkit, they work fine.

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 4: 3073554_4.patch, failed testing. View results

mpp’s picture

Status: Needs work » Needs review
StatusFileSize
new2.81 KB

Fix test

mpp’s picture

StatusFileSize
new2.8 KB

using published_status instead of publication_status.

mpp’s picture

StatusFileSize
new4.46 KB

Added a test for an unpublished node.

mpp’s picture

StatusFileSize
new3.98 KB

Revert deprecation of format_string to FormattableMarkup.

mpp’s picture

Issue summary: View changes

The last submitted patch, 7: 3073554_7.patch, failed testing. View results

The last submitted patch, 9: 3073554_9.patch, failed testing. View results

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

acbramley’s picture

Status: Needs review » Reviewed & tested by the community

Coincidentally needed the exact same for Linkit, patch does what it says on the tin and has tests, think this can be RTBC!

alexpott’s picture

Status: Reviewed & tested by the community » Needs review
  1. +++ b/core/modules/node/node.tokens.inc
    @@ -51,6 +51,14 @@ function node_token_info() {
    +  $node['status'] = [
    +    'name' => t('Status'),
    +    'description' => t('The publication status of the node (0 or 1).'),
    +  ];
    

    I'm not sure that this is that useful as a token. Why do we want to add it?

  2. +++ b/core/modules/node/node.tokens.inc
    @@ -51,6 +51,14 @@ function node_token_info() {
    +  $node['published_status'] = [
    +    'name' => t('Published'),
    +    'description' => t('The publication status of the node ("Published" or "Unpublished").'),
    +  ];
    
    @@ -167,6 +175,14 @@ function node_tokens($type, $tokens, array $data, array $options, BubbleableMeta
    +          $replacements[$original] = $node->isPublished() ? t('Published') : t('Unpublished');
    

    The one concern I have with this is how we also account for the content_moderation module. If you have that module enabled I think you'd want it to override this token and put its status label there. Or at least have it available too.

mpp’s picture

I'm not sure that this is that useful as a token. Why do we want to add it?

When adding a link to content it's useful to know whether its published or not. Linkit uses tokens to display the autocomplete results. But indeed the integer value might not be very useful...

The one concern I have with this is how we also account for the content_moderation module. If you have that module enabled I think you'd want it to override this token and put its status label there. Or at least have it available too.

Note that we use content_moderation but the status remains the same (it's either 0 or 1). In the context of content_moderation 'Draft' the node remains 'Unpublished' so this patch is still valid.
We could also add a moderation_status (Draft, Archived, ...) but I'd prefer that in a follow up issue.

azinck’s picture

This works well for me. Incidentally, I also wanted this for Linkit!

I agree with mpp that this is still very useful even without the content_moderation info and that the content_moderation state should be handled in a follow-up issue. We're also using content_moderation but for this purpose we just want the published status.

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

lukus’s picture

Status: Needs review » Reviewed & tested by the community

This is working well for me too!

I'm also using it for LinkIt, but also think this would work well for other circumstances where a list of content is provided via an autocomplete.

--

The one concern I have with this is how we also account for the content_moderation module. If you have that module enabled I think you'd want it to override this token and put its status label there. Or at least have it available too.

I also agree that content moderation should be a separate issue. The base concept of published / unpublished exists whether content_moderation is used or not.

I'm not sure that this is that useful as a token. Why do we want to add it?

I imagine 'status' could be useful for modules like Rules Tokens .. where logic based decisions are based on token input. Would be good to get confirmation whether this assumption is true though.

catch’s picture

Tagging for product manager review for #16.1 - it's also not clear to me how useful this is to provide in core.

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

spokje’s picture

Status: Reviewed & tested by the community » Needs work
Issue tags: +Needs reroll

Patch in #10 needs a reroll against 9.3.x.

Added tag and put on "Needs Work"

ravi.shankar’s picture

Status: Needs work » Needs review
Issue tags: -Needs reroll
StatusFileSize
new3.98 KB

Added reroll of patch #10 for Drupal 9.3.x.

Status: Needs review » Needs work

The last submitted patch, 25: 3073554-25.patch, failed testing. View results

ravi.shankar’s picture

Status: Needs work » Needs review
StatusFileSize
new3.99 KB
new876 bytes

Addressed failed test of patch #25.

kim.pepper’s picture

Issue tags: +#pnx-sprint
rp7’s picture

People looking for content moderation states token: #3225851: Add tokens for content moderation state

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

jaysonjaynes’s picture

The patch in #27 worked great for me for Linkit. Thank you!

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

jtotheeannie’s picture

The patch at #27 worked for us on LinkIt. Would love to get this into core!
Cheers

smustgrave’s picture

StatusFileSize
new2.68 KB
new3.99 KB

Tested #27 and it works

Uploading a tests-only patch and copy of #27 if the tests-only fails and regular passes I'll mark RTBC

The last submitted patch, 34: 3073554-34-tests-only.patch, failed testing. View results

smustgrave’s picture

Status: Needs review » Reviewed & tested by the community

Everything looks good.

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 34: 3073554-34.patch, failed testing. View results

smustgrave’s picture

Status: Needs work » Reviewed & tested by the community

Putting back as it seems to be a random failure with quickedit.

alexpott’s picture

Status: Reviewed & tested by the community » Needs review
+++ b/core/modules/node/node.tokens.inc
@@ -51,6 +51,14 @@ function node_token_info() {
+  $node['status'] = [
+    'name' => t('Status'),
+    'description' => t('The publication status of the node (0 or 1).'),
+  ];

We've still not provided reasons as to why this useful. See #16 / #17

The text version seems useful to people using linkit but #22 is still unanswered so setting back to needs review.

acbramley’s picture

+++ b/core/modules/node/tests/src/Kernel/NodeTokenReplaceTest.php
@@ -67,6 +67,8 @@ public function testNodeTokenReplacement() {
+    $tests['[node:status]'] = (int) $node->isPublished();
+    $tests['[node:published_status]'] = $node->isPublished() ? t('Published') : t('Unpublished');

@@ -88,6 +90,8 @@ public function testNodeTokenReplacement() {
+    $metadata_tests['[node:status]'] = $base_bubbleable_metadata;
+    $metadata_tests['[node:published_status]'] = $base_bubbleable_metadata;

@@ -108,6 +112,28 @@ public function testNodeTokenReplacement() {
+    $tests['[node:status]'] = (int) $node->isPublished();
+    $tests['[node:published_status]'] = $node->isPublished() ? t('Published') : t('Unpublished');

Shouldn't we be more explicit here about what we expect? And remove the t() usage in tests?

We know exactly what we expect, we don't need to use the node API to get the expected values. Copying the implementation code as test code seems wrong.

Also agree that the integer-based tokens seem unlikely to be useful enough to be in core.

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

smustgrave’s picture

Status: Needs review » Postponed (maintainer needs more info)

Re-reading the issue summary this seems like it probably should be handled by custom module. Mentions linkit so maybe that's where it should go?

Moving PNMI as don't know where else to put it.

mark_fullmer’s picture

Re-reading the issue summary this seems like it probably should be handled by custom module. Mentions linkit so maybe that's where it should go?

Some of the other comments (17, 21) mention other contexts where it would be useful to have a token that indicates the node status: Rules ("where logic based decisions are based on token input") and content moderation notifications (though #3225851: Add tokens for content moderation state will usually be more useful for folks than the published status). If there is more than one external context where this is relevant, it makes sense to put this in core: we wouldn't want to have to tell someone who's looking to use this in Rules that they need to install LinkIt!

An argument for not putting it in core is that folks might want to change the label of this output. For example, if they wanted "Unpublished" to render as a more comprehensible "Not published."

Implementing this in custom code, or in the LinkIt contrib module, is a facile change, but maybe there's an architectural rationale for putting it in core, too? The node entity is provided by core, node status (published/unpublished) is a paradigm originating in core, and the node module in core already has a number of similar tokens defined.

mark_fullmer’s picture

Issue summary: View changes

I've updated the issue description to better capture the community's conversation about this.

mark_fullmer’s picture

Status: Postponed (maintainer needs more info) » Needs review
smustgrave’s picture

Thanks! Unfortunately I can't review it since I worked on it some. Will have to see if someone else can.

needs-review-queue-bot’s picture

Status: Needs review » Needs work
StatusFileSize
new1.71 KB

The Needs Review Queue Bot tested this issue. It fails the Drupal core commit checks. Therefore, this issue status is now "Needs work".

Apart from a re-roll or rebase, this issue may need more work to address feedback in the issue or MR comments. To progress an issue, incorporate this feedback as part of the process of updating the issue. This helps other contributors to know what is outstanding.

Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.

mark_fullmer’s picture

Status: Needs work » Needs review
StatusFileSize
new3.32 KB
new3.73 KB

The attached patch:
1. Removes the integer-based token, since multiple commenters above cannot establish its value in core (catch in #22, alexpott in #39, acbramley in #40).
2. Updates the test coverage to explicitly look for 'Published' and 'Unpublished' text, and removes the t() usage, per #40.
3. Declares usage of FormattableMarkup to make syntax checking happy (#47).

Setting back to "Needs review"!

ricksta’s picture

Assigned: Unassigned » ricksta
Status: Needs review » Reviewed & tested by the community
StatusFileSize
new65.21 KB
new23.78 KB
new24.39 KB
new314.86 KB

I can confirm the patch works. Screenshots attached.

ricksta’s picture

Assigned: ricksta » Unassigned
catch’s picture

gábor hojtsy’s picture

Hm, I see not many people found the numeric token useful in comments but do we have similar tokens where we only expose a derivative textual value of the internal value in tokens and not the base value? It seems like an odd pattern to me.

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

guillaumeg’s picture

+1, patch working as expected.

mark_fullmer’s picture

do we have similar tokens where we only expose a derivative textual value of the internal value in tokens and not the base value. It seems like an odd pattern to me.

Yes, a number of the tokens in node.tokens.inc do this: the "author" token returns the username rather than the UID, and the "changed" and "created" tokens return formatted dates, rather than raw timestamps.

  • lauriii committed b23fad55 on 11.x
    Issue #3073554 by mpp, ravi.shankar, smustgrave, mark_fullmer, alexpott...
lauriii’s picture

Issue summary: View changes
Status: Reviewed & tested by the community » Fixed
Issue tags: -Needs product manager review

The Linkit use case seems suffcient for adding the proposed few lines of code. Added a CR to inform folks about the new token.

Committed b23fad5 and pushed to 11.x. Thanks!

guillaumeg’s picture

Awesome, thanks for getting that merged to 11.x !

Could we also get that change backported for 10.2 ?

lauriii’s picture

10.2.x will be branched from 11.x later this year and this will be in there 😊

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.