You are here

public static function TimePickerWidget::fieldSettingsFormValidate in Date Popup Timepicker 8

1 call to TimePickerWidget::fieldSettingsFormValidate()
DateRangeTimePickerWidget::fieldSettingsFormValidate in src/Plugin/Field/FieldWidget/DateRangeTimePickerWidget.php

File

src/Plugin/Field/FieldWidget/TimePickerWidget.php, line 351

Class

TimePickerWidget
Plugin implementation of the 'datetime_timepicker' widget.

Namespace

Drupal\date_popup_timepicker\Plugin\Field\FieldWidget

Code

public static function fieldSettingsFormValidate(&$element, FormStateInterface $form_state) {
  $key = $element['#parents'][count($element['#parents']) - 1];
  $copy_element_settings = $element['#parents'];
  unset($copy_element_settings[count($copy_element_settings) - 1]);
  $settings =& $form_state
    ->getValue($copy_element_settings);
  if (isset($settings)) {

    // For two-tiered array.
    foreach ($settings[$key] as $subkey => $value) {

      // Init validation limits.
      if ($key == 'minutes' && $subkey == 'interval') {
        $limits = [
          1,
          59,
        ];
      }
      elseif ($key == 'hours' || $subkey == 'hour') {
        $limits = [
          0,
          23,
        ];
      }
      else {
        $limits = [
          0,
          59,
        ];
      }

      // Validate int hours and minutes settings.
      if ($value !== '') {
        if (!is_numeric($value) || intval($value) != $value || $value < $limits[0] || $value > $limits[1]) {
          $t_args = [
            '%name' => $element['#title'],
            '@start' => $limits[0],
            '@end' => $limits[1],
          ];
          $form_state
            ->setErrorByName($element['#markup'], t('%name must be an integer between @start and @end.', $t_args));
        }
        else {
          $form_state
            ->setValue($settings[$key][$subkey], (int) $value);
        }
      }
      else {
        $settings[$key][$subkey] = NULL;
      }
    }

    // For one-tiered array.
    if ($settings[$key] !== '') {

      // Validate rows part.
      if ($key === 'rows') {
        if (!is_numeric($settings[$key]) || intval($settings[$key]) != $settings[$key] || $settings[$key] < 2) {
          $t_args = [
            '%name' => $element['#title'],
          ];
          $form_state
            ->setErrorByName($element['#markup'], t('%name must be an integer greater than 1.', $t_args));
        }
        else {
          $form_state
            ->setValue($settings[$key], (int) $settings[$key]);
        }
      }
    }
    else {
      $settings[$key] = NULL;
    }
  }
}