You are here

private function ContextFormBase::validateReactions in Context 8.4

Same name and namespace in other branches
  1. 8 modules/context_ui/src/Form/ContextFormBase.php \Drupal\context_ui\Form\ContextFormBase::validateReactions()

Validate the context reaction plugins configuration forms.

Parameters

array $form: The rendered form.

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

1 call to ContextFormBase::validateReactions()
ContextFormBase::validateForm in modules/context_ui/src/Form/ContextFormBase.php
Form validation handler.

File

modules/context_ui/src/Form/ContextFormBase.php, line 247

Class

ContextFormBase
Provides a context form base.

Namespace

Drupal\context_ui\Form

Code

private function validateReactions(array &$form, FormStateInterface $form_state) {
  $reactions = $form_state
    ->getValue('reactions', []);

  // Loop trough each reaction and update the configuration values by
  // validating the reactions form.
  foreach ($reactions as $reaction_id => $configuration) {
    $reaction = $this->entity
      ->getReaction($reaction_id);
    $reaction_values = (new FormState())
      ->setValues($configuration);
    $reaction
      ->validateConfigurationForm($form, $reaction_values);

    // Menu root should not be selected as the plugin does not exist.
    if ($reaction_id === 'menu') {
      $menu_items = $reaction_values
        ->getValue('menu_items');
      foreach ($menu_items as $menu_item) {
        $menu = strtok($menu_item, ':');
        $plugin_id = substr($menu_item, strlen($menu) + 1);
        if (!$plugin_id) {
          $plugin_title = $form['reactions']['reaction-menu']['options']['menu_items']['#options'][$menu_item];
          $error = $this
            ->t('Menu root @plugin_title cannot be selected.', [
            '@plugin_title' => $plugin_title,
          ]);
          $form_state
            ->setErrorByName("reactions][{$reaction_id}][{$menu_item}", $error);
        }
      }
    }
    if ($reaction_values
      ->hasAnyErrors()) {

      // In case of errors, copy them back from the dummy FormState to the
      // master form.
      foreach ($reaction_values
        ->getErrors() as $element => $error) {
        $form_state
          ->setErrorByName("reactions][{$reaction_id}][{$element}", $error);
      }
    }
  }
}