ConfirmFormBase.php 1.85 KB
Newer Older
1 2 3 4 5 6 7 8 9
<?php

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

namespace Drupal\Core\Form;

10
use Drupal\Component\Utility\Url;
11

12 13 14
/**
 * Provides an generic base class for a confirmation form.
 */
15
abstract class ConfirmFormBase extends FormBase implements ConfirmFormInterface {
16 17

  /**
18
   * {@inheritdoc}
19
   */
20
  public function getDescription() {
21 22 23 24
    return t('This action cannot be undone.');
  }

  /**
25
   * {@inheritdoc}
26
   */
27
  public function getConfirmText() {
28 29 30 31
    return t('Confirm');
  }

  /**
32
   * {@inheritdoc}
33
   */
34
  public function getCancelText() {
35 36 37 38
    return t('Cancel');
  }

  /**
39
   * {@inheritdoc}
40
   */
41
  public function getFormName() {
42 43 44 45
    return 'confirm';
  }

  /**
46
   * {@inheritdoc}
47
   */
48
  public function buildForm(array $form, array &$form_state) {
49 50
    $path = $this->getCancelPath();
    // Prepare cancel link.
51 52 53
    $query = $this->getRequest()->query;
    if ($query->has('destination')) {
      $options = Url::parse($query->get('destination'));
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
    }
    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;
  }

}