Problem/Motivation
While writing the e-mail text I discovered that there's not a lot token to choose from if I add there a "Browse available tokens" link that way with a form alter hook to see what tokens are available, otherwise I have no idea what can I insert:
if (\Drupal::service('module_handler')->moduleExists('token')) {
$form['notifications']['tokens'] = [
'#theme' => 'token_tree_link',
'#token_types' => [
'booking_contact',
'bookable_calendar',
],
'#global_types' => TRUE,
'#click_insert' => TRUE,
'#show_restricted' => FALSE,
'#recursion_limit' => 3,
'#text' => t('Browse available tokens'),
'#weight' => -999,
];
}
I spent half an hour searching for and I found out that the "token.module only exposes tokens for base fields for entity types that do *not* define their own tokens, otherwise you would get conflicts and overlaps. So that is by-design." mentioned there:
https://www.drupal.org/project/token/issues/2794343#comment-12903340 β
So that means if in a hook_token_info() you define a type like this manually:
$info['types']['booking'] = [
'name' => t('Booking'),
'description' => t('Tokens associated with Bookings'),
];
then Drupal won't generate any tokens for that entity type. Was this by design or is it by accident? I'd advise removing these type definitions. Let there be tokens for all entity fields.
Also, there are multiple problems with several custom tokens used currently:
- there are more tokens defined in bookable_calendar_tokens()
, than in bookable_calendar_token_info()
. So missing definition in bookable_calendar_token_info()
for [bookable_calendar:description]
- there is no [booking:date]
since the fieldname is booking_date
- [booking_contact:party_size]
doesn't work because of a typo, "part_size" is defined instead without "y"
- the [booking:created]
returns a timestamp which is for humans in e-mail not really useful.
- since there are no Drupal-generated tokens, reference field tokens do not work right now. For example you cannot use [booking_contact:booking_instance:entity:date]
or [booking_contact:original:date]
or [booking_contact:booking:0:entity:created]
- if you remove the $info['types']
definitions, then the following custom token definitions won't be needed anymore because Drupal will generate the tokens for them based on fields:
[bookable_calendar:title]
[bookable_calendar:description]
[booking_contact:email]
[booking_contact:party_size]
[booking:date]
[booking:created]
Steps to reproduce
Proposed resolution
Remaining tasks
User interface changes
API changes
Data model changes