FlagForListDeleteForm.php in Flag Lists 4.0.x
File
src/Form/FlagForListDeleteForm.php
View source
<?php
namespace Drupal\flag_lists\Form;
use Drupal\Core\Entity\EntityConfirmFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
class FlagForListDeleteForm extends EntityConfirmFormBase {
public function buildForm(array $form, FormStateInterface $form_state) {
$flagListService = \Drupal::service('flaglists');
$flaggingCollections = $flagListService
->getAllFlaggingCollections();
$output = [];
foreach ($flaggingCollections as $flag) {
if ($flag
->getBaseFlag()
->id() == $this->entity
->id()) {
$output[] = $flag
->getName();
}
}
if (!empty($output)) {
$form['active_items'] = [
'#theme' => 'item_list',
'#list_type' => 'ul',
'#items' => $output,
'#title' => $this
->t('Please remove the following Flagging Collections before proceding:'),
'#empty' => $this
->t('No Flag List Items found'),
];
}
$form = parent::buildForm($form, $form_state);
if (!empty($output)) {
unset($form['actions']['submit']);
unset($form['description']);
}
return $form;
}
public function getQuestion() {
return $this
->t('Are you sure you want to delete %name and the corresponding Flag?', [
'%name' => $this->entity
->label(),
]);
}
public function getCancelUrl() {
return new Url('entity.flag_for_list.collection');
}
public function getConfirmText() {
return $this
->t('Delete');
}
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(),
]));
$form_state
->setRedirectUrl($this
->getCancelUrl());
}
}