For fields of type "List (text)," the items in "Allowed Values" are entered in the "key|label' format.
I cannot figure out a way to output the key, as the label is the default for tokens related to a field of this type.
Typically, I believe that Drupal site builders prefer to indicate a machine-friendly string for the key, so that it may be used in arguments, etc.
For example, I'm using the "Custom Breadcrumbs" module and want to use the key instead of a label in creation of a path.
(This action I am requesting is possible in Views using the Formatter option upon configuring the display for a field.)
Comments
Comment #1
itaces commentedI was able to get this working in "Manage Display Settings" by enabling "Token" under "Custom Display Settings." Once that config option is enabled, then you can go to the field that uses select, and select from the dropdown in the "Format" column the "Key."
Usage of that field's token around the site now outputs the field's key.
Comment #2
ahillio commentedThat works, thank you!!!
Comment #3
Jarviss commentedHi Itaces! Enabling Token affects only default display. I try to get List (text) key in Display Suite code field and I see no tokens that could return field KEYs.
May you suggest how it can be achieved?
Comment #4
loopy1492 commentedI was able to find it. Here's a link to make things easier on people coming here in the future...
/admin/config/people/accounts/display
That's the "Custom Display Settings" at the bottom for the user accounts form. You can select "Token", then, in the field itself, select "Key" format.
You can do this with any content type.
Comment #5
yannickooWould be better to have a key and a value. Maybe we should move this to the Entity API module?
Comment #6
yannickooSorry but the original issue was to get the key and this is absolutely possible with just changing the formatter.
Comment #7
yenidem commentedHi itaces,
your solution is worked me too.. thank you for your idea :), I used it on Rules.
Comment #8
geek-merlinThe solution from #1 worked for me too, weird enough only for the field_foo (underscore) token, NOT the field-foo one.
Comment #9
divined commentedNot working when use in path alias. But work at user display.
Comment #10
jeramy commentedI just wanted to clarify this since I struggled with it for a while: This change did eventually work for me, though not immediately because I was using the wrong token.
I was trying to send an email using the Rules module that contained the Key value from my List (text) select list but kept getting the Label value even after making this change. It was not until I installed the Devel module and checked all available tokens that I realized that the token I was using [node:field-system-type-] still contained the Label, but (only after making the above suggested change) the token [node:field_system_type_] was also available and contained the key.
tl;dr If the above change doesn't seem to work try switching from dashes to underscores in your Token.
Comment #11
david.qdoscc commentedI was also struggling with this as I had list fields within a field collection that I needed to access the key via tokens. The following worked for me (note the mixture of -'s and _'s):
[node:field-collection-field:0:field_collection_listfield]
[node:field-collection-field:1:field_collection_listfield] etc.
Comment #12
roborew commentedHave been facing the same problem as divined where pathauto is still returning the label value. I would also like to use the Key value with the path alias, but leave the human readable select field. Changing the formatter made no difference.
I ran the debugger and found that that the problem lies in entity_tokens module, entity_tokens.tokens.inc line 310, with the _entity_token_get_token() function.
This seems to ignore the formatting setting when choosing Key in managing display. Issue is more likely related to the entity tokens module.
Comment #13
roborew commentedThis post https://www.drupal.org/node/1271348 suggests that Entity Tokens does not use display formatters, so once the entity_token_tokens() hook is fired up it should send back the plain data values. However as above this additional look-up for the label() is currently giving me the wrong value, as i'm after the default value.
My solution if it helps anyone else, I used hook_tokens_alter(), to reselect the original value.
Comment #14
roborew commentedI ended up rolling the solution into a module so I can use it on multiple sites.
https://www.drupal.org/sandbox/roboco/2709617
You can now use the field edit page to choose when you want to override the entity tokens formatting to the default or raw value, instead of using the formatting that happens in _entity_token_get_token().
Comment #15
philySolution suggested by itaces in comment #1 works for me in Rules.
As per comment #8 by axel.rutz, only when using underscore in field name token.
Thanks you guys.
Comment #16
dunx commentedA typo correction for roborew's code has this working very nicely for pathauto. Thanks.
Comment #17
silverham commentedUsing the token: (where field_myfield is a taxonomy field), seems to work on Drupal 9
[node:field_myfield:entity:field_list:value]