abstract class ConfirmFormBase

Provides an generic base class for a confirmation form.

Hierarchy

Expanded class hierarchy of ConfirmFormBase

29 files declare their use of ConfirmFormBase
AdminBlockDeleteForm.php in drupal/core/modules/block/lib/Drupal/block/Form/AdminBlockDeleteForm.php
Contains \Drupal\block\Form\AdminBlockDeleteForm.
BanDelete.php in drupal/core/modules/ban/lib/Drupal/ban/Form/BanDelete.php
Contains \Drupal\ban\Form\BanDelete.
BreakLockForm.php in drupal/core/modules/views_ui/lib/Drupal/views_ui/Form/BreakLockForm.php
Contains \Drupal\views_ui\Form\BreakLockForm.
ConfigTestDeleteForm.php in drupal/core/modules/config/tests/config_test/lib/Drupal/config_test/Form/ConfigTestDeleteForm.php
Contains \Drupal\config_test\Form\ConfigTestDeleteForm.
ConfirmFormTestForm.php in drupal/core/modules/system/tests/modules/form_test/lib/Drupal/form_test/ConfirmFormTestForm.php
Contains \Drupal\form_test\ConfirmFormTestForm.

... See full list

File

drupal/core/lib/Drupal/Core/Form/ConfirmFormBase.php, line 13
Contains \Drupal\Core\Form\ConfirmFormBase.

Namespace

Drupal\Core\Form
View source
abstract class ConfirmFormBase implements FormInterface {

  /**
   * Returns the question to ask the user.
   *
   * @return string
   *   The form question. The page title will be set to this value.
   */
  protected abstract function getQuestion();

  /**
   * Returns the page to go to if the user cancels the action.
   *
   * @return string|array
   *   This can be either:
   *   - A string containing a Drupal path.
   *   - An associative array with a 'path' key. Additional array values are
   *     passed as the $options parameter to l().
   *   If the 'destination' query parameter is set in the URL when viewing a
   *   confirmation form, that value will be used instead of this path.
   */
  protected abstract function getCancelPath();

  /**
   * Returns additional text to display as a description.
   *
   * @return string
   *   The form description.
   */
  protected function getDescription() {
    return t('This action cannot be undone.');
  }

  /**
   * Returns a caption for the button that confirms the action.
   *
   * @return string
   *   The form confirmation text.
   */
  protected function getConfirmText() {
    return t('Confirm');
  }

  /**
   * Returns a caption for the link which cancels the action.
   *
   * @return string
   *   The form cancellation text.
   */
  protected function getCancelText() {
    return t('Cancel');
  }

  /**
   * Returns the internal name used to refer to the confirmation item.
   *
   * @return string
   *   The internal form name.
   */
  protected function getFormName() {
    return 'confirm';
  }

  /**
   * Implements \Drupal\Core\Form\FormInterface::buildForm().
   */
  public function buildForm(array $form, array &$form_state) {
    $path = $this
      ->getCancelPath();

    // Prepare cancel link.
    if (isset($_GET['destination'])) {
      $options = drupal_parse_url($_GET['destination']);
    }
    elseif (is_array($path)) {
      $options = $path;
    }
    else {
      $options = array(
        'path' => $path,
      );
    }
    drupal_set_title($this
      ->getQuestion(), PASS_THROUGH);
    $form['#attributes']['class'][] = 'confirmation';
    $form['description'] = array(
      '#markup' => $this
        ->getDescription(),
    );
    $form[$this
      ->getFormName()] = array(
      '#type' => 'hidden',
      '#value' => 1,
    );
    $form['actions'] = array(
      '#type' => 'actions',
    );
    $form['actions']['submit'] = array(
      '#type' => 'submit',
      '#value' => $this
        ->getConfirmText(),
    );
    $form['actions']['cancel'] = array(
      '#type' => 'link',
      '#title' => $this
        ->getCancelText(),
      '#href' => $options['path'],
      '#options' => $options,
    );

    // By default, render the form using theme_confirm_form().
    if (!isset($form['#theme'])) {
      $form['#theme'] = 'confirm_form';
    }
    return $form;
  }

  /**
   * Implements \Drupal\Core\Form\FormInterface::validateForm().
   */
  public function validateForm(array &$form, array &$form_state) {
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ConfirmFormBase::buildForm public function Implements \Drupal\Core\Form\FormInterface::buildForm(). Overrides FormInterface::buildForm 28
ConfirmFormBase::getCancelPath abstract protected function Returns the page to go to if the user cancels the action. 29
ConfirmFormBase::getCancelText protected function Returns a caption for the link which cancels the action. 2
ConfirmFormBase::getConfirmText protected function Returns a caption for the button that confirms the action. 27
ConfirmFormBase::getDescription protected function Returns additional text to display as a description. 10
ConfirmFormBase::getFormName protected function Returns the internal name used to refer to the confirmation item.
ConfirmFormBase::getQuestion abstract protected function Returns the question to ask the user. 29
ConfirmFormBase::validateForm public function Implements \Drupal\Core\Form\FormInterface::validateForm(). Overrides FormInterface::validateForm 1
FormInterface::getFormID public function Returns a unique string identifying the form. 90
FormInterface::submitForm public function Form submission handler. 55