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?