You are here

public function DateBase::prepare in Webform 6.x

Same name and namespace in other branches
  1. 8.5 src/Plugin/WebformElement/DateBase.php \Drupal\webform\Plugin\WebformElement\DateBase::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 WebformElementBase::prepare

See also

\Drupal\webform\Element\WebformCompositeBase::processWebformComposite

3 calls to DateBase::prepare()
Date::prepare in src/Plugin/WebformElement/Date.php
Prepare an element to be rendered within a webform.
DateList::prepare in src/Plugin/WebformElement/DateList.php
Prepare an element to be rendered within a webform.
DateTime::prepare in src/Plugin/WebformElement/DateTime.php
Prepare an element to be rendered within a webform.
3 methods override DateBase::prepare()
Date::prepare in src/Plugin/WebformElement/Date.php
Prepare an element to be rendered within a webform.
DateList::prepare in src/Plugin/WebformElement/DateList.php
Prepare an element to be rendered within a webform.
DateTime::prepare in src/Plugin/WebformElement/DateTime.php
Prepare an element to be rendered within a webform.

File

src/Plugin/WebformElement/DateBase.php, line 62

Class

DateBase
Provides a base 'date' class.

Namespace

Drupal\webform\Plugin\WebformElement

Code

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

  // Don't used 'datetime_wrapper', instead use 'form_element' wrapper.
  // Note: Below code must be executed before parent::prepare().
  // @see \Drupal\Core\Datetime\Element\Datelist
  // @see \Drupal\webform\Plugin\WebformElement\DateTime
  $element['#theme_wrappers'] = [
    'form_element',
  ];

  // Must manually process #states.
  // @see \Drupal\Core\Form\FormHelper::processStates
  if (!empty($element['#states'])) {
    $element['#attached']['library'][] = 'core/drupal.states';
    $element['#wrapper_attributes']['data-drupal-states'] = Json::encode($element['#states']);
  }
  parent::prepare($element, $webform_submission);

  // Parse #default_value date input format.
  $this
    ->parseInputFormat($element, '#default_value');

  // If date picker does not exist, remove date picker related properties.
  if (!$this
    ->datePickerExists()) {
    unset($element['#datepicker'], $element['#datepicker_button'], $element['#date_date_format'], $element['#date_date_datepicker_button']);
  }

  // Set date min/max attributes.
  // This overrides extra attributes set via Datetime::processDatetime.
  // @see \Drupal\Core\Datetime\Element\Datetime::processDatetime
  if (isset($element['#date_date_format'])) {
    $date_min = $this
      ->getElementProperty($element, 'date_date_min') ?: $this
      ->getElementProperty($element, 'date_min');
    if ($date_min) {
      $element['#attributes']['min'] = static::formatDate($element['#date_date_format'], strtotime($date_min));
      $element['#attributes']['data-min-year'] = static::formatDate('Y', strtotime($date_min));
    }
    $date_max = $this
      ->getElementProperty($element, 'date_date_max') ?: $this
      ->getElementProperty($element, 'date_max');
    if (!empty($date_max)) {
      $element['#attributes']['max'] = static::formatDate($element['#date_date_format'], strtotime($date_max));
      $element['#attributes']['data-max-year'] = static::formatDate('Y', strtotime($date_max));
    }
  }

  // Set date days (of week) attributes.
  if (!empty($element['#date_days'])) {
    $element['#attributes']['data-days'] = implode(',', $element['#date_days']);
  }

  // Display datepicker button.
  if (!empty($element['#datepicker_button']) || !empty($element['#date_date_datepicker_button'])) {
    $element['#attributes']['data-datepicker-button'] = TRUE;
    $element['#attached']['drupalSettings']['webform']['datePicker']['buttonImage'] = base_path() . drupal_get_path('module', 'webform') . '/images/elements/date-calendar.png';
  }

  // Set first day according to admin/config/regional/settings.
  $config = $this->configFactory
    ->get('system.date');
  $element['#attached']['drupalSettings']['webform']['dateFirstDay'] = $config
    ->get('first_day');
  $cacheability = CacheableMetadata::createFromObject($config);
  $cacheability
    ->applyTo($element);
  if ($this
    ->datePickerExists()) {
    $element['#attached']['library'][] = 'webform/webform.element.date';
  }
  $element['#after_build'][] = [
    get_class($this),
    'afterBuild',
  ];
}