You are here

public function Date::prepare in Webform 6.x

Same name and namespace in other branches
  1. 8.5 src/Plugin/WebformElement/Date.php \Drupal\webform\Plugin\WebformElement\Date::prepare()

Prepare an element to be rendered within a webform.

Parameters

array $element: An element.

\Drupal\webform\WebformSubmissionInterface $webform_submission: A webform submission. Webform submission is optional since it is not used by composite sub elements.

Overrides DateBase::prepare

See also

\Drupal\webform\Element\WebformCompositeBase::processWebformComposite

File

src/Plugin/WebformElement/Date.php, line 56

Class

Date
Provides a 'date' element.

Namespace

Drupal\webform\Plugin\WebformElement

Code

public function prepare(array &$element, WebformSubmissionInterface $webform_submission = NULL) {

  // Unset unsupported date format for date elements that are not using a
  // datepicker.
  if (empty($element['#datepicker'])) {
    unset($element['#date_date_format']);
  }

  // Set default date format to HTML date.
  if (!isset($element['#date_date_format'])) {
    $element['#date_date_format'] = $this
      ->getDefaultProperty('date_date_format');
  }

  // Set placeholder attribute.
  if (!empty($element['#placeholder'])) {
    $element['#attributes']['placeholder'] = $element['#placeholder'];
  }

  // Prepare element after date format has been updated.
  parent::prepare($element, $webform_submission);

  // Set the (input) type attribute to 'date'.
  // @see \Drupal\Core\Render\Element\Date::getInfo
  $element['#attributes']['type'] = 'date';

  // Convert date element into textfield with date picker.
  if ($this
    ->datePickerExists() && !empty($element['#datepicker'])) {
    $element['#attributes']['type'] = 'text';

    // Must manually set 'data-drupal-date-format' to trigger date picker.
    // @see \Drupal\Core\Render\Element\Date::processDate
    $element['#attributes']['data-drupal-date-format'] = [
      $element['#date_date_format'],
    ];
  }
}