You are here

FieldInheritanceDeleteForm.php in Field Inheritance 8

Same filename and directory in other branches
  1. 2.0.x src/Form/FieldInheritanceDeleteForm.php

File

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

namespace Drupal\field_inheritance\Form;

use Drupal\Core\Entity\EntityConfirmFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Drupal\Core\Messenger\Messenger;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Entity\EntityFieldManager;

/**
 * Builds the form to delete Field inheritance entities.
 */
class FieldInheritanceDeleteForm extends EntityConfirmFormBase {

  /**
   * The messenger service.
   *
   * @var \Drupal\Core\Messenger\Messenger
   */
  protected $messenger;

  /**
   * The entity field manager service.
   *
   * @var \Drupal\Core\Entity\EntityFieldManager
   */
  protected $entityFieldManager;

  /**
   * Construct an EventSeriesDeleteForm.
   *
   * @param \Drupal\Core\Messenger\Messenger $messenger
   *   The messenger service.
   * @param \Drupal\Core\Entity\EntityFieldManager $entity_field_manager
   *   The entity field manager service.
   */
  public function __construct(Messenger $messenger, EntityFieldManager $entity_field_manager) {
    $this->messenger = $messenger;
    $this->entityFieldManager = $entity_field_manager;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('messenger'), $container
      ->get('entity_field.manager'));
  }

  /**
   * {@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.field_inheritance.collection');
  }

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

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $this->entity
      ->delete();
    $this->messenger
      ->addMessage($this
      ->t('content @type: deleted @label.', [
      '@type' => $this->entity
        ->bundle(),
      '@label' => $this->entity
        ->label(),
    ]));
    $this->entityFieldManager
      ->clearCachedFieldDefinitions();
    $form_state
      ->setRedirectUrl($this
      ->getCancelUrl());
  }

}

Classes

Namesort descending Description
FieldInheritanceDeleteForm Builds the form to delete Field inheritance entities.