13.11.2025 | Nikolas Kopp, Pascal Crott
my_group.module
/**
* Implements hook_tokens().
*/
function example_group_tokens($type, $tokens, array $data, array $options, BubbleableMetadata $bubbleable_metadata) {
$replacements = [];
// Automatically expose related entities to group_relationships.
if ($type == 'group_relationship' && !empty($data[$type])) {
$token_service = \Drupal::token();
/** @var \Drupal\example\GroupRelationshipTypeServiceInterface $group_relationship_type_service */
$group_relationship_type_service = \Drupal::service('example.group_relationship_type_service');
$group_relationship = $data['group_relationship'];
assert($group_relationship instanceof GroupRelationshipInterface);
foreach ($tokens as $name => $original) {
/** @var \Drupal\Core\Entity\EntityTypeInterface $entity_type */
foreach ($group_relationship_type_service->getConfiguredEntityTypes() as $entity_type_id => $entity_type) {
if ($name == $entity_type_id) {
$entity = $group_relationship->getEntity();
$bubbleable_metadata->addCacheableDependency($entity);
$replacements[$original] = $entity->label();
}
// Actual chaining of tokens handled below.
if ($entity_tokens = $token_service->findWithPrefix($tokens, $entity_type_id)) {
$replacements += $token_service->generate($entity_type_id, $entity_tokens, [$entity_type_id => $group_relationship->getEntity()], $options, $bubbleable_metadata);
}
}
}
}
return $replacements;
}