3 namespace Drupal\Core\Entity;
5 use Drupal\Core\Form\ConfirmFormHelper;
6 use Drupal\Core\Form\ConfirmFormInterface;
7 use Drupal\Core\Form\FormStateInterface;
10 * Provides a generic base class for an entity-based confirmation form.
12 abstract class ContentEntityConfirmFormBase extends ContentEntityForm implements ConfirmFormInterface {
17 public function getBaseFormId() {
18 return $this->entity->getEntityTypeId() . '_confirm_form';
24 public function getDescription() {
25 return $this->t('This action cannot be undone.');
31 public function getConfirmText() {
32 return $this->t('Confirm');
38 public function getCancelText() {
39 return $this->t('Cancel');
45 public function getFormName() {
52 public function buildForm(array $form, FormStateInterface $form_state) {
53 $form = parent::buildForm($form, $form_state);
55 $form['#title'] = $this->getQuestion();
57 $form['#attributes']['class'][] = 'confirmation';
58 $form['description'] = ['#markup' => $this->getDescription()];
59 $form[$this->getFormName()] = ['#type' => 'hidden', '#value' => 1];
61 // By default, render the form using theme_confirm_form().
62 if (!isset($form['#theme'])) {
63 $form['#theme'] = 'confirm_form';
71 public function form(array $form, FormStateInterface $form_state) {
72 // Do not attach fields to the confirm form.
79 protected function actions(array $form, FormStateInterface $form_state) {
83 '#value' => $this->getConfirmText(),
85 [$this, 'submitForm'],
88 'cancel' => ConfirmFormHelper::buildCancelLink($this, $this->getRequest()),
95 * The save() method is not used in ContentEntityConfirmFormBase. This
96 * overrides the default implementation that saves the entity.
98 * Confirmation forms should override submitForm() instead for their logic.
100 public function save(array $form, FormStateInterface $form_state) {}
105 * The delete() method is not used in ContentEntityConfirmFormBase. This
106 * overrides the default implementation that redirects to the delete-form
109 * Confirmation forms should override submitForm() instead for their logic.
111 public function delete(array $form, FormStateInterface $form_state) {}
116 public function validateForm(array &$form, FormStateInterface $form_state) {
117 // Override the default validation implementation as it is not necessary
118 // nor possible to validate an entity in a confirmation form.
119 return $this->entity;