You are here

public function MenuPositionRuleForm::validateForm in Menu Position 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/MenuPositionRuleForm.php, line 247

Class

MenuPositionRuleForm
The Menu Position rule form.

Namespace

Drupal\menu_position\Form

Code

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

  // Don't allow the user to select a menu name instead of a menu item.
  list($menu_name, $parent) = explode(':', $form_state
    ->getValue('parent'));
  if (empty($parent)) {
    $form_state
      ->setErrorByName('parent', $this
      ->t('Please select a menu item. You have selected the name of a menu.'));
  }

  // Validate visibility condition settings.
  foreach ($form_state
    ->getValue('conditions') 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([
        'conditions',
        $condition_id,
        'negate',
      ], (bool) $values['negate']);
    }

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