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;
class IngredientDeleteForm extends ContentEntityConfirmFormBase {
protected $loggerChannel;
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;
}
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'));
}
public function getQuestion() {
return $this
->t('Are you sure you want to delete ingredient %name?', [
'%name' => $this->entity
->label(),
]);
}
public function getCancelUrl() {
return new Url('ingredient.admin');
}
public function getConfirmText() {
return $this
->t('Delete');
}
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');
}
}