The idea is to build tokens that take a parameter instead of a fixed name and then use them for things as replacing local urls for help texts or links to drupal.org.
Example tokens:
[local:admin/modules] -> will expand to a local url pointing to the module administration page
[documentation:handbook/modules/system] -> will expand to a link to external documentation, which defaults to drupal.org, in this case http://drupal.org/handbook/modules/system
To my surprise we just need to define the token types and it just works adding some minimal logic in hook_tokens(). Though it wouldn't be bad to really support multiple parameters separated by ':' and adding some mapping logic to the token system.
The patch creates these two token types (local, documentation) and uses them for the first few lines of system module help. Also help texts are greatly simplified by this:
// Old help text (note the parameters at the end)
$output .= '<dd>' . t('The System module allows users with the appropriate permissions to enable and disable modules on the <a href="@modules">Modules administration page</a>. Drupal comes with a number of core modules, and each module provides a discrete set of features and may be enabled or disabled depending on the needs of the site. Many additional modules contributed by members of the Drupal community are available for download at the <a href="@drupal-modules">Drupal.org module page</a>.', array('@modules' => url('admin/modules'), '@drupal-modules' => 'http://drupal.org/project/modules')) . '</dd>';
// New help text (not variables are replaced by tokens)
$output .= '<dd>' . t('The System module allows users with the appropriate permissions to enable and disable modules on the <a href="[local:admin/modules]">Modules administration page</a>. Drupal comes with a number of core modules, and each module provides a discrete set of features and may be enabled or disabled depending on the needs of the site. Many additional modules contributed by members of the Drupal community are available for download at the <a href="[documentation:project/modules]">Drupal.org module page</a>.') . '</dd>';
Of course we need to run help text through token replacement.
| Comment | File | Size | Author |
|---|---|---|---|
| #5 | 1445144_parametrized_token_help_02.patch | 6.96 KB | jose reyero |
| parametrized_tokens_help.patch | 6.53 KB | jose reyero |
Comments
Comment #1
damien tournoud commentedSounds like a smart idea!
Most likely, those need to be
check_plain()-ed, right?Comment #2
miro_dietikerGreat idea.
How would you treat the difference of e.g. internal help links ... help links to drupal.org manuals ... APIs and even contrib specific sources?
Is your solution only intended to cover drupal.org links?
While i see a good reason to e.g. cover specific documentation on a custom server, i would expect to never see the drupal.org manpages copied/mirrored to a custom location. So if i do mirroring for a specific project, i only want to redirect those links that i mirror.
What kind of separation do you think makes sense (and possibly what kind of extensibility)?
Comment #3
dave reidWhat I can say about this is that this is kinda an abuse of the token system as you wouldn't have any tokens defined in hook_token_info() since it's just [internal:*] and [documentation:*] and there's nothing actually *defined* in the second part of the token - this will break things like token tree display.
Comment #4
dave reidComment #5
jose reyero commentedThis is a new version of the patch addressing some of the concerns above:
- Adds a token name, tokens now look like: [url:local:admin/modules]
- Some filtering (check_url, check_plain)
Comment #6
jose reyero commentedComment #7
jhodgdonFrom a help-writing perspective, I'm +1 on the concept, as long as Dave Reid is OK with the Token implementation...
Regarding that, the Token implementation seems a little backwards from the rest of the token system. For instance, if you are doing something with a comment and want a token representing the last login date of the user who created the node being commented on, you do something like (I may have the details wrong, but something like this):
[comment:node:author:last-login:short]
This is a chain that progresses from the starting point (the comment) through different pieces of information, with each part of the token supplying its output as the next item's input, and where the items are from different modules' token types. Also, at each point in the chain, as you are building up a token, something like the Rules UI can list for you all possible next items you can put in the chain, because they are each explicitly listed in a hook_token_info() implementation. (For instance, if you have done comment:node, it knows to then give you the node: tokens supplied by various modules.)
It doesn't seem like this proposal will work with either chaining or with the idea of being able to list all available possibilities, in its present form, and given that the current Token system (I think) doesn't support variable next-item-in-chain.
{As a side note, I just filed #1483662: URLs for drupal.org module help pages need updates because I noticed the URLs for help docs are out of date.}
Comment #8
dave reidI still just don't really understand what the benefit is here of using tokens compared to the existing method of putting links in with t(). Also the 'url' token type is kind of already reserved by the Token module to move into core: http://drupalcode.org/project/token.git/blob/8f89f245a332ecf69c6d9d77716...
Comment #9
jose reyero commented@jhodgdon
[comment:node:author:last-login:short]
Really? Where can I read about that?
@Dave Reid
Currently writing help text for modules is painful and one of the reasons is all these variable replacements.
Anyway this should be only the first step because once we have simpler texts (no texts + variable generating code) we can add better handling for them, like making them configurable, or fit into the config files, or simple text files.
Also we'd make the whole system more consistent as I understand token should be the preferred way for replacing text parameters. Plus it will be more flexible as tokens can be altered. For instance, when you have a link to some external documentation (usually drupal.org) you'd like to redirect to some other place.
Related (these would be much easier using tokens and will be updated accordingly):
#1444980: Add renderable / localizable text element - make modules help text readable and flexible - pluggable localization
#517044: Improve text handling, texts in text files (editable, overridable, translatable, etc...)
Comment #10
jhodgdonRE [comment:node:author:last-login:short] -- that is what you do when setting up certain Rules in the Rules module... Let's see...
hook_token_info() - this describes the "needs-data" and "type" parts of tokens.
Hm. It doesn't look like the core token_replace() supports this actually... It's possible that this is just something Rules does, and not part of the base Token system, but it seems to key off these needs-data and type parts of tokens from the core system.
Probably Dave Reid would know better than I would. :)
Comment #11
dave reidI also think that this is better suited for a filter with using documentation://handbook/... and internal://admin/help rather than using tokens for them. It would be nice if we could run the help text through the filter system rather than tokens.
Comment #12
dave reidSee also #401956: Provide an [internal] filter
Comment #13
jhodgdonRE #11 -- yes, a filter makes more sense than tokens to me too, since I don't think this fits with the Tokens paradigm. But I also don't see the problem with using t() like we always have, and substituting in URLs. That pattern is all over core, in shorter text pieces, and I don't see why we would want to invent another pattern just for help?
Comment #14
dave reidYeah I don't see why we need to change what we're doing either. I also don't get why you'd want to map *all* d.org links to a different domain either. You'd have to support every single possible link.
Comment #15
dave reidIf you want to rewrite d.org links, it would be better just to run them through url() so that they could be modified with hook_url_outbound_alter() instead.
Comment #16
jose reyero commented@Dave Reid,
We've just changed all d.org links, which took a big patch. It would have been a one line change if we had this, http://drupalcode.org/project/drupal.git/commitdiff/efc984dfda053550ea6e...
(Plus we'll need to update *all* contrib modules' links to d.org documentation.)
Comment #17
miro_dietiker5: 1445144_parametrized_token_help_02.patch queued for re-testing.
Comment #32
smustgrave commentedThank you for sharing your idea for improving Drupal.
We are working to decide if this proposal meets the Criteria for evaluating proposed changes. There hasn't been any discussion here for over 8 years which suggests that this has either been implemented or there is no community support. Your thoughts on this will allow a decision to be made.
Since we need more information to move forward with this issue, the status is now Postponed (maintainer needs more info). If we don't receive additional information to help with the issue, it may be closed after three months.
Thanks!
Comment #33
smustgrave commentedSince there's been no follow up going to close this one out, but if still a valid task in D11 we can always re-open
Thanks!