You are here

public function SavedSearchTypeForm::validateForm in Search API Saved Searches 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/SavedSearchTypeForm.php, line 450

Class

SavedSearchTypeForm
Provides a form for adding and editing saved search types.

Namespace

Drupal\search_api_saved_searches\Form

Code

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

  /** @var \Drupal\search_api_saved_searches\SavedSearchTypeInterface $type */
  $type = $this
    ->getEntity();

  // Store the selected displays as a numerically indexed array.
  $key = [
    'options',
    'displays',
    'selected',
  ];
  $selected = $form_state
    ->getValue($key, []);
  $selected = array_keys(array_filter($selected));
  $form_state
    ->setValue($key, $selected);

  // Store the array of notification plugin IDs with integer keys.
  $plugin_ids = array_values(array_filter($form_state
    ->getValue('notification_plugins', [])));
  $form_state
    ->setValue('notification_plugins', $plugin_ids);

  // Make sure "query_limit" is not less than the "max_results", if both are
  // set.
  $max_results = $form_state
    ->getValue([
    'options',
    'max_results',
  ]);
  $query_limit = $form_state
    ->getValue([
    'options',
    'query_limit',
  ]);
  if ($max_results && $query_limit && $max_results > $query_limit) {
    $vars = [
      '@query_limit' => $form['misc']['query_limit']['#title'],
      '@max_results' => $form['misc']['max_results']['#title'],
    ];
    $form_state
      ->setErrorByName('options][query_limit', $this
      ->t('"@query_limit" must not be less than "@max_results", if both are specified.', $vars));
  }
  else {
    if ($max_results === '') {
      $form_state
        ->unsetValue([
        'options',
        'max_results',
      ]);
    }
    if ($query_limit === '') {
      $form_state
        ->unsetValue([
        'options',
        'query_limit',
      ]);
    }
  }

  // Call validateConfigurationForm() for each enabled notification plugin
  // with a form.
  $plugins = $this
    ->getNotificationPluginManager()
    ->createPlugins($type, $plugin_ids);
  $previous_plugins = $form_state
    ->get('notification_plugins');
  foreach ($plugins as $plugin_id => $plugin) {
    if ($plugin instanceof PluginFormInterface) {
      if (!in_array($plugin_id, $previous_plugins)) {
        $form_state
          ->setRebuild();
        continue;
      }
      $plugin_form =& $form['notification_configs'][$plugin_id];
      $plugin_form_state = SubformState::createForSubform($plugin_form, $form, $form_state);
      $plugin
        ->validateConfigurationForm($plugin_form, $plugin_form_state);
    }
  }
}