hook_token_info() is used to expose data objects and their properties. The odd thing about this hook is that the data objects are declared separately from their tokens, which causes some confusion.
I attached a patch that updates system.api.php with a reworked hook_token_info() and hook_token_info_alter(). It groups data types and their tokens together, and moved the function of needs-data 'properties' to array keys, so the whole array structure is simper, and so is the documentation. Note that the patch indeed only updates the documentation. Once a consensus has been reached, the 7 hook_token_info() implementations in core can be changed, as well as the code that calls these hooks.
| Comment | File | Size | Author |
|---|---|---|---|
| #20 | drupal_1968214_06.patch | 30.61 KB | xano |
| #9 | drupal_1968214_02.patch | 41.3 KB | xano |
| #1 | drupal_1968214_00.patch | 6.48 KB | xano |
Comments
Comment #1
xanoComment #2
dave reidThis is critical information that tells us if the token is global, or requires input.
Comment #3
xanoGood point. Would it make sense to make that a boolean flag? The reason I removed it, was the seemingly duplicate information, as types usually have the same string as their array key and their needs-data value.
A concern that was just voiced on IRC was that the suggested solution does not allow developers to add tokens to existing types. I want to make token_info() use NestedArray, so this functionality is still available.
Comment #4
eaton commentedThat's precisely the reason that the two separate pools of hook data were initially defined. The set of "Tokenable things" and the set of "Discrete tokens" can be managed independently without explicitly digging in and altering someone else's data.
I don't have really strong feelings one way or another, as long as it doesn't prevent a dev from hucking arbitrary tokens into a given token space.
Comment #5
xanoSee #1911768: Make token and token type descriptions optional for a related issue that touches the same code.
Comment #6
xanotoken_info() uses module_invoke_all(), which was converted to use NestedArray a while ago.
The only usage of needs-data I have found is Token.module. Core does not use the property. I opted to go for the boolean flag, because it prevents duplicate data and also makes it easier to fix #1920688: Support multiple instances of the same token type in token replacement more gracefully.
Comment #7
xanoSome data types have a "type" property. It doesn't seem to be used in core, but Token.module uses it to allow types to extend other types. Core uses it for the current-user data type, and the proposed patch for #943028: 'Date' token namespace is ambiguous uses it as well. If core should continue to use it, I believe we should also perform the actual merge in core, rather than in Token.module, and we should document the property in system.api.php.
Comment #8
dave reidWe should have a separate issue for renaming 'needs-data' to a 'needs-data' => TRUE/FALSE flag instead.
Comment #9
xanoThis patch only changes the return value structure, and it adds documentation for tokens' "type" properties, and a few lines of code to actually support this in core. It leaves needs-data alone.
Comment #10
dave reidThis is looking really awesome. The only thing I can think of is that I actually prefer the tokens to be listed like they were before, but all in one array:
As compared to what #9 currently has in the hooks:
The former is a little more verbose, but I find it easier to read and scan, especially for people that want to look at the info hooks to figure out which tokens to alter.
If we agree to go with the first syntax, I would prefer to have it addressed not in a followup issue but here.
Comment #11
dave reidThis is a functional change, and should be removed for now. This will also conflict with #1969540: Convert token.inc to a service
Comment #12
xanoI opened #1969730: Token type 'type' property is used, but not supported for the base type bug. I also updated the syntax. Note that the patch still touches token.inc and conflicts with #1969540: Convert token.inc to a service. There's no way around that.
Comment #14
xanoThis patch depends on #1969540: Convert token.inc to a service. The test will obviously fail.
Comment #16
xano#14: drupal_1968214_04.patch queued for re-testing.
Comment #18
xanoRe-roll now that #1969540: Convert token.inc to a service has been committed.
Comment #19
dave reidThis seems potentially confusing that 'type' is back to back. We should move the latter 'type' up above with the other first-level key/values before 'tokens'.
Isn't this what the example code in hook_token_info() is for? This seems to be unnecessary. Also it's incorrect. The 'title' token uses 'label' instead of 'title' and the 'description' key/value is not optional yet.
We should probably just remove this chunk.
Function no longer exists, since it's the service now?
Comment #20
xanoRegarding the "type" properties for tokens and token types: I sorted the properties by name. You could also say the property has to come last, because it's optional.
Comment #21
xanoComment #22
tim.plunkettFixing tags
Comment #23
catchThis doesn't look impossible to implement with a bc layer - even if we had to do two info hooks for the different formats, so moving back to 8.x for now.
Comment #33
joachim commentedI think this should probably be closed in favour of #2233353: Convert Token API into plugins?
Comment #36
darvanen#33: I agree this should be closed in favour of #2233353: Convert Token API into plugins.
I'll leave it for 3 months for further discussion. If there is none I will close it after those three months have passed.