- Issue created by @davidandersonencsd
- π΅πΉPortugal rfmarcelino
Thank you @DavidAndersonENCSD for your feedback.
Yes, you're right. Right now we're only storing a string.
It may make sense to change it to a DateTime field, but it's also less flexible and would also constrain what we can do in the future (allow multiple slots or a range of slots, etc.)
We'll also need to consider the impact on projects already using the module. Eventually, there needs to be a '2.x'. - π¦πΉAustria electric.larry Vienna
For a client's application form, I needed to display the selected time slot in a user-friendly date and time format. By default, using the webform_booking token on the confirmation page or in emails shows a raw value like 2024-12-31 13:37|1. However, the client wanted a format like Montag, 17. Februar 2025 - 09:20 Uhr.
To achieve this, I used hook_tokens_alter() to extract and format the date and time from the webform_booking field, replacing the token with the formatted value.
In this example, the webform_booking field key is terminreservierung. I placed the token [webform_submission:values:terminreservierung] on the confirmation page and in confirmation emails.
function mymodule_tokens_alter(array &$replacements, array $context, BubbleableMetadata $bubbleable_metadata) { if (isset($context['type'], $context['data']['webform_submission']) && $context['type'] === 'webform_submission') { // Replace reservation field token with formatted date. if (!empty($context['tokens']['values:terminreservierung'])) { $submission = $context['data']['webform_submission']; $appointment = $submission->getElementData('terminreservierung'); // Extract Date and time from submission info. // Submission field holds date, time and number of slots. "2025-02-17 09:20|1". $slot_data = explode('|', $appointment); $slot_start_time = new \DateTime($slot_data[0]); // Format date using the "long" date format. $value = \Drupal::service('date.formatter')->format( $slot_start_time->getTimestamp(), 'long' ); $replacements[$context['tokens']['values:terminreservierung']] = $value; } } }
- π¦πΉAustria electric.larry Vienna
Here's a patch, adding the features from webform_booking-3427876-submission-datetime-formattable.
This adds placeholder variables (tokens) for dynamically personalizing confirmation pages and email notifications.
Available tokens:
[webform_submission:booking:?]
To output individual elements, replace the '?' with the booking form element's key.[webform_submission:booking:element_key]
Default value. Displays the raw booking date and time ('Y-m-d H:i').[webform_submission:booking:element_key:date]
Displays the booking date formatted using the default html_date format ('Y-m-d').[webform_submission:booking:element_key:date:*]
Replace the '*' with the machine name of a custom format defined in Date and Time Formats (/admin/config/regional/date-time) to display the booking date in a specific format. E. g. [webform_submission:booking:element_key:date:long][webform_submission:booking:element_key:time]
Displays the booking time formatted as 'H:i'.[webform_submission:booking:element_key:slots]
Displays the number of slots booked. -
rfmarcelino β
committed 8a614c63 on 1.1.x
#3427876 - Submission Datetime Formattable
-
rfmarcelino β
committed 8a614c63 on 1.1.x
- π΅πΉPortugal rfmarcelino
Thank you @electric.larry for you contribution.
I'm not sure if this is what @davidandersonencsd had in mind but a very nice improvement to the module.