You are here

public function ThemeSwitcherRuleForm::validateForm in Theme Switcher Rules 8

Form validation handler.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Overrides FormBase::validateForm

File

src/Form/ThemeSwitcherRuleForm.php, line 238

Class

ThemeSwitcherRuleForm
Form handler for the ThemeSwitcherRule add and edit forms.

Namespace

Drupal\theme_switcher\Form

Code

public function validateForm(array &$form, FormStateInterface $form_state) {
  parent::validateForm($form, $form_state);

  // Validate the weight.
  $form_state
    ->setValue('weight', (int) $form_state
    ->getValue('weight'));

  // Validate visibility condition settings.
  foreach ($form_state
    ->getValue('visibility') as $condition_id => $values) {

    // All condition plugins use 'negate' as a Boolean in their schema.
    // However, certain form elements may return it as 0/1. Cast here to
    // ensure the data is in the expected type.
    if (array_key_exists('negate', $values)) {
      $form_state
        ->setValue([
        'visibility',
        $condition_id,
        'negate',
      ], (bool) $values['negate']);
    }

    // Allow the condition to validate the form.
    $condition = $form_state
      ->get([
      'conditions',
      $condition_id,
    ]);
    $subform = SubformState::createForSubform($form['visibility'][$condition_id], $form, $form_state);
    $condition
      ->validateConfigurationForm($form['visibility'][$condition_id], $subform);
  }
}