Enable to use native entity field tokens for the 5 custom booking entity

Created on 18 August 2023, 11 months ago
Updated 25 August 2023, 10 months ago

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

πŸ› Bug report
Status

Needs review

Version

2.2

Component

Code

Created by

πŸ‡ΈπŸ‡°Slovakia kaszarobert

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @kaszarobert
  • @kaszarobert opened merge request.
  • Status changed to Needs review 11 months ago
  • πŸ‡ΈπŸ‡°Slovakia kaszarobert

    I opened a MR with the fixes I wrote about in the issue summary. Please review them.

  • πŸ‡ΊπŸ‡ΈUnited States josh.fabean

    Thanks. I'm looking into that. I remember we created tokens, so things would work, didn't realize that breaks default entity tokens. Depending on if this changes or breaks existing tokens, might need to up version numbers or something.

Production build 0.69.0 2024