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

itaces’s picture

I 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.

ahillio’s picture

That works, thank you!!!

Jarviss’s picture

Hi 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?

loopy1492’s picture

I 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.

yannickoo’s picture

Would be better to have a key and a value. Maybe we should move this to the Entity API module?

yannickoo’s picture

Status: Active » Closed (fixed)

Sorry but the original issue was to get the key and this is absolutely possible with just changing the formatter.

yenidem’s picture

Hi itaces,
your solution is worked me too.. thank you for your idea :), I used it on Rules.

geek-merlin’s picture

The solution from #1 worked for me too, weird enough only for the field_foo (underscore) token, NOT the field-foo one.

divined’s picture

Issue summary: View changes

Not working when use in path alias. But work at user display.

jeramy’s picture

I 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.

david.qdoscc’s picture

I 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.

roborew’s picture

Have 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.

  // If there is a label for a property, e.g. defined by an options list or an
  // entity label, make use of it.
  if ($label = $wrapper->label()) {
    return empty($options['sanitize']) ? $label : check_plain($label);
  }

This seems to ignore the formatting setting when choosing Key in managing display. Issue is more likely related to the entity tokens module.

roborew’s picture

This 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.

function MODULENAME_tokens_alter(array &$replacements, array $context) {
  $options = $context['options'];
  if($context['type'] == 'entity'
    && isset($options['pathauto'])
    && isset($replacements['[node:NAME_OF_FIELD]'])) {
    $entity = $context['data']['entity'];
    $wrapper = entity_metadata_wrapper('node', $entity);
    $replacements['[node:field_type]'] = $wrapper->NAME_OF_FIELD->value();
  }
}
roborew’s picture

I 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().

PhilY’s picture

Solution 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.

dunx’s picture

A typo correction for roborew's code has this working very nicely for pathauto. Thanks.

function MODULENAME_tokens_alter(array &$replacements, array $context) {
  $options = $context['options'];
  if($context['type'] == 'entity'
    && isset($options['pathauto'])
    && isset($replacements['[node:NAME_OF_FIELD]'])) {
    $entity = $context['data']['entity'];
    $wrapper = entity_metadata_wrapper('node', $entity);
    $replacements['[node:NAME_OF_FIELD]'] = $wrapper->NAME_OF_FIELD->value();
  }
}
silverham’s picture

Using the token: (where field_myfield is a taxonomy field), seems to work on Drupal 9
[node:field_myfield:entity:field_list:value]