You are here

public function SmartDateFieldItemList::defaultValuesFormValidate in Smart Date 3.0.x

Same name and namespace in other branches
  1. 8.2 src/Plugin/Field/FieldType/SmartDateFieldItemList.php \Drupal\smart_date\Plugin\Field\FieldType\SmartDateFieldItemList::defaultValuesFormValidate()
  2. 8 src/Plugin/Field/FieldType/SmartDateFieldItemList.php \Drupal\smart_date\Plugin\Field\FieldType\SmartDateFieldItemList::defaultValuesFormValidate()
  3. 3.x src/Plugin/Field/FieldType/SmartDateFieldItemList.php \Drupal\smart_date\Plugin\Field\FieldType\SmartDateFieldItemList::defaultValuesFormValidate()
  4. 3.1.x src/Plugin/Field/FieldType/SmartDateFieldItemList.php \Drupal\smart_date\Plugin\Field\FieldType\SmartDateFieldItemList::defaultValuesFormValidate()
  5. 3.2.x src/Plugin/Field/FieldType/SmartDateFieldItemList.php \Drupal\smart_date\Plugin\Field\FieldType\SmartDateFieldItemList::defaultValuesFormValidate()
  6. 3.3.x src/Plugin/Field/FieldType/SmartDateFieldItemList.php \Drupal\smart_date\Plugin\Field\FieldType\SmartDateFieldItemList::defaultValuesFormValidate()
  7. 3.4.x src/Plugin/Field/FieldType/SmartDateFieldItemList.php \Drupal\smart_date\Plugin\Field\FieldType\SmartDateFieldItemList::defaultValuesFormValidate()

Validates the submitted default value.

Invoked from \Drupal\field_ui\Form\FieldConfigEditForm to allow administrators to configure instance-level default value.

Parameters

array $element: The default value form element.

array $form: The form where the settings form is being included in.

\Drupal\Core\Form\FormStateInterface $form_state: The form state of the (entire) configuration form.

Overrides DateTimeFieldItemList::defaultValuesFormValidate

File

src/Plugin/Field/FieldType/SmartDateFieldItemList.php, line 72

Class

SmartDateFieldItemList
Represents a configurable entity smartdate field.

Namespace

Drupal\smart_date\Plugin\Field\FieldType

Code

public function defaultValuesFormValidate(array $element, array &$form, FormStateInterface $form_state) {
  if ($duration = $form_state
    ->getValue([
    'default_value_input',
    'default_duration',
  ])) {
    $increments = SmartDateListItemBase::parseValues($form_state
      ->getValue([
      'default_value_input',
      'default_duration_increments',
    ]));

    // Handle a false result: will display the proper error later.
    if (!$increments) {
      $increments = [];
    }
    $increment_min = -1;

    // Iterate through returned array and throw an error for an invalid key.
    foreach ($increments as $key => $label) {
      if (intval($key) == 0 && $key !== '0' && $key !== 0 && $key !== 'custom') {
        $form_state
          ->setErrorByName('default_value_input][default_duration_increments', $this
          ->t('Invalid tokens in the allowed increments specified. Please provide either integers or "custom" as the key for each value.'));
        break;
      }
      else {
        $increment_min = $increment_min < intval($key) ? intval($key) : $increment_min;
      }
    }
    if (!in_array('custom', $increments)) {
      if ($increment_min < 0) {
        $form_state
          ->setErrorByName('default_value_input][default_duration_increments', $this
          ->t('Unable to parse valid durations from the allowed increments specified.'));
      }
      else {
        $messenger = \Drupal::messenger();
        $messenger
          ->addMessage($this
          ->t('No string to allow for custom values, so the provided increments will be strictly enforced.'), 'warning');
      }
    }
    if (!isset($increments[$duration])) {
      $form_state
        ->setErrorByName('default_value_input][default_duration', $this
        ->t('Please specify a default duration that is one of the provided options.'));
    }
  }

  // Use the parent class method to validate relative dates.
  DateTimeFieldItemList::defaultValuesFormValidate($element, $form, $form_state);
}