ConfirmFormBase.php 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. <?php
  2. namespace Drupal\Core\Form;
  3. /**
  4. * Provides an generic base class for a confirmation form.
  5. */
  6. abstract class ConfirmFormBase extends FormBase implements ConfirmFormInterface {
  7. /**
  8. * {@inheritdoc}
  9. */
  10. public function getDescription() {
  11. return $this->t('This action cannot be undone.');
  12. }
  13. /**
  14. * {@inheritdoc}
  15. */
  16. public function getConfirmText() {
  17. return $this->t('Confirm');
  18. }
  19. /**
  20. * {@inheritdoc}
  21. */
  22. public function getCancelText() {
  23. return $this->t('Cancel');
  24. }
  25. /**
  26. * {@inheritdoc}
  27. */
  28. public function getFormName() {
  29. return 'confirm';
  30. }
  31. /**
  32. * {@inheritdoc}
  33. */
  34. public function buildForm(array $form, FormStateInterface $form_state) {
  35. $form['#title'] = $this->getQuestion();
  36. $form['#attributes']['class'][] = 'confirmation';
  37. $form['description'] = ['#markup' => $this->getDescription()];
  38. $form[$this->getFormName()] = ['#type' => 'hidden', '#value' => 1];
  39. $form['actions'] = ['#type' => 'actions'];
  40. $form['actions']['submit'] = [
  41. '#type' => 'submit',
  42. '#value' => $this->getConfirmText(),
  43. '#button_type' => 'primary',
  44. ];
  45. $form['actions']['cancel'] = ConfirmFormHelper::buildCancelLink($this, $this->getRequest());
  46. // By default, render the form using theme_confirm_form().
  47. if (!isset($form['#theme'])) {
  48. $form['#theme'] = 'confirm_form';
  49. }
  50. return $form;
  51. }
  52. }