
The token module is one of these essential modules on any Drupal 8 project. It allows you to use tokens in certain input fields, whether configuration or content, to target the value of one entity field. Many modules use it to allow users or site builder to provide dynamic value without the need for coding.
Let's see how to access the content's values from these tokens, but also to the values indirectly associated with these contents, from Entity reference fields.
Access fields directly attached to an entity
Recovering the value of a content's field is done simply. For example, to retrieve the title of a content, we will use the token [node:title]. Similarly to retrieve the value of a particular field of content (so a node in Drupal terminology), eg field_name, we can use [node:field_name]. Generally, when you have the ability to use tokens, you have a list of tokens available in the context you are in (a node, a user, etc.).
And you have all the time to browse the list of available tokens and select those that correspond to your need.
So far, so good !
Access to fields of entities referenced by an entity
But we may also need to access, always with tokens, to field values that are no longer attached directly to the entity that is the reference context, but which are attached to another entity referenced by our content, or even why not, to an entity that is referenced by another entity itself referenced by our content.
Are you still following?
For example, we may have content (a node), which reference an another content, the latter content referencing a taxonomy term, and we wish to retrieve the name of that taxonomy term.
Or another (deeper) example: a content that references another content, the latter content references some paragraphs (paragraph entity), paragraphs to which an image is attached (the paragraph therefore references a File entity that stores the image) . And we want to retrieve the image's url, from the initial content, and this always with a simple token.
The Drupal 8 token module allows us to achieve our ends by using tokens chained to each other (previously, on Drupal 7, we needed the entity_token module to achieve the same results, module included with the entity module). But sometimes these chained tokens are not all present in the list of tokens available, especially when one reaches 3 levels of depth or more. At the same time, the possibilities of presenting chained tokens up to three (or more) levels of depth become so numerous, that this list would become too long, disrupting the selection of the main tokens.
I confess to having looked for a certain time the syntax to use. But after a few readings, and deductions, bingo, its obviousness is obvious (oups!). I therefore share the result here.
The key here is to use a special token, entity, which will allow us to chain the entities referenced one to the other from our token and to traverse them as for a small Sunday ballad. And because a drawing is better than a long speech, let us take a few examples.
- We have a content (A) that references another content (B) with an Entity Reference field field_ref_content, and the content B contains an image, image referenced from field field_image. If we want to have the url of this image from the A content, we just need to use the following token: [node:field_ref_content:entity:field_image:entity:url]
- We have a content (A) that references (field_ref_content) another content (B), the latter content references a paragraph (field_paragraph), paragraph to which an image is attached (the paragraph therefore references the image from field field_image). And we want to retrieve the image's url, from the content A. Our token to be used is then: [node:field_ref_content:entity:field_paragraph:entity:field_image:entity:url].
- In the case of multiple fields it is of course necessary to target which entity to be accessed using its delta (or its index). For example, a content (A) that references paragraphs (multiple field field_paragraphs), paragraphs that have an image. To access the url of this image for the first paragraph (delta 0) we will then use the token: [node:field_paragraphs:0:entity:field_image:entity:url]
Finally, it is rather simple, no need here of a Drupal 8 expert. To access the values of a referenced entity, it is therefore enough to use the token entity and to associate it with the Entity Reference field used, and then we access the entirety of this entity and therefore its fields, and this in a recursive way, to infinity if needed.
Commentaires
Thank you for easy to…
Thank you for easy to understand post! I was looking all day on how to do something like this
Very helpful
This is very helpful information. Thank you for documenting this feature.
It would be great if you could add this information to the d.o documentation pages (https://www.drupal.org/documentation/modules/token). More people could find this valuable information.
Thanks. Will do.
Thanks. Will do.
This works really while…
This works really while theming too
https://www.drupal.org/project/twig_tweak
Do chained tokens work with Webform in Drupal 8?
Hi!
Thanks for the write up! I thought that this should work for what I am doing. In my Webform the user can select an entity (via entity reference). The referenced entities have a field with an email address. This email address should be the "To:" email address that the webform submission gets sent too.
Here is the Webform token info for submission values:
[webform_submission:values:?]
Webform tokens from submitted data. Replace the '?' with the 'element_key', 'element_key:format' or 'element_key:format:items'. The 'format' can be 'value', 'raw', or custom format specifically associated with the element The 'items' can be 'comma', 'semicolon', 'and', 'ol', 'ul', or custom delimiter For example, to display the Contact webform's 'Subject' element's value you would use the [webform_submission:values:subject] token.
My email field on the reference entities has a machine name of: field_email, so I figure this would be the token:
[webform_submission:values:contact_reason_or_person:entity:field_email]
Web form displays the following in the Webform email To field:
To: [contact_reason_or_person]
I have tried a number of :entity chained tokens and all of them give the same results. I tried the token:
[webform_submission:values:contact_reason_or_person:entity:title]
which I figured all entities should have but it too showed up as:
To: [contact_reason_or_person]
Thanks again for the blog post and in advance for any assistance you can offer!
Frederick
Hi,…
Hi,
If your entity reference field on your webform can reference multiple entities, then you have to use a delta in the chained token.
For example [webform_submission:values:contact_reason_or_person:0:entity:field_email]
Looks like this is not currently working in Webform
I tried your suggestion, but it does not work. It appears that chained tokens are not working with the Webform module yet. I say yet because they are currently working on (https://www.drupal.org/node/2873307) tokens for sub-elements, when you have a composite field with several sub-fields in it. I have filed an (https://www.drupal.org/node/2877360) issue regarding chained tokens.
If you search a workaround…
If you search a workaround immediatly available, pending that webform supports it, you can always alter the email before it is sent, and fetch the entity referenced and the email field value, and set it as the recipient.
What about field collections…
What about field collections? If I am on 8.2 and cannot use paragraphs, I have to use field collections. I have not found a way to chain the tokens to access a field in the collection. Ideas?
I doubt you can use chained…
I doubt you can use chained token on field collections, as field collection are not an entity but only fields. You should certainly write your own token.
Delta
Wondering if it's possible to get the delta this way? So you could display the weight value of each paragraph item in a list.
Good Work Here
Thanks, and here's a very good tutorial on creating a custom Drupal token module at: https://www.cloudways.com/blog/create-custom-drupal-token-modules/ I found it useful, I hope other will like it too. Thanks
Ajouter un commentaire