ConfirmFormBase.php 3.03 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
<?php

/**
 * @file
 * Contains \Drupal\Core\Form\ConfirmFormBase.
 */

namespace Drupal\Core\Form;

/**
 * Provides an generic base class for a confirmation form.
 */
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.
   */
  abstract protected 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.
   */
  abstract protected 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) {
  }

}