function template_preprocess_datetime_form

Same name in other branches
  1. 9 core/includes/theme.inc \template_preprocess_datetime_form()
  2. 8.9.x core/includes/theme.inc \template_preprocess_datetime_form()
  3. 10 core/includes/theme.inc \template_preprocess_datetime_form()

Prepares variables for datetime form element templates.

The datetime form element serves as a wrapper around the date element type, which creates a date and a time component for a date.

Default template: datetime-form.html.twig.

Parameters

array $variables: An associative array containing:

  • element: An associative array containing the properties of the element. Properties used: #title, #value, #options, #description, #required, #attributes.

See also

form_process_datetime()

File

core/includes/theme.inc, line 374

Code

function template_preprocess_datetime_form(&$variables) {
    $element = $variables['element'];
    $variables['attributes'] = [];
    if (isset($element['#id'])) {
        $variables['attributes']['id'] = $element['#id'];
    }
    if (!empty($element['#attributes']['class'])) {
        $variables['attributes']['class'] = (array) $element['#attributes']['class'];
    }
    $variables['content'] = $element;
    // Add instructional text describing the required date and time formats.
    // This will be hidden on JavaScript-enabled browsers that have a native
    // datepicker.
    if (!empty($element['#date_date_format'])) {
        $placeholder_date = 'YYYY-MM-DD';
        $variables['attributes']['data-drupal-field-elements'] = 'date' . ($element['#date_time_element'] === 'none' ? '' : '-time');
        $date_format = is_array($element['#date_date_format']) ? $element['#date_date_format'][0] : date($element['#date_date_format']);
        $variables['content']['date']['#attributes']['placeholder'] = $placeholder_date;
        $help_date = t('Enter the date using the format @placeholder_date (e.g., @date_format).', [
            '@date_format' => $date_format,
            '@placeholder_date' => $placeholder_date,
        ]);
        $variables['content']['date']['#attributes']['data-help'] = $help_date;
        // Include time formatting instructions when a time input is present.
        if (!empty($element['#date_time_format'])) {
            $placeholder_time = 'hh:mm:ss';
            $time_format = is_array($element['#date_time_format']) ? $element['#date_time_format'][0] : date($element['#date_time_format']);
            $variables['content']['time']['#attributes']['placeholder'] = $placeholder_time;
            $help_time = t('Enter the time using the format @placeholder_time (e.g., @time_format).', [
                '@time_format' => $time_format,
                '@placeholder_time' => $placeholder_time,
            ]);
            $variables['content']['time']['#attributes']['data-help'] = $help_time;
        }
    }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.