ContentEntityConfirmFormBase.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. namespace Drupal\Core\Entity;
  3. use Drupal\Core\Form\ConfirmFormHelper;
  4. use Drupal\Core\Form\ConfirmFormInterface;
  5. use Drupal\Core\Form\FormStateInterface;
  6. /**
  7. * Provides a generic base class for an entity-based confirmation form.
  8. */
  9. abstract class ContentEntityConfirmFormBase extends ContentEntityForm implements ConfirmFormInterface {
  10. /**
  11. * {@inheritdoc}
  12. */
  13. public function getBaseFormId() {
  14. return $this->entity->getEntityTypeId() . '_confirm_form';
  15. }
  16. /**
  17. * {@inheritdoc}
  18. */
  19. public function getDescription() {
  20. return $this->t('This action cannot be undone.');
  21. }
  22. /**
  23. * {@inheritdoc}
  24. */
  25. public function getConfirmText() {
  26. return $this->t('Confirm');
  27. }
  28. /**
  29. * {@inheritdoc}
  30. */
  31. public function getCancelText() {
  32. return $this->t('Cancel');
  33. }
  34. /**
  35. * {@inheritdoc}
  36. */
  37. public function getFormName() {
  38. return 'confirm';
  39. }
  40. /**
  41. * {@inheritdoc}
  42. */
  43. public function buildForm(array $form, FormStateInterface $form_state) {
  44. $form = parent::buildForm($form, $form_state);
  45. $form['#title'] = $this->getQuestion();
  46. $form['#attributes']['class'][] = 'confirmation';
  47. $form['description'] = ['#markup' => $this->getDescription()];
  48. $form[$this->getFormName()] = ['#type' => 'hidden', '#value' => 1];
  49. // By default, render the form using theme_confirm_form().
  50. if (!isset($form['#theme'])) {
  51. $form['#theme'] = 'confirm_form';
  52. }
  53. return $form;
  54. }
  55. /**
  56. * {@inheritdoc}
  57. */
  58. public function form(array $form, FormStateInterface $form_state) {
  59. // Do not attach fields to the confirm form.
  60. return $form;
  61. }
  62. /**
  63. * {@inheritdoc}
  64. */
  65. protected function actions(array $form, FormStateInterface $form_state) {
  66. return [
  67. 'submit' => [
  68. '#type' => 'submit',
  69. '#value' => $this->getConfirmText(),
  70. '#submit' => [
  71. [$this, 'submitForm'],
  72. ],
  73. ],
  74. 'cancel' => ConfirmFormHelper::buildCancelLink($this, $this->getRequest()),
  75. ];
  76. }
  77. /**
  78. * {@inheritdoc}
  79. *
  80. * The save() method is not used in ContentEntityConfirmFormBase. This
  81. * overrides the default implementation that saves the entity.
  82. *
  83. * Confirmation forms should override submitForm() instead for their logic.
  84. */
  85. public function save(array $form, FormStateInterface $form_state) {}
  86. /**
  87. * {@inheritdoc}
  88. *
  89. * The delete() method is not used in ContentEntityConfirmFormBase. This
  90. * overrides the default implementation that redirects to the delete-form
  91. * confirmation form.
  92. *
  93. * Confirmation forms should override submitForm() instead for their logic.
  94. */
  95. public function delete(array $form, FormStateInterface $form_state) {}
  96. /**
  97. * {@inheritdoc}
  98. */
  99. public function validateForm(array &$form, FormStateInterface $form_state) {
  100. // Override the default validation implementation as it is not necessary
  101. // nor possible to validate an entity in a confirmation form.
  102. return $this->entity;
  103. }
  104. }