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.
| Comment | File | Size | Author |
|---|---|---|---|
| #49 | node-publish-status-4.png | 314.86 KB | ricksta |
| #49 | node-publish-status-3.png | 24.39 KB | ricksta |
| #49 | node-publish-status-2.png | 23.78 KB | ricksta |
| #49 | node-publish-status-1.png | 65.21 KB | ricksta |
| #48 | 3073554-34_48.interdiff.txt | 3.32 KB | mark_fullmer |
Comments
Comment #2
mpp commentedAdded both the status as integer (0 or 1) and as a string (Publisher or Unpublished).
Comment #3
mpp commentedSeems 'publised' is in use for the publication date, renaming to 'published_status'
Comment #4
mpp commentedAdded tests.
Comment #5
ducktape commentedTested both tokens with Linkit, they work fine.
Comment #7
mpp commentedFix test
Comment #8
mpp commentedusing published_status instead of publication_status.
Comment #9
mpp commentedAdded a test for an unpublished node.
Comment #10
mpp commentedRevert deprecation of format_string to FormattableMarkup.
Comment #11
mpp commentedComment #15
acbramley commentedCoincidentally needed the exact same for Linkit, patch does what it says on the tin and has tests, think this can be RTBC!
Comment #16
alexpottI'm not sure that this is that useful as a token. Why do we want to add it?
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.
Comment #17
mpp commentedWhen 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...
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.
Comment #18
azinck commentedThis 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.
Comment #21
lukusThis 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.
--
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 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.
Comment #22
catchTagging for product manager review for #16.1 - it's also not clear to me how useful this is to provide in core.
Comment #24
spokjePatch in #10 needs a reroll against
9.3.x.Added tag and put on "Needs Work"
Comment #25
ravi.shankar commentedAdded reroll of patch #10 for Drupal 9.3.x.
Comment #27
ravi.shankar commentedAddressed failed test of patch #25.
Comment #28
kim.pepperComment #29
rp7 commentedPeople looking for content moderation states token: #3225851: Add tokens for content moderation state
Comment #31
jaysonjaynes commentedThe patch in #27 worked great for me for Linkit. Thank you!
Comment #33
jtotheeannie commentedThe patch at #27 worked for us on LinkIt. Would love to get this into core!
Cheers
Comment #34
smustgrave commentedTested #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
Comment #36
smustgrave commentedEverything looks good.
Comment #38
smustgrave commentedPutting back as it seems to be a random failure with quickedit.
Comment #39
alexpottWe'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.
Comment #40
acbramley commentedShouldn'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.
Comment #42
smustgrave commentedRe-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.
Comment #43
mark_fullmerSome 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.
Comment #44
mark_fullmerI've updated the issue description to better capture the community's conversation about this.
Comment #45
mark_fullmerComment #46
smustgrave commentedThanks! Unfortunately I can't review it since I worked on it some. Will have to see if someone else can.
Comment #47
needs-review-queue-bot commentedThe 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.
Comment #48
mark_fullmerThe 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
FormattableMarkupto make syntax checking happy (#47).Setting back to "Needs review"!
Comment #49
ricksta commentedI can confirm the patch works. Screenshots attached.
Comment #50
ricksta commentedComment #51
catchComment #52
gábor hojtsyHm, 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.
Comment #54
guillaumeg commented+1, patch working as expected.
Comment #55
mark_fullmerYes, 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.
Comment #57
lauriiiThe 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!
Comment #58
guillaumeg commentedAwesome, thanks for getting that merged to 11.x !
Could we also get that change backported for 10.2 ?
Comment #59
lauriii10.2.x will be branched from 11.x later this year and this will be in there 😊