You are here

public function WordfilterConfigurationForm::save in Wordfilter 8.2

Form submission handler for the 'save' action.

Normally this method should be overridden to provide specific messages to the user and redirect the form after the entity has been saved.

Parameters

array $form: An associative array containing the structure of the form.

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

Return value

int Either SAVED_NEW or SAVED_UPDATED, depending on the operation performed.

Overrides EntityForm::save

File

src/Form/WordfilterConfigurationForm.php, line 312

Class

WordfilterConfigurationForm
Class WordfilterConfigurationForm.

Namespace

Drupal\wordfilter\Form

Code

public function save(array $form, FormStateInterface $form_state) {
  $wordfilter_configuration = $this
    ->getWordfilterConfiguration();
  $items = $wordfilter_configuration
    ->getItems();

  // Put submitted data onto the items. It has automatically been populated on
  // the configuration entity's data, but not the configuration items objects.
  foreach ($wordfilter_configuration
    ->get('items') as $delta => $item) {
    $items[$delta]
      ->setSubstitute($item['substitute']);
    $items[$delta]
      ->setFilterWords($item['filter_words']);
  }
  $num_items = count($items);
  if ($num_items > 1) {

    // Remove multiple empty items.
    foreach ($items as $item) {
      if (empty($item
        ->getSubstitute()) && empty($item
        ->getFilterWords())) {
        $wordfilter_configuration
          ->removeItem($item);
      }
    }
  }
  $status = $wordfilter_configuration
    ->save();
  switch ($status) {
    case SAVED_NEW:
      drupal_set_message($this
        ->t('Created the %label Wordfilter configuration.', [
        '%label' => $wordfilter_configuration
          ->label(),
      ]));
      break;
    default:
      drupal_set_message($this
        ->t('Saved the %label Wordfilter configuration.', [
        '%label' => $wordfilter_configuration
          ->label(),
      ]));
  }
  $form_state
    ->setRedirectUrl($wordfilter_configuration
    ->toUrl('collection'));
}