You are here

CustomContextualLinkDeleteForm.php in Custom Contextual Links 8.2

Namespace

Drupal\ccl\Form

File

src/Form/CustomContextualLinkDeleteForm.php
View source
<?php

namespace Drupal\ccl\Form;

use Drupal\Core\Entity\EntityConfirmFormBase;
use Drupal\Core\Entity\EntityStorageException;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;

/**
 * Builds the form to delete Custom Contextual Link entities.
 */
class CustomContextualLinkDeleteForm extends EntityConfirmFormBase {

  /**
   * {@inheritdoc}
   */
  public function getQuestion() {
    return $this
      ->t('Are you sure you want to delete %name?', [
      '%name' => $this->entity
        ->label(),
    ]);
  }

  /**
   * {@inheritdoc}
   */
  public function getCancelUrl() {
    return new Url('entity.custom_contextual_link.collection');
  }

  /**
   * {@inheritdoc}
   */
  public function getConfirmText() {
    return $this
      ->t('Delete');
  }

  /**
   * {@inheritdoc}
   */
  public function form(array $form, FormStateInterface $form_state) {
    $form = parent::form($form, $form_state);

    // Add library and class in order to clear the sessionStorage upon deletion.
    $form['#attached']['library'][] = 'ccl/ccl';
    $form['#attributes']['class'][] = 'ccl-form';
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    try {
      $this->entity
        ->delete();
      $this
        ->messenger()
        ->addMessage($this
        ->t('content @type: deleted @label.', [
        '@type' => $this->entity
          ->bundle(),
        '@label' => $this->entity
          ->label(),
      ]));
    } catch (EntityStorageException $exception) {
      $this
        ->logger('ccl')
        ->error($exception
        ->getMessage());
      $this
        ->messenger()
        ->addError($this
        ->t('Could not delete entity. See the error log for more details.'));
    }
    $form_state
      ->setRedirectUrl($this
      ->getCancelUrl());
  }

}

Classes

Namesort descending Description
CustomContextualLinkDeleteForm Builds the form to delete Custom Contextual Link entities.