xtcentity-2.x-dev/src/Form/XtcEntityDeleteForm.php
src/Form/XtcEntityDeleteForm.php
<?php
namespace Drupal\xtcentity\Form;
use Drupal\Core\Entity\EntityConfirmFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
/**
* Builds the form to delete XTC Entity optionset entities.
*/
class XtcEntityDeleteForm extends EntityConfirmFormBase {
/**
* {@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.' . $this->entity->getEntityType()->id() . '.collection');
}
/**
* {@inheritdoc}
*/
public function getConfirmText() {
return $this->t('Delete');
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$this->entity->delete();
$this->messenger()->addStatus(
$this->t('Deleted the %label %entitylabel.',
[
'%label' => $this->entity->label(),
'%entitylabel' => $this->entity->getEntityType()->getLabel(),
]
)
);
$form_state->setRedirectUrl($this->getCancelUrl());
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$form = parent::buildForm($form, $form_state);
if ($this->entity->id() == 'default') {
$form['#title'] = $this->t('The default optionset cannot be deleted.');
$form['description'] = ['#markup' => t('Please click Cancel to go back to the list of optionsets.')];
$form['actions']['submit']['#access'] = FALSE;
}
return $form;
}
}
