You are here

protected function SavedSearchTypeForm::buildNotificationPluginConfigForm in Search API Saved Searches 8

Builds the configuration forms for all selected notification plugins.

Parameters

array $form: The current form array.

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

1 call to SavedSearchTypeForm::buildNotificationPluginConfigForm()
SavedSearchTypeForm::form in src/Form/SavedSearchTypeForm.php
Gets the actual form array to be built.

File

src/Form/SavedSearchTypeForm.php, line 373

Class

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

Namespace

Drupal\search_api_saved_searches\Form

Code

protected function buildNotificationPluginConfigForm(array &$form, FormStateInterface $form_state) {
  $type = $this->entity;
  $selected_plugins = $form_state
    ->getValue('notification_plugins');
  if ($selected_plugins === NULL) {

    // Initial form build, use the saved notification plugins (or none for new
    // indexes).
    $plugins = $type
      ->getNotificationPlugins();
  }
  else {

    // The form is being rebuilt – use the notification plugins selected by
    // the user instead of the ones saved in the config.
    $plugins = $this
      ->getNotificationPluginManager()
      ->createPlugins($type, $selected_plugins);
  }
  $form_state
    ->set('notification_plugins', array_keys($plugins));
  $show_message = FALSE;
  foreach ($plugins as $plugin_id => $plugin) {
    if ($plugin instanceof PluginFormInterface) {

      // Get the "sub-form state" and appropriate form part to send to
      // buildConfigurationForm().
      $plugin_form = [];
      if (!empty($form['notification_configs'][$plugin_id])) {
        $plugin_form = $form['notification_configs'][$plugin_id];
      }
      $plugin_form_state = SubformState::createForSubform($plugin_form, $form, $form_state);
      $form['notification_configs'][$plugin_id] = $plugin
        ->buildConfigurationForm($plugin_form, $plugin_form_state);
      $show_message = TRUE;
      $form['notification_configs'][$plugin_id]['#type'] = 'details';
      $form['notification_configs'][$plugin_id]['#title'] = $this
        ->t('Configure the %notification notification method', [
        '%notification' => $plugin
          ->label(),
      ]);
      $form['notification_configs'][$plugin_id]['#open'] = $type
        ->isNew();
    }
  }

  // If the user changed the notification plugins and there is at least one
  // plugin config form, show a message telling the user to configure it.
  if ($selected_plugins && $show_message) {
    $message = $this
      ->t('Please configure the used notification methods.');
    $this
      ->messenger()
      ->addWarning($message);
  }
}