You are here

abstract class DateBase in Webform 8.5

Same name and namespace in other branches
  1. 6.x src/Plugin/WebformElement/DateBase.php \Drupal\webform\Plugin\WebformElement\DateBase

Provides a base 'date' class.

Hierarchy

Expanded class hierarchy of DateBase

1 file declares its use of DateBase
WebformDevelSchema.php in modules/webform_devel/src/WebformDevelSchema.php

File

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

Namespace

Drupal\webform\Plugin\WebformElement
View source
abstract class DateBase extends WebformElementBase {

  /**
   * {@inheritdoc}
   */
  protected function defineDefaultProperties() {
    return [
      // Form validation.
      'date_date_min' => '',
      'date_date_max' => '',
      'date_days' => [
        '0',
        '1',
        '2',
        '3',
        '4',
        '5',
        '6',
      ],
    ] + parent::defineDefaultProperties() + $this
      ->defineDefaultMultipleProperties();
  }

  /****************************************************************************/

  // Element rendering methods.

  /****************************************************************************/

  /**
   * {@inheritdoc}
   */
  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');

    // 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);
    $element['#attached']['library'][] = 'webform/webform.element.date';
    $element['#after_build'][] = [
      get_class($this),
      'afterBuild',
    ];
  }

  /**
   * {@inheritdoc}
   */
  protected function prepareElementValidateCallbacks(array &$element, WebformSubmissionInterface $webform_submission = NULL) {
    $element['#element_validate'] = array_merge([
      [
        get_class($this),
        'preValidateDate',
      ],
    ], $element['#element_validate']);
    $element['#element_validate'][] = [
      get_class($this),
      'validateDate',
    ];
    parent::prepareElementValidateCallbacks($element, $webform_submission);
  }

  /**
   * {@inheritdoc}
   */
  public function setDefaultValue(array &$element) {
    if ($this
      ->hasMultipleValues($element)) {
      $element['#default_value'] = isset($element['#default_value']) ? (array) $element['#default_value'] : NULL;
      return;
    }

    // Datelist and Datetime require #default_value to be DrupalDateTime.
    if (in_array($element['#type'], [
      'datelist',
      'datetime',
    ])) {
      if (!empty($element['#default_value']) && is_string($element['#default_value'])) {
        $element['#default_value'] = $element['#default_value'] ? DrupalDateTime::createFromTimestamp(strtotime($element['#default_value'])) : NULL;
      }
    }
  }

  /**
   * After build handler for date elements.
   */
  public static function afterBuild(array $element, FormStateInterface $form_state) {

    // Add parent title to sub-elements to child elements which applies to
    // datetime and datelist elements.
    $child_keys = Element::children($element);
    foreach ($child_keys as $child_key) {
      if (isset($element[$child_key]['#title'])) {
        $t_args = [
          '@parent' => $element['#title'],
          '@child' => $element[$child_key]['#title'],
        ];
        $element[$child_key]['#title'] = t('@parent: @child', $t_args);
      }
    }

    // Remove orphaned form label.
    if ($child_keys) {
      $element['#label_attributes']['webform-remove-for-attribute'] = TRUE;
    }
    return $element;
  }

  /****************************************************************************/

  // Display submission value methods.

  /****************************************************************************/

  /**
   * {@inheritdoc}
   */
  protected function formatTextItem(array $element, WebformSubmissionInterface $webform_submission, array $options = []) {
    $value = $this
      ->getValue($element, $webform_submission, $options);
    $timestamp = strtotime($value);
    if (empty($timestamp)) {
      return $value;
    }
    $format = $this
      ->getItemFormat($element);
    if ($format === 'raw') {
      return $value;
    }
    elseif (DateFormat::load($format)) {
      return \Drupal::service('date.formatter')
        ->format($timestamp, $format);
    }
    else {
      return static::formatDate($format, $timestamp);
    }
  }

  /**
   * {@inheritdoc}
   */
  public function getItemDefaultFormat() {
    return 'fallback';
  }

  /**
   * {@inheritdoc}
   */
  public function getItemFormats() {
    $formats = parent::getItemFormats();
    $date_formats = DateFormat::loadMultiple();
    foreach ($date_formats as $date_format) {
      $formats[$date_format
        ->id()] = $date_format
        ->label();
    }

    // If a default format is defined update the fallback date formats label.
    // @see \Drupal\webform\Plugin\WebformElementBase::getItemFormat
    $default_format = $this->configFactory
      ->get('webform.settings')
      ->get('format.' . $this
      ->getPluginId() . '.item');
    if ($default_format && isset($date_formats[$default_format])) {
      $formats['fallback'] = $this
        ->t('Default date format (@label)', [
        '@label' => $date_formats[$default_format]
          ->label(),
      ]);
    }
    return $formats;
  }

  /****************************************************************************/

  // Export methods.

  /****************************************************************************/

  /**
   * {@inheritdoc}
   */
  public function buildExportRecord(array $element, WebformSubmissionInterface $webform_submission, array $export_options) {
    $element['#format'] = $this
      ->getDateType($element) === 'datetime' ? 'Y-m-d H:i:s' : 'Y-m-d';
    return [
      $this
        ->formatText($element, $webform_submission, $export_options),
    ];
  }

  /****************************************************************************/

  // Element configuration methods.

  /****************************************************************************/

  /**
   * {@inheritdoc}
   */
  public function form(array $form, FormStateInterface $form_state) {
    $form = parent::form($form, $form_state);

    // Append supported date input format to #default_value description.
    $form['default']['default_value']['#description'] .= '<br /><br />' . $this
      ->t('Accepts any date in any <a href="https://www.gnu.org/software/tar/manual/html_chapter/tar_7.html#Date-input-formats">GNU Date Input Format</a>. Strings such as today, +2 months, and Dec 9 2004 are all valid.');

    // Append token date format to #default_value description.
    $form['default']['default_value']['#description'] .= '<br /><br />' . $this
      ->t("You may use tokens. Tokens should use the 'html_date' or 'html_datetime' date format. (i.e. @date_format)", [
      '@date_format' => '[current-user:field_date_of_birth:date:html_date]',
    ]);

    // Allow custom date formats to be entered.
    $form['display']['format']['#type'] = 'webform_select_other';
    $form['display']['format']['#other__option_label'] = $this
      ->t('Custom date format…');
    $form['display']['format']['#other__description'] = $this
      ->t('A user-defined date format. See the <a href="http://php.net/manual/function.date.php">PHP manual</a> for available options.');
    $form['date'] = [
      '#type' => 'fieldset',
      '#title' => $this
        ->t('Date settings'),
    ];

    // Date min/max validation.
    $form['date']['date_container'] = $this
      ->getFormInlineContainer() + [
      '#weight' => 10,
    ];
    $form['date']['date_container']['date_date_min'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Date minimum'),
      '#description' => $this
        ->t('Specifies the minimum date.') . ' ' . $this
        ->t('To limit the minimum date to the submission date use the <code>[webform_submission:created:html_date]</code> token.') . '<br /><br />' . $this
        ->t('Accepts any date in any <a href="https://www.gnu.org/software/tar/manual/html_chapter/tar_7.html#Date-input-formats">GNU Date Input Format</a>. Strings such as today, +2 months, and Dec 9 2004 are all valid.'),
    ];
    $form['date']['date_container']['date_date_max'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Date maximum'),
      '#description' => $this
        ->t('Specifies the maximum date.') . ' ' . $this
        ->t('To limit the maximum date to the submission date use the <code>[webform_submission:created:html_date]</code> token.') . '<br /><br />' . $this
        ->t('Accepts any date in any <a href="https://www.gnu.org/software/tar/manual/html_chapter/tar_7.html#Date-input-formats">GNU Date Input Format</a>. Strings such as today, +2 months, and Dec 9 2004 are all valid.'),
    ];

    // Date days of the week validation.
    $form['date']['date_days'] = [
      '#type' => 'checkboxes',
      '#title' => $this
        ->t('Date days of the week'),
      '#options' => DateHelper::weekDaysAbbr(TRUE),
      '#element_validate' => [
        [
          '\\Drupal\\webform\\Utility\\WebformElementHelper',
          'filterValues',
        ],
      ],
      '#description' => $this
        ->t('Specifies the day(s) of the week. Please note, the date picker will disable unchecked days of the week.'),
      '#options_display' => 'side_by_side',
      '#required' => TRUE,
      '#weight' => 20,
    ];

    // Date/time min/max validation.
    if ($this
      ->hasProperty('date_date_min') && $this
      ->hasProperty('date_time_min') && $this
      ->hasProperty('date_min')) {
      $form['validation']['date_min_max_message'] = [
        '#type' => 'webform_message',
        '#message_type' => 'warning',
        '#access' => TRUE,
        '#message_message' => $this
          ->t("'Date/time' minimum or maximum should not be used with 'Date' or 'Time' specific minimum or maximum.") . '<br/>' . '<strong>' . $this
          ->t('This can cause unexpected validation errors.') . '</strong>',
        '#message_close' => TRUE,
        '#message_storage' => WebformMessageElement::STORAGE_SESSION,
        '#states' => [
          'visible' => [
            [
              ':input[name="properties[date_date_min]"]' => [
                'filled' => TRUE,
              ],
            ],
            [
              ':input[name="properties[date_date_max]"]' => [
                'filled' => TRUE,
              ],
            ],
            [
              ':input[name="properties[date_time_min]"]' => [
                'filled' => TRUE,
              ],
            ],
            [
              ':input[name="properties[date_time_max]"]' => [
                'filled' => TRUE,
              ],
            ],
          ],
        ],
      ];
    }
    $form['validation']['date_container'] = $this
      ->getFormInlineContainer();
    $form['validation']['date_container']['date_min'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Date/time minimum'),
      '#description' => $this
        ->t('Specifies the minimum date/time.') . ' ' . $this
        ->t('To limit the minimum date/time to the submission date/time use the <code>[webform_submission:created:html_datetime]</code> token.') . '<br /><br />' . $this
        ->t('Accepts any date in any <a href="https://www.gnu.org/software/tar/manual/html_chapter/tar_7.html#Date-input-formats">GNU Date/Time Input Format</a>. Strings such as today, +2 months, and Dec 9 2004 10:00 PM are all valid.'),
    ];
    $form['validation']['date_container']['date_max'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Date/time maximum'),
      '#description' => $this
        ->t('Specifies the maximum date/time.') . ' ' . $this
        ->t('To limit the maximum date/time to the submission date/time use the <code>[webform_submission:created:html_datetime]</code> token.') . '<br /><br />' . $this
        ->t('Accepts any date in any <a href="https://www.gnu.org/software/tar/manual/html_chapter/tar_7.html#Date-input-formats">GNU Date/Time Input Format</a>. Strings such as today, +2 months, and Dec 9 2004 10:00 PM are all valid.'),
    ];
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {
    $properties = $this
      ->getConfigurationFormProperties($form, $form_state);

    // Validate #default_value GNU Date Input Format.
    if (!$this
      ->validateGnuDateInputFormat($properties, '#default_value')) {
      $this
        ->setGnuDateInputFormatError($form['properties']['default']['default_value'], $form_state);
    }

    // Validate #*_min and #*_max GNU Date Input Format.
    if (!$this
      ->validateGnuDateInputFormat($properties, '#date_min')) {
      $this
        ->setGnuDateInputFormatError($form['properties']['validation']['date_min'], $form_state);
    }
    if (!$this
      ->validateGnuDateInputFormat($properties, '#date_max')) {
      $this
        ->setGnuDateInputFormatError($form['properties']['validation']['date_max'], $form_state);
    }
    if (!$this
      ->validateGnuDateInputFormat($properties, '#date_date_min')) {
      $this
        ->setGnuDateInputFormatError($form['properties']['date']['date_date_min'], $form_state);
    }
    if (!$this
      ->validateGnuDateInputFormat($properties, '#date_date_max')) {
      $this
        ->setGnuDateInputFormatError($form['properties']['date']['date_date_max'], $form_state);
    }
    parent::validateConfigurationForm($form, $form_state);
  }

  /**
   * Get the type of date/time element.
   *
   * @param array $element
   *   An element.
   *
   * @return string
   *   The type of date/time element which be either a 'date' or 'datetime'.
   */
  protected function getDateType(array $element) {
    switch ($element['#type']) {
      case 'datelist':
        return isset($element['#date_part_order']) && !in_array('hour', $element['#date_part_order']) ? 'date' : 'datetime';
      case 'datetime':
        return 'datetime';
      case 'date':
      default:
        return 'date';
    }
  }

  /**
   * Parse GNU Date Input Format.
   *
   * @param array $element
   *   An element.
   * @param string $property
   *   The element's date property.
   */
  protected function parseInputFormat(array &$element, $property) {
    if (!isset($element[$property])) {
      return;
    }
    elseif (is_array($element[$property])) {
      foreach ($element[$property] as $key => $value) {
        $timestamp = strtotime($value);
        $element[$property][$key] = $timestamp ? \Drupal::service('date.formatter')
          ->format($timestamp, 'html_' . $this
          ->getDateType($element)) : NULL;
      }
    }
    else {
      $timestamp = strtotime($element[$property]);
      $element[$property] = $timestamp ? \Drupal::service('date.formatter')
        ->format($timestamp, 'html_' . $this
        ->getDateType($element)) : NULL;
    }
  }

  /****************************************************************************/

  // Validation methods.

  /****************************************************************************/

  /**
   * Validate GNU date input format.
   *
   * @param array $properties
   *   An array of element properties.
   * @param string $key
   *   The property name containing the GNU date input format.
   *
   * @return bool
   *   TRUE if property's value is a valid GNU date input format or contains
   *   a token.
   */
  protected function validateGnuDateInputFormat(array $properties, $key) {
    if (empty($properties[$key])) {
      return TRUE;
    }
    $values = (array) $properties[$key];
    foreach ($values as $value) {
      if (!preg_match('/^\\[[^]]+\\]$/', $value)) {
        if (strtotime($value) === FALSE) {
          return FALSE;
        }
      }
    }
    return TRUE;
  }

  /**
   * Set GNU date input format error.
   *
   * @param array $element
   *   The property element.
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   The current state of the form.
   */
  protected function setGnuDateInputFormatError(array $element, FormStateInterface $form_state) {
    $t_args = [
      '@title' => $element['#title'] ?: $element['#key'],
    ];
    $form_state
      ->setError($element, $this
      ->t('The @title could not be interpreted in <a href="https://www.gnu.org/software/tar/manual/html_chapter/tar_7.html#Date-input-formats">GNU Date Input Format</a>.', $t_args));
  }

  /**
   * Webform element pre validation handler for Date elements.
   */
  public static function preValidateDate(&$element, FormStateInterface $form_state, &$complete_form) {

    // ISSUE #2723159:
    // Datetime form element cannot validate when using a
    // format without seconds.
    // WORKAROUND:
    // Append the second format before the time element is validated.
    //
    // @see \Drupal\Core\Datetime\Element\Datetime::valueCallback
    // @see https://www.drupal.org/node/2723159
    if ($element['#type'] === 'datetime' && $element['#date_time_format'] === 'H:i' && strlen($element['#value']['time']) === 8) {
      $element['#date_time_format'] = 'H:i:s';
    }

    // ISSUE:
    // Date list in composite element is missing the date object.
    //
    // WORKAROUND:
    // Manually set the date object.
    $date_element_types = [
      'datelist' => '\\Drupal\\Core\\Datetime\\Element\\Datelist',
      'datetime' => '\\Drupal\\Core\\Datetime\\Element\\Datetime',
    ];
    if (isset($date_element_types[$element['#type']])) {
      $date_class = $date_element_types[$element['#type']];
      $input_exists = FALSE;
      $input = NestedArray::getValue($form_state
        ->getValues(), $element['#parents'], $input_exists);
      if (!isset($input['object'])) {

        // Time picker converts all submitted time values to H:i:s format.
        // @see \Drupal\webform\Element\WebformTime::validateWebformTime
        if (isset($element['#date_time_element']) && $element['#date_time_element'] === 'timepicker') {
          $element['#date_time_format'] = 'H:i:s';
        }
        $input = $date_class::valueCallback($element, $input, $form_state);
        $form_state
          ->setValueForElement($element, $input);
        $element['#value'] = $input;
      }
    }
  }

  /**
   * Webform element validation handler for date elements.
   *
   * Note that #required is validated by _form_validate() already.
   *
   * @see \Drupal\Core\Render\Element\Number::validateNumber
   */
  public static function validateDate(&$element, FormStateInterface $form_state, &$complete_form) {
    $value = $element['#value'];
    $name = empty($element['#title']) ? $element['#parents'][0] : $element['#title'];
    $date_date_format = !empty($element['#date_date_format']) ? $element['#date_date_format'] : DateFormat::load('html_date')
      ->getPattern();
    $date_time_format = !empty($element['#date_time_format']) ? $element['#date_time_format'] : DateFormat::load('html_time')
      ->getPattern();

    // Convert DrupalDateTime array and object to ISO datetime.
    if (is_array($value)) {
      $value = $value['object'] ? $value['object']
        ->format(DateFormat::load('html_datetime')
        ->getPattern()) : '';
    }
    elseif ($value) {
      $datetime = WebformDateHelper::createFromFormat($date_date_format, $value);
      if ($datetime === FALSE || static::formatDate($date_date_format, $datetime
        ->getTimestamp()) !== $value) {
        $form_state
          ->setError($element, t('%name must be a valid date.', [
          '%name' => $name,
        ]));
        $value = '';
      }
      else {

        // Clear timestamp to date elements.
        if ($element['#type'] === 'date') {
          $datetime
            ->setTime(0, 0, 0);
          $value = $datetime
            ->format(DateFormat::load('html_date')
            ->getPattern());
        }
        else {
          $value = $datetime
            ->format(DateFormat::load('html_datetime')
            ->getPattern());
        }
      }
    }
    $form_state
      ->setValueForElement($element, $value);
    if ($value === '') {
      return;
    }
    $time = strtotime($value);

    // Ensure that the input is greater than the #date_date_min property, if set.
    if (isset($element['#date_date_min'])) {
      $min = strtotime(static::formatDate('Y-m-d', strtotime($element['#date_date_min'])));
      if ($time < $min) {
        $form_state
          ->setError($element, t('%name must be on or after %min.', [
          '%name' => $name,
          '%min' => static::formatDate($date_date_format, $min),
        ]));
      }
    }

    // Ensure that the input is less than the #date_date_max property, if set.
    if (isset($element['#date_date_max'])) {
      $max = strtotime(static::formatDate('Y-m-d 23:59:59', strtotime($element['#date_date_max'])));
      if ($time > $max) {
        $form_state
          ->setError($element, t('%name must be on or before %max.', [
          '%name' => $name,
          '%max' => static::formatDate($date_date_format, $max),
        ]));
      }
    }

    // Ensure that the input is greater than the #date_min property, if set.
    if (isset($element['#date_min'])) {
      $min = strtotime($element['#date_min']);
      if ($time < $min) {
        $form_state
          ->setError($element, t('%name must be on or after %min.', [
          '%name' => $name,
          '%min' => static::formatDate($date_date_format, $min) . ' ' . static::formatDate($date_time_format, $min),
        ]));
      }
    }

    // Ensure that the input is less than the #date_max property, if set.
    if (isset($element['#date_max'])) {
      $max = strtotime($element['#date_max']);
      if ($time > $max) {
        $form_state
          ->setError($element, t('%name must be on or before %max.', [
          '%name' => $name,
          '%max' => static::formatDate($date_date_format, $max) . ' ' . static::formatDate($date_time_format, $max),
        ]));
      }
    }

    // Ensure that the input is a day of week.
    if (!empty($element['#date_days'])) {
      $days = $element['#date_days'];
      $day = date('w', $time);
      if (!in_array($day, $days)) {
        $form_state
          ->setError($element, t('%name must be a %days.', [
          '%name' => $name,
          '%days' => WebformArrayHelper::toString(array_intersect_key(DateHelper::weekDays(TRUE), array_combine($days, $days)), t('or')),
        ]));
      }
    }
  }

  /**
   * {@inheritdoc}
   */
  public function getTestValues(array $element, WebformInterface $webform, array $options = []) {
    $format = DateFormat::load('html_datetime')
      ->getPattern();
    if (!empty($element['#date_year_range'])) {
      list($min, $max) = static::datetimeRangeYears($element['#date_year_range']);
    }
    else {
      $min = !empty($element['#date_date_min']) ? strtotime($element['#date_date_min']) : strtotime('-10 years');
      $max = !empty($element['#date_date_max']) ? strtotime($element['#date_date_max']) : max($min, strtotime('+20 years') ?: PHP_INT_MAX);
    }
    return static::formatDate($format, rand($min, $max));
  }

  /**
   * Specifies the start and end year to use as a date range.
   *
   * Copied from: DateElementBase::datetimeRangeYears.
   *
   * @param string $string
   *   A min and max year string like '-3:+1' or '2000:2010' or '2000:+3'.
   * @param object $date
   *   (optional) A date object to test as a default value. Defaults to NULL.
   *
   * @return array
   *   A numerically indexed array, containing the minimum and maximum year
   *   described by this pattern.
   *
   * @see \Drupal\Core\Datetime\Element\DateElementBase::datetimeRangeYears
   */
  protected static function datetimeRangeYears($string, $date = NULL) {
    $datetime = new DrupalDateTime();
    $this_year = $datetime
      ->format('Y');
    list($min_year, $max_year) = explode(':', $string);

    // Valid patterns would be -5:+5, 0:+1, 2008:2010.
    $plus_pattern = '@[\\+|\\-][0-9]{1,4}@';
    $year_pattern = '@^[0-9]{4}@';
    if (!preg_match($year_pattern, $min_year, $matches)) {
      if (preg_match($plus_pattern, $min_year, $matches)) {
        $min_year = $this_year + $matches[0];
      }
      else {
        $min_year = $this_year;
      }
    }
    if (!preg_match($year_pattern, $max_year, $matches)) {
      if (preg_match($plus_pattern, $max_year, $matches)) {
        $max_year = $this_year + $matches[0];
      }
      else {
        $max_year = $this_year;
      }
    }

    // We expect the $min year to be less than the $max year. Some custom values
    // for -99:+99 might not obey that.
    if ($min_year > $max_year) {
      $temp = $max_year;
      $max_year = $min_year;
      $min_year = $temp;
    }

    // If there is a current value, stretch the range to include it.
    $value_year = $date instanceof DrupalDateTime ? $date
      ->format('Y') : '';
    if (!empty($value_year)) {
      $min_year = min($value_year, $min_year);
      $max_year = max($value_year, $max_year);
    }
    return [
      $min_year,
      $max_year,
    ];
  }

  /**
   * Format custom date.
   *
   * @param string $custom_format
   *   A PHP date format string suitable for input to date().
   * @param int $timestamp
   *   (optional) A UNIX timestamp to format.
   *
   * @return string
   *   Formatted date.
   */
  protected static function formatDate($custom_format, $timestamp = NULL) {

    /** @var \Drupal\Core\Datetime\DateFormatterInterface $date_formatter */
    $date_formatter = \Drupal::service('date.formatter');
    return $date_formatter
      ->format($timestamp ?: \Drupal::time()
      ->getRequestTime(), 'custom', $custom_format);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DateBase::afterBuild public static function After build handler for date elements. 1
DateBase::buildExportRecord public function Build an element's export row. Overrides WebformElementBase::buildExportRecord
DateBase::datetimeRangeYears protected static function Specifies the start and end year to use as a date range.
DateBase::defineDefaultProperties protected function Define an element's default properties. Overrides WebformElementBase::defineDefaultProperties 3
DateBase::form public function Gets the actual configuration webform array to be built. Overrides WebformElementBase::form 3
DateBase::formatDate protected static function Format custom date.
DateBase::formatTextItem protected function Format an element's value as text. Overrides WebformElementBase::formatTextItem
DateBase::getDateType protected function Get the type of date/time element.
DateBase::getItemDefaultFormat public function Get an element's default single value format name. Overrides WebformElementBase::getItemDefaultFormat
DateBase::getItemFormats public function Get an element's available single value formats. Overrides WebformElementBase::getItemFormats
DateBase::getTestValues public function Get test values for an element. Overrides WebformElementBase::getTestValues
DateBase::parseInputFormat protected function Parse GNU Date Input Format.
DateBase::prepare public function Prepare an element to be rendered within a webform. Overrides WebformElementBase::prepare 3
DateBase::prepareElementValidateCallbacks protected function Prepare an element's validation callbacks. Overrides WebformElementBase::prepareElementValidateCallbacks
DateBase::preValidateDate public static function Webform element pre validation handler for Date elements.
DateBase::setDefaultValue public function Set an element's default value using saved data. Overrides WebformElementBase::setDefaultValue 1
DateBase::setGnuDateInputFormatError protected function Set GNU date input format error.
DateBase::validateConfigurationForm public function Form validation handler. Overrides WebformElementBase::validateConfigurationForm 1
DateBase::validateDate public static function Webform element validation handler for date elements.
DateBase::validateGnuDateInputFormat protected function Validate GNU date input format.
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
TrustedCallbackInterface::THROW_EXCEPTION constant Untrusted callbacks throw exceptions.
TrustedCallbackInterface::TRIGGER_SILENCED_DEPRECATION constant Untrusted callbacks trigger silenced E_USER_DEPRECATION errors.
TrustedCallbackInterface::TRIGGER_WARNING constant Untrusted callbacks trigger E_USER_WARNING errors.
WebformCompositeFormElementTrait::preRenderWebformCompositeFormElement public static function Adds form element theming to an element if its title or description is set. 1
WebformElementBase::$configFactory protected property The configuration factory.
WebformElementBase::$currentUser protected property The current user.
WebformElementBase::$defaultProperties protected property An associative array of an element's default properties names and values.
WebformElementBase::$elementInfo protected property A element info manager.
WebformElementBase::$elementManager protected property The webform element manager.
WebformElementBase::$entityTypeManager protected property The entity type manager.
WebformElementBase::$librariesManager protected property The webform libraries manager.
WebformElementBase::$logger protected property A logger instance.
WebformElementBase::$submissionStorage protected property The webform submission storage.
WebformElementBase::$tokenManager protected property The token manager.
WebformElementBase::$translatableProperties protected property An indexed array of an element's translated properties.
WebformElementBase::alterForm public function Alter an element's associated form. Overrides WebformElementInterface::alterForm 1
WebformElementBase::build protected function Build an element as text or HTML. 4
WebformElementBase::buildConfigurationForm public function Form constructor. Overrides PluginFormInterface::buildConfigurationForm 3
WebformElementBase::buildConfigurationFormTabs protected function Build configuration form tabs. 1
WebformElementBase::buildExportHeader public function Build an element's export header. Overrides WebformElementInterface::buildExportHeader 4
WebformElementBase::buildExportOptionsForm public function Get an element's export options webform. Overrides WebformElementInterface::buildExportOptionsForm 4
WebformElementBase::buildHtml public function Build an element as HTML element. Overrides WebformElementInterface::buildHtml 2
WebformElementBase::buildText public function Build an element as text element. Overrides WebformElementInterface::buildText 1
WebformElementBase::checkAccessRule protected function Checks an access rule against a user account's roles and id.
WebformElementBase::checkAccessRules public function Check element access (rules). Overrides WebformElementInterface::checkAccessRules
WebformElementBase::create public static function Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface::create 2
WebformElementBase::defineDefaultBaseProperties protected function Define default base properties used by all elements. 4
WebformElementBase::defineDefaultMultipleProperties protected function Define default multiple properties used by most elements. 1
WebformElementBase::defineTranslatableProperties protected function Define an element's translatable properties. 15
WebformElementBase::displayDisabledWarning public function Display element disabled warning. Overrides WebformElementInterface::displayDisabledWarning 1
WebformElementBase::finalize public function Finalize an element to be rendered within a webform. Overrides WebformElementInterface::finalize 1
WebformElementBase::format protected function Format an element's value as HTML or plain text. 2
WebformElementBase::formatCustomItem protected function Format an element's item using custom HTML or plain text. 2
WebformElementBase::formatCustomItems protected function Format an element's items using custom HTML or plain text.
WebformElementBase::formatHtml public function Format an element's value as HTML. Overrides WebformElementInterface::formatHtml 2
WebformElementBase::formatHtmlItem protected function Format an element's value as HTML. 19
WebformElementBase::formatHtmlItems protected function Format an element's items as HTML. 2
WebformElementBase::formatTableColumn public function Format an element's table column value. Overrides WebformElementInterface::formatTableColumn 4
WebformElementBase::formatText public function Format an element's value as plain text. Overrides WebformElementInterface::formatText 2
WebformElementBase::formatTextItems protected function Format an element's items as text. 2
WebformElementBase::getAdminLabel public function Get an element's admin label (#admin_title, #title or #webform_key). Overrides WebformElementInterface::getAdminLabel
WebformElementBase::getConfigurationFormProperties public function Get an associative array of element properties from configuration webform. Overrides WebformElementInterface::getConfigurationFormProperties 3
WebformElementBase::getConfigurationFormProperty protected function Get configuration property value. 1
WebformElementBase::getDefaultBaseProperties Deprecated protected function Get default base properties used by all elements.
WebformElementBase::getDefaultKey public function Gets the element's default key. Overrides WebformElementInterface::getDefaultKey 1
WebformElementBase::getDefaultMultipleProperties Deprecated protected function Get default multiple properties used by most elements.
WebformElementBase::getDefaultProperties public function Get default properties. Overrides WebformElementInterface::getDefaultProperties
WebformElementBase::getDefaultProperty public function Get an element's default property value. Overrides WebformElementInterface::getDefaultProperty
WebformElementBase::getElementInfoDefaultProperty protected function Get a render element's default property.
WebformElementBase::getElementProperty public function Get an element's property value. Overrides WebformElementInterface::getElementProperty
WebformElementBase::getElementSelectorInputsOptions protected function Get an element's (sub)inputs selectors as options. 9
WebformElementBase::getElementSelectorInputValue public function Get an element's (sub)input selector value. Overrides WebformElementInterface::getElementSelectorInputValue 5
WebformElementBase::getElementSelectorOptions public function Get an element's selectors as options. Overrides WebformElementInterface::getElementSelectorOptions 11
WebformElementBase::getElementSelectorSourceValues public function Get an element's selectors source values. Overrides WebformElementInterface::getElementSelectorSourceValues 3
WebformElementBase::getElementStateOptions public function Get an element's supported states as options. Overrides WebformElementInterface::getElementStateOptions 1
WebformElementBase::getExportDefaultOptions public function Get an element's default export options. Overrides WebformElementInterface::getExportDefaultOptions 5
WebformElementBase::getFormElementClassDefinition public function Get the Webform element's form element class definition. Overrides WebformElementInterface::getFormElementClassDefinition
WebformElementBase::getFormInlineContainer protected function Get form--inline container which is used for side-by-side element layout.
WebformElementBase::getInfo public function Retrieves the default properties for the defined element type. Overrides WebformElementInterface::getInfo
WebformElementBase::getItemFormat public function Get element's single value format name by looking for '#format' property, global settings, and finally default settings. Overrides WebformElementInterface::getItemFormat 1
WebformElementBase::getItemsDefaultFormat public function Get an element's default multiple value format name. Overrides WebformElementInterface::getItemsDefaultFormat 2
WebformElementBase::getItemsFormat public function Get element's multiple value format name by looking for '#format' property, global settings, and finally default settings. Overrides WebformElementInterface::getItemsFormat
WebformElementBase::getItemsFormats public function Get an element's available multiple value formats. Overrides WebformElementInterface::getItemsFormats 2
WebformElementBase::getKey public function Get an element's key/name. Overrides WebformElementInterface::getKey
WebformElementBase::getLabel public function Get an element's label (#title or #webform_key). Overrides WebformElementInterface::getLabel
WebformElementBase::getOffCanvasWidth public function Get configuration form's off-canvas width. Overrides WebformElementInterface::getOffCanvasWidth 1
WebformElementBase::getPluginApiLink public function Get link to element's API documentation. Overrides WebformElementInterface::getPluginApiLink
WebformElementBase::getPluginApiUrl public function Get the URL for the element's API documentation. Overrides WebformElementInterface::getPluginApiUrl
WebformElementBase::getPluginCategory public function Gets the category of the plugin instance. Overrides WebformElementInterface::getPluginCategory
WebformElementBase::getPluginDescription public function Gets the description of the plugin instance. Overrides WebformElementInterface::getPluginDescription
WebformElementBase::getPluginLabel public function Gets the label of the plugin instance. Overrides WebformElementInterface::getPluginLabel 3
WebformElementBase::getRawValue public function Get an element's submission raw value. Overrides WebformElementInterface::getRawValue
WebformElementBase::getRelatedTypes public function Get related element types. Overrides WebformElementInterface::getRelatedTypes 6
WebformElementBase::getTableColumn public function Get element's table column(s) settings. Overrides WebformElementInterface::getTableColumn 4
WebformElementBase::getTranslatableProperties public function Get translatable properties. Overrides WebformElementInterface::getTranslatableProperties
WebformElementBase::getTypeName public function Gets the type name (aka id) of the plugin instance with the 'webform_' prefix. Overrides WebformElementInterface::getTypeName
WebformElementBase::getValue public function Get an element's submission value. Overrides WebformElementInterface::getValue 1
WebformElementBase::hasCompositeFormElementWrapper protected function Determine if the element has a composite field wrapper.
WebformElementBase::hasManagedFiles public function Determine if the element is or includes a managed_file upload element. Overrides WebformElementInterface::hasManagedFiles 2
WebformElementBase::hasMultipleValues public function Checks if the element value has multiple values. Overrides WebformElementInterface::hasMultipleValues 6
WebformElementBase::hasMultipleWrapper public function Checks if the element uses the 'webform_multiple' element. Overrides WebformElementInterface::hasMultipleWrapper 3
WebformElementBase::hasProperty public function Determine if the element supports a specified property. Overrides WebformElementInterface::hasProperty
WebformElementBase::hasValue public function Determine if an element's has a submission value. Overrides WebformElementInterface::hasValue 2
WebformElementBase::hasWrapper public function Checks if the element has a wrapper. Overrides WebformElementInterface::hasWrapper
WebformElementBase::hiddenElementAfterBuild public static function Webform element #after_build callback.
WebformElementBase::initialize public function Initialize an element to be displayed, rendered, or exported. Overrides WebformElementInterface::initialize 8
WebformElementBase::isComposite public function Checks if the element is a composite element. Overrides WebformElementInterface::isComposite
WebformElementBase::isContainer public function Checks if the element is a container that can contain elements. Overrides WebformElementInterface::isContainer 9
WebformElementBase::isDisabled public function Checks if the element is disabled. Overrides WebformElementInterface::isDisabled
WebformElementBase::isEmptyExcluded public function Checks if an empty element is excluded. Overrides WebformElementInterface::isEmptyExcluded 1
WebformElementBase::isEnabled public function Checks if the element is enabled. Overrides WebformElementInterface::isEnabled 1
WebformElementBase::isExcluded public function Checks if the element is excluded via webform.settings. Overrides WebformElementInterface::isExcluded
WebformElementBase::isHidden public function Checks if the element is hidden. Overrides WebformElementInterface::isHidden 2
WebformElementBase::isInput public function Checks if the element carries a value. Overrides WebformElementInterface::isInput 11
WebformElementBase::isMultiline public function Checks if the element value could contain multiple lines. Overrides WebformElementInterface::isMultiline 2
WebformElementBase::isRoot public function Checks if the element is a root element. Overrides WebformElementInterface::isRoot 3
WebformElementBase::postCreate public function Acts on a webform submission element after it is created. Overrides WebformElementInterface::postCreate 1
WebformElementBase::postDelete public function Delete any additional value associated with an element. Overrides WebformElementInterface::postDelete 5
WebformElementBase::postLoad public function Acts on loaded webform submission. Overrides WebformElementInterface::postLoad 1
WebformElementBase::postSave public function Acts on a saved webform submission element before the insert or update hook is invoked. Overrides WebformElementInterface::postSave 5
WebformElementBase::preCreate public function Changes the values of an entity before it is created. Overrides WebformElementInterface::preCreate 1
WebformElementBase::preDelete public function 1
WebformElementBase::prefixExportHeader protected function Prefix an element's export header.
WebformElementBase::prepareCompositeFormElement protected function Replace Core's composite #pre_render with Webform's composite #pre_render.
WebformElementBase::prepareElementPreRenderCallbacks protected function Prepare an element's pre render callbacks. 3
WebformElementBase::prepareMultipleWrapper protected function Set multiple element wrapper. 1
WebformElementBase::prepareWrapper protected function Set an elements #states and flexbox wrapper. 1
WebformElementBase::preRenderFixFlexboxWrapper public static function Fix flexbox wrapper.
WebformElementBase::preRenderFixStatesWrapper public static function Fix state wrapper.
WebformElementBase::preSave public function Acts on a webform submission element before the presave hook is invoked. Overrides WebformElementInterface::preSave 4
WebformElementBase::preview public function Generate a renderable preview of the element. Overrides WebformElementInterface::preview 37
WebformElementBase::replaceTokens public function Replace tokens for all element properties. Overrides WebformElementInterface::replaceTokens
WebformElementBase::setConfigurationFormDefaultValue protected function Set an element's configuration webform element default value. 2
WebformElementBase::setConfigurationFormDefaultValueRecursive protected function Set configuration webform default values recursively.
WebformElementBase::setElementDefaultCallback protected function Set element's default callback.
WebformElementBase::submitConfigurationForm public function Form submission handler. Overrides PluginFormInterface::submitConfigurationForm
WebformElementBase::supportsMultipleValues public function Checks if the element supports multiple values. Overrides WebformElementInterface::supportsMultipleValues 8
WebformElementBase::trustedCallbacks public static function Lists the trusted callbacks provided by the implementing class. Overrides TrustedCallbackInterface::trustedCallbacks 1
WebformElementBase::validateMinlength public static function Form API callback. Validate element #minlength value.
WebformElementBase::validateMultiple public static function Form API callback. Validate element #multiple > 1 value.
WebformElementBase::validateUnique public static function Form API callback. Validate element #unique value.
WebformElementBase::validateUniqueMultiple public static function Form API callback. Validate element #unique multiple values.
WebformElementBase::__construct public function Constructs a WebformElementBase object. Overrides PluginBase::__construct 2
WebformEntityInjectionTrait::$webform protected property The webform.
WebformEntityInjectionTrait::$webformSubmission protected property The webform submission.
WebformEntityInjectionTrait::getWebform public function Get the webform that this handler is attached to.
WebformEntityInjectionTrait::getWebformSubmission public function Set webform and webform submission entity.
WebformEntityInjectionTrait::resetEntities public function Reset webform and webform submission entity.
WebformEntityInjectionTrait::setEntities public function
WebformEntityInjectionTrait::setWebform public function Set the webform that this is handler is attached to.
WebformEntityInjectionTrait::setWebformSubmission public function Get the webform submission that this handler is handling.