You are here

public function SearchExcludeNidForm::submitForm in Search exclude nid 8

Form submission 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 ConfigFormBase::submitForm

File

src/Form/SearchExcludeNidForm.php, line 51

Class

SearchExcludeNidForm
The form to collect nids to be excluded.

Namespace

Drupal\search_exclude_nid\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {
  $config = $this
    ->config('search_exclude_nid.settings');
  $excluded_nids = array();
  if (!empty($form_state
    ->getValue('excluded_nids'))) {
    $excluded_nids_arr = explode(',', $form_state
      ->getValue('excluded_nids'));
    foreach ($excluded_nids_arr as $excluded_nid) {
      $excluded_nid = intval($excluded_nid);
      $node = node_load($excluded_nid);

      // Check if node exists for given nid and avoid duplicates.
      if ($excluded_nid && !in_array($excluded_nid, $excluded_nids) && !empty($node)) {
        $excluded_nids[] = $excluded_nid;
      }
      else {
        drupal_set_message(t('nid: %nid has been removed from exclusion list as no node exists with that id or it is a duplicate.', array(
          '%nid' => $excluded_nid,
        )), 'warning');
      }
    }
  }
  $config
    ->set('excluded_nids', $excluded_nids);
  $config
    ->save();
  return parent::submitForm($form, $form_state);
}