You are here

public function FilterConditionForm::save in Tooltip Taxonomy 8

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/FilterConditionForm.php, line 213

Class

FilterConditionForm
Form handler for the FilterCondition entity add and edit forms.

Namespace

Drupal\tooltip_taxonomy\Form

Code

public function save(array $form, FormStateInterface $form_state) {
  $condition = $this->entity;
  $this->pathCondition
    ->submitConfigurationForm($form['path'], $form_state);
  $this->contentTypeCondition
    ->submitConfigurationForm($form['node'], $form_state);

  // The content type negate should be always false.
  $this->contentTypeCondition
    ->setConfig('negate', 0);
  foreach ($form_state
    ->getValues() as $key => $value) {
    if ($key === 'vids') {
      $vids = [];
      foreach ($value as $vid) {
        if (!empty($vid)) {
          $vids[] = $vid;
        }
      }

      // Vocabularies settings.
      $condition
        ->set('vids', $vids);
    }
    else {
      $condition
        ->set($key, $value);
    }
  }

  // Path setting.
  $condition
    ->set('path', $this->pathCondition
    ->getConfiguration());

  // Content type settings.
  $condition
    ->set('contentTypes', $this->contentTypeCondition
    ->getConfiguration());
  $status = $condition
    ->save();
  if ($status) {
    $this
      ->messenger()
      ->addMessage($this
      ->t('Saved the %label Example.', [
      '%label' => $condition
        ->label(),
    ]));
  }
  else {
    $this
      ->messenger()
      ->addMessage($this
      ->t('The %label Example was not saved.', [
      '%label' => $condition
        ->label(),
    ]), MessengerInterface::TYPE_ERROR);
  }

  // We need to invalidate all node pages by node_view tag
  // to apply the changes make here.
  \Drupal::service('cache_tags.invalidator')
    ->invalidateTags([
    'node_view',
  ]);
  $form_state
    ->setRedirect('entity.tooltip_taxonomy.config');
}