You are here

IngredientDeleteForm.php in Recipe 8.2

File

modules/ingredient/src/Form/IngredientDeleteForm.php
View source
<?php

namespace Drupal\ingredient\Form;

use Drupal\Component\Datetime\TimeInterface;
use Drupal\Core\Entity\ContentEntityConfirmFormBase;
use Drupal\Core\Entity\EntityRepositoryInterface;
use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Logger\LoggerChannelInterface;
use Drupal\Core\Url;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Provides a form for deleting an ingredient.
 */
class IngredientDeleteForm extends ContentEntityConfirmFormBase {

  /**
   * The ingredient logger channel.
   *
   * @var \Drupal\Core\Logger\LoggerChannelInterface
   */
  protected $loggerChannel;

  /**
   * Constructs a new IngredientDeleteForm object.
   *
   * @param \Drupal\Core\Entity\EntityRepositoryInterface $entity_repository
   *   The entity manager.
   * @param \Drupal\Core\Entity\EntityTypeBundleInfoInterface $entity_type_bundle_info
   *   The entity type bundle service.
   * @param \Drupal\Component\Datetime\TimeInterface $time
   *   The time service.
   * @param \Drupal\Core\Logger\LoggerChannelInterface $logger_channel
   *   The logger service.
   */
  public function __construct(EntityRepositoryInterface $entity_repository, EntityTypeBundleInfoInterface $entity_type_bundle_info, TimeInterface $time, LoggerChannelInterface $logger_channel) {
    parent::__construct($entity_repository, $entity_type_bundle_info, $time);
    $this->loggerChannel = $logger_channel;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('entity.repository'), $container
      ->get('entity_type.bundle.info'), $container
      ->get('datetime.time'), $container
      ->get('logger.factory')
      ->get('ingredient'));
  }

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

  /**
   * {@inheritdoc}
   *
   * If the delete command is canceled, return to the ingredient list.
   */
  public function getCancelUrl() {
    return new Url('ingredient.admin');
  }

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

  /**
   * {@inheritdoc}
   *
   * Delete the ingredient and log the event.
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $entity = $this
      ->getEntity();
    $entity
      ->delete();
    $this->loggerChannel
      ->notice('@type: deleted %title.', [
      '@type' => $this->entity
        ->bundle(),
      '%title' => $this->entity
        ->label(),
    ]);
    $form_state
      ->setRedirect('ingredient.admin');
  }

}

Classes

Namesort descending Description
IngredientDeleteForm Provides a form for deleting an ingredient.