SimpleMegaMenuTypeDeleteForm.php in Simple Mega Menu 2.0.x
File
src/Form/SimpleMegaMenuTypeDeleteForm.php
View source
<?php
namespace Drupal\simple_megamenu\Form;
use Drupal\Core\Entity\EntityDeleteForm;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
class SimpleMegaMenuTypeDeleteForm extends EntityDeleteForm {
public function getQuestion() {
return $this
->t('Are you sure you want to delete %name?', [
'%name' => $this->entity
->label(),
]);
}
public function getCancelUrl() {
return new Url('entity.simple_mega_menu_type.collection');
}
public function getConfirmText() {
return $this
->t('Delete');
}
public function buildForm(array $form, FormStateInterface $form_state) {
$entities = $this->entityTypeManager
->getStorage('simple_mega_menu')
->getQuery()
->condition('type', $this->entity
->id())
->count()
->execute();
if ($entities) {
$caption = '<p>' . $this
->formatPlural($entities, '%type is used by 1 piece of content on your site. You can not remove this content type until you have removed all of the %type content.', '%type is used by @count pieces of content on your site. You may not remove %type until you have removed all of the %type content.', [
'%type' => $this->entity
->label(),
]) . '</p>';
$form['#title'] = $this
->getQuestion();
$form['description'] = [
'#markup' => $caption,
];
return $form;
}
return parent::buildForm($form, $form_state);
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$this->entity
->delete();
$this
->messenger()
->addStatus($this
->t('content @type: deleted @label.', [
'@type' => $this->entity
->bundle(),
'@label' => $this->entity
->label(),
]));
$form_state
->setRedirectUrl($this
->getCancelUrl());
}
}