EntityConfirmFormBase.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. * @ingroup entity_api
  10. */
  11. abstract class EntityConfirmFormBase extends EntityForm implements ConfirmFormInterface {
  12. /**
  13. * {@inheritdoc}
  14. */
  15. public function getBaseFormId() {
  16. return $this->entity->getEntityTypeId() . '_confirm_form';
  17. }
  18. /**
  19. * {@inheritdoc}
  20. */
  21. public function getDescription() {
  22. return $this->t('This action cannot be undone.');
  23. }
  24. /**
  25. * {@inheritdoc}
  26. */
  27. public function getConfirmText() {
  28. return $this->t('Confirm');
  29. }
  30. /**
  31. * {@inheritdoc}
  32. */
  33. public function getCancelText() {
  34. return $this->t('Cancel');
  35. }
  36. /**
  37. * {@inheritdoc}
  38. */
  39. public function getFormName() {
  40. return 'confirm';
  41. }
  42. /**
  43. * {@inheritdoc}
  44. */
  45. public function buildForm(array $form, FormStateInterface $form_state) {
  46. $form = parent::buildForm($form, $form_state);
  47. $form['#title'] = $this->getQuestion();
  48. $form['#attributes']['class'][] = 'confirmation';
  49. $form['description'] = ['#markup' => $this->getDescription()];
  50. $form[$this->getFormName()] = ['#type' => 'hidden', '#value' => 1];
  51. // By default, render the form using theme_confirm_form().
  52. if (!isset($form['#theme'])) {
  53. $form['#theme'] = 'confirm_form';
  54. }
  55. return $form;
  56. }
  57. /**
  58. * {@inheritdoc}
  59. */
  60. protected function actions(array $form, FormStateInterface $form_state) {
  61. return [
  62. 'submit' => [
  63. '#type' => 'submit',
  64. '#value' => $this->getConfirmText(),
  65. '#submit' => [
  66. [$this, 'submitForm'],
  67. ],
  68. ],
  69. 'cancel' => ConfirmFormHelper::buildCancelLink($this, $this->getRequest()),
  70. ];
  71. }
  72. /**
  73. * {@inheritdoc}
  74. *
  75. * The save() method is not used in EntityConfirmFormBase. This overrides the
  76. * default implementation that saves the entity.
  77. *
  78. * Confirmation forms should override submitForm() instead for their logic.
  79. */
  80. public function save(array $form, FormStateInterface $form_state) {}
  81. /**
  82. * {@inheritdoc}
  83. *
  84. * The delete() method is not used in EntityConfirmFormBase. This overrides
  85. * the default implementation that redirects to the delete-form confirmation
  86. * form.
  87. *
  88. * Confirmation forms should override submitForm() instead for their logic.
  89. */
  90. public function delete(array $form, FormStateInterface $form_state) {}
  91. }