Submission Datetime Formattable

Created on 14 March 2024, about 1 year ago

Problem/Motivation

The submission date and time are just plain text (I think?). Can we get that in a more consumable format so that we can apply Drupal Date and Time Formats (and Smartdate formats) to it? Ultimately, it would be nice to take the submission information and plug it into a Fullcalendar view so that I can show users what slots are booked and what is still open.

Steps to reproduce

Proposed resolution

Remaining tasks

User interface changes

API changes

Data model changes

✨ Feature request
Status

Active

Version

1.0

Component

Code

Created by

πŸ‡ΊπŸ‡ΈUnited States davidandersonencsd

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

Merge Requests

Comments & Activities

  • 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
  • Merge request !8Add tokens for date, time and slots. β†’ (Merged) created by Unnamed author
  • πŸ‡¦πŸ‡Ή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.

  • πŸ‡΅πŸ‡Ή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.

Production build 0.71.5 2024