You are here

public function CustomContextualLinkForm::save in Custom Contextual Links 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/CustomContextualLinkForm.php, line 299

Class

CustomContextualLinkForm
Class CustomContextualLinkForm.

Namespace

Drupal\ccl\Form

Code

public function save(array $form, FormStateInterface $form_state) {

  /** @var \Drupal\ccl\Entity\CustomContextualLink $custom_contextual_link */
  $custom_contextual_link = $this->entity;
  try {
    $custom_contextual_link
      ->clearOptions();
    $status = $custom_contextual_link
      ->save();
    switch ($status) {
      case SAVED_NEW:
        $this
          ->messenger()
          ->addMessage($this
          ->t('Created the %label Custom Contextual Link.', [
          '%label' => $custom_contextual_link
            ->label(),
        ]));
        break;
      default:
        $this
          ->messenger()
          ->addMessage($this
          ->t('Saved the %label Custom Contextual Link.', [
          '%label' => $custom_contextual_link
            ->label(),
        ]));
    }
    $this->eventDispatcher
      ->dispatch('ccl_update_cache');
    try {
      $form_state
        ->setRedirectUrl($custom_contextual_link
        ->toUrl('collection'));
    } catch (EntityMalformedException $exception) {
      $this
        ->logger('ccl')
        ->error($exception
        ->getMessage());
    }
  } catch (EntityStorageException $exception) {
    $this
      ->logger('ccl')
      ->error($exception
      ->getMessage());
    $this
      ->messenger()
      ->addError($this
      ->t('There was an error saving the link. Please consult the error log for more details.'));
  }
}