As discussed in #860082: BIKESHED: Token for a term or menu item's entire tree/hierarchy it would be useful if we can provide an array token type since it can be re-used by both a [menu-link:parents] token, a [term:parents] token, as well as [entity:field-name] tokens since fields are just an array of values. Plus this gives us implementations of a simple and complex use case.
Proposed tokens
[array:first] - first element of the array
[array:last] - last element of the array
[array:value:key] - dynamic token where 'key' is array[key]
[array:keys] - a chained 'array' token type of the keys of the array
[array:count] - the number of elements in the array
[array:join] - a comma-separated string of the values
[array:join:value] - this is a dynamic token, results of implode(value, array)
Comment | File | Size | Author |
---|---|---|---|
#13 | 1047740-13-token-array-test-fix-SINCE-9.txt | 1.97 KB | bfroehle |
#13 | 1047740-13-token-array-test-fix.patch | 7.38 KB | bfroehle |
#9 | 1047740-token-array-type.patch | 7.83 KB | Dave Reid |
Comments
Comment #1
BenK CreditAttribution: BenK commentedSubscribing
Comment #2
effulgentsia CreditAttribution: effulgentsia commentedsubscribe
Comment #3
fagoIn hook_entity_property_info() of the entity API, arrays are treated via the special data type "list". They are represented via numerically indexed arrays, thus the keys are always just numeric. So they are really just a list of items, where the keys have no meaning it all.
I think it would make sense the same way for token too, as if you support arbitrary key:value pairs there is not really difference to all other data structures supported in token.
Then other useful tokens I could think of, would be [array:count] and perhaps a variant imploding all strings with commas.
Also in case you don't mind, calling the array type 'list' in token too, would probably help to streamline both data description worlds a bit.
Comment #4
Dave ReidNaming it list is also something I'm not sold on. It could possibly confuse/conflict with the list field module? Note that most people are not going to be seeing the [array:] part of the token. It will mainly be a token that is used for things like [node:menu-link:parents] and [term:parents] as an array 'type' token.
Comment #5
fagoI like 'list' as it makes clear that we are not talking about associative arrays. But having not list/array part as the token is a good thing anyway.
Comment #6
manimejia CreditAttribution: manimejia commentedsubscribe ... great idea!
Comment #7
Scott J CreditAttribution: Scott J commentedWould this help replace Array Tokens module?
Comment #8
Dave ReidYes it would, but it would likely be a Drupal 7 only solution.
Comment #9
Dave ReidComment #10
Dave ReidMerging #1012202: Add 'Roles' token like [current-user:roles] and [user:roles] into this issue since it gives us a good real-world implementation of an array token type.
Comment #12
fagoWhat about array items that would require chaining, e.g. a list of nodes (node reference)?
Comment #13
bfroehle CreditAttribution: bfroehle commentedThe test case makes me think this should be array_reverse($array, TRUE) so that we preserve the array keys.
This should be check_plain, of course.
Attached a patch which fixes these bugs and makes the tests pass. One slight modification to the test was required, namely
which I believe is correct since value should return a text string.
I've also attached a diff of the changes since #9 for easy review.
Powered by Dreditor.
Comment #14
bfroehle CreditAttribution: bfroehle commentedI don't know how to handle this todo, or even if it needs to be handled before this patch is committed.
Powered by Dreditor.
Comment #15
daviesap CreditAttribution: daviesap commented+1
Comment #16
tsvenson CreditAttribution: tsvenson commented+1 Follow
Comment #17
SilviaT CreditAttribution: SilviaT commented+1
Comment #18
Dave ReidCommitted #13 for now to CVS. Working on how to handle the rest now while we're switching to Git.
http://drupal.org/cvs?commit=505086
Comment #19
bfroehle CreditAttribution: bfroehle commented@Dave Reid: not sure what you mean by "the rest"
One unintended problem here is core doesn't allow spaces in tokens, so a token like
will not actually work.
There might need to be a different character which is substituted in for a space in a replacement.
Comment #20
pillarsdotnet CreditAttribution: pillarsdotnet commentedurlencode() immediately comes to mind, but it would make the above
[user:roles:join:%2C+]
which is not exactly intuitive.Comment #21
bryancasler CreditAttribution: bryancasler commentedsubscribe
Comment #22
quimrovira CreditAttribution: quimrovira commentedWhat about complex array types?
Let's suppose we've got a node reference field, set up as a multiple value field. How should we try to handle this using token module? Could we just allow chaining the token expression to apply to each array element?
Some examples:
Ideally, we could have some sort of string/render fallback by default, but be able to treat arrays of values with a known corresponding token type definition. To handle this, we would need to add a "subtype" key to the tokens structure returned by hook_token_info, and get it on array type generate_token call.
If we had this, everything else would be a matter of ripping the array function (first,last,join,nth,etc.) off the token, mapping the array using the remaining token expression using the given subtype as token_generate's type, and applying the array function to the resulting list.
In the end, all that matters would be offering some token definitions for other modules like:
An idea to work around the array problem might be by creating additional token types on the fly, or maybe using hook_token_info_alter itself to allow "array-of-subtype" declarations, which seems like a neater idea.
What do you think?
Comment #23
Trunkhorn CreditAttribution: Trunkhorn commentedsub
Comment #24
ldweeks CreditAttribution: ldweeks commentedSubscribing...
Comment #25
bryancasler CreditAttribution: bryancasler commentedbump
Comment #26
anruethersubscribe
Comment #27
rorymadden CreditAttribution: rorymadden commentedSubscribe. Is this still a blocker for #691078: Field tokens?
Comment #28
bryancasler CreditAttribution: bryancasler commentedDave Reid, I just upgraded to the new Beta 2 Token release and I noticed some possibly new options.
http://awesomescreenshot.com/09edg9551
http://awesomescreenshot.com/024dg9add
Is this issue resolved?
EDIT: I just remembered I am using this token patch from #143 as suggested by #262. #691078: Field tokens
Comment #29
Jackinloadup CreditAttribution: Jackinloadup commentedsubscribe
Comment #30
Dave ReidI'm going to consider this part of the issue fixed, the rest will be a follow-up as a part of #691078: Field tokens which will solve the complex use case.
Comment #32
Ahmed Pc CreditAttribution: Ahmed Pc commentedI have a problem, I want to get the first item of [upload_fid] token, I have tried to use [upload_fid:first] but it did not work, I am a beginner in drupal, could you please help me ???
Comment #33
bfroehle CreditAttribution: bfroehle commentedAhmed Pc: Please create a new issue.
Comment #34
Ahmed Pc CreditAttribution: Ahmed Pc commentedI have created a new issue
http://drupal.org/node/1833130