Score:0

Chained tokens for referenced custom entities

cn flag

We have two custom entities: parliament_period (can be an election or a legislature) and parliament. A parliament_period entity has a reference to a parliament entity. In a Poll node, we have an entity reference field for parliament_period entities whose bundle is legislature (field_legislature). (It is about polls the deputies take part of.) Part of the path alias for Poll nodes should be the content of a url_basis field from the parliament entities.

I used [node:field_legislature:entity:parliament:entity:url_basis]/[node:field_legislature:entity:identifier]/poll/[node:title] as Pathauto pattern. It used to work for some time, but it stopped working because of the [node:field_legislature:entity:parliament:entity:url_basis] token.

I am not sure what I have changed to make it stop working. In the hook_tokens() implementation I used the following code.

if ($type == 'parliament_period' && !empty($data['parliament_period'])) {
  /** @var \Drupal\pw_basic\Entity\ParliamentPeriod $parliamentPeriod */
  $parliamentPeriod = $data['parliament_period'];
  if ($parliament_tokens = $token_service->findWithPrefix($tokens, 'parliament')) {
    $replacements += $token_service->generate('parliament', $parliament_tokens, ['parliament' => $parliamentPeriod->getParliament()], $options, $bubbleable_metadata);
  }
}

This allows for chaining to replace any tokens related to a parliament entity when a parliament_period entity is given (such as in [node:field_legislature:entity]). hook_tokens() is correctly invoked for the parliament entity tokens, but the name of the token sent to hook_tokens() is entity:url_basis, not url_basis.

On the other hand, [node:field_legislature:entity:parliament:url_basis] is not allowed, without the entity part.

Should I remove the entity: part before I call $token_service->generate()for all the tokens identified, or is there a better way to chain tokens for custom entities?

apaderno avatar
us flag
*entity* should not be removed. It's the code handling the tokens for the *parliament* entity that needs to handle it, not the code handling tokens for the *parliament_period* entity. We cannot tell you why the code worked before and it's not working now, without seeing the code you used before and the code you use now. We would also need to see the full `hook_tokens()` code, not just a few lines.
apaderno avatar
us flag
Also, was the Token module or another module that implements tokens uninstalled before the code stopped to work, or installed?
cn flag
I'm wondering why you need a custom token for this at all? Assuming `parliament` is the name of an entity reference field on the `parliament_period` entity type, `[node:field_legislature:entity:parliament:entity:url_basis]` would already be available without any extra code
Tobias Krause avatar
cn flag
@Clive This assumption is correct: `parliament` here in this token is a field name in `parliament_period' entity which references a parliament entity. So you mean this should already work? It did work as I mentioned and I did not find any changes so I got confused a) why it worked and b) why now it does not work anymore. Can you tell me where the Token module handles this chaining?
4uk4 avatar
cn flag
token.tokens.inc > [`field_tokens()`](https://git.drupalcode.org/project/token/-/blob/8.x-1.x/token.tokens.inc#L1677) - "Implements hook_tokens() on behalf of field.module."
Tobias Krause avatar
cn flag
Thank you @4k4 - I actually found the place why the chaining is not working anymore. I opened an issue in the project page on drupal.org: https://www.drupal.org/project/token/issues/3228947
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.