Commit c309e0e5 authored by webchick's avatar webchick

Issue #1938600 by tim.plunkett: Add a FormInterface replacement for confirm_form().

parent 9f0a5320
<?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) {
}
}
<?php
/**
* @file
* Contains \Drupal\system\Tests\Form\ConfirmFormTest.
*/
namespace Drupal\system\Tests\Form;
use Drupal\simpletest\WebTestBase;
/**
* Tests confirmation forms.
*/
class ConfirmFormTest extends WebTestBase {
/**
* Modules to enable.
*
* @var array
*/
public static $modules = array('form_test');
public static function getInfo() {
return array(
'name' => 'Confirmation forms',
'description' => 'Tests confirmation forms.',
'group' => 'Form API',
);
}
function testConfirmForm() {
// Test the building of the form.
$this->drupalGet('form-test/confirm-form');
$site_name = $this->container->get('config.factory')->get('system.site')->get('name');
$this->assertTitle(t('ConfirmFormTestForm::getQuestion(). | @site-name', array('@site-name' => $site_name)), 'The question was found as the page title.');
$this->assertText(t('ConfirmFormTestForm::getDescription().'), 'The description was used.');
$this->assertFieldByXPath('//input[@id="edit-submit"]', t('ConfirmFormTestForm::getConfirmText().'), 'The confirm text was used.');
// Test canelling the form.
$this->clickLink(t('ConfirmFormTestForm::getCancelText().'));
$this->assertUrl('admin', array(), "The form's cancel link was followed.");
// Test submitting the form.
$this->drupalPost('form-test/confirm-form', NULL, t('ConfirmFormTestForm::getConfirmText().'));
$this->assertText('The ConfirmFormTestForm::submitForm() method was used for this form.');
$this->assertUrl('', array(), "The form's redirect was followed.");
// Test submitting the form with a destination.
$this->drupalPost('form-test/confirm-form', NULL, t('ConfirmFormTestForm::getConfirmText().'), array('query' => array('destination' => 'admin/config')));
$this->assertUrl('admin/config', array(), "The form's redirect was not followed, the destination query string was followed.");
// Test cancelling the form with a complex destination.
$this->drupalGet('form-test/confirm-form-array-path');
$this->clickLink(t('ConfirmFormArrayPathTestForm::getCancelText().'));
$this->assertUrl('admin', array('query' => array('destination' => 'admin/config')), "The form's complex cancel link was followed.");
}
}
......@@ -3387,6 +3387,8 @@ function _system_sort_requirements($a, $b) {
*
* @return
* The form array.
*
* @deprecated Use \Drupal\Core\Form\ConfirmFormBase instead.
*/
function confirm_form($form, $question, $path, $description = NULL, $yes = NULL, $no = NULL, $name = 'confirm') {
$description = isset($description) ? $description : t('This action cannot be undone.');
......
......@@ -32,3 +32,17 @@ form_test.route5:
_form: '\Drupal\form_test\SystemConfigFormTestForm'
requirements:
_access: 'TRUE'
form_test.route6:
pattern: '/form-test/confirm-form'
defaults:
_form: '\Drupal\form_test\ConfirmFormTestForm'
requirements:
_access: 'TRUE'
form_test.route7:
pattern: '/form-test/confirm-form-array-path'
defaults:
_form: '\Drupal\form_test\ConfirmFormArrayPathTestForm'
requirements:
_access: 'TRUE'
<?php
/**
* @file
* Contains \Drupal\form_test\ConfirmFormArrayPathTestForm.
*/
namespace Drupal\form_test;
/**
* Provides a test confirmation form with a complex cancellation destination.
*/
class ConfirmFormArrayPathTestForm extends ConfirmFormTestForm {
/**
* Overrides \Drupal\form_test\ConfirmFormTestForm::getFormID().
*/
public function getFormID() {
return 'form_test_confirm_array_path_test_form';
}
/**
* Overrides \Drupal\form_test\ConfirmFormTestForm::getCancelPath().
*/
protected function getCancelPath() {
return array(
'path' => 'admin',
'query' => array(
'destination' => 'admin/config',
),
);
}
/**
* Overrides \Drupal\form_test\ConfirmFormTestForm::getCancelText().
*/
protected function getCancelText() {
return t('ConfirmFormArrayPathTestForm::getCancelText().');
}
}
<?php
/**
* @file
* Contains \Drupal\form_test\ConfirmFormTestForm.
*/
namespace Drupal\form_test;
use Drupal\Core\Form\ConfirmFormBase;
/**
* Provides a test confirmation form.
*/
class ConfirmFormTestForm extends ConfirmFormBase {
/**
* Implements \Drupal\Core\Form\FormInterface::getFormID().
*/
public function getFormID() {
return 'form_test_confirm_test_form';
}
/**
* Implements \Drupal\Core\Form\ConfirmFormBase::getQuestion().
*/
protected function getQuestion() {
return t('ConfirmFormTestForm::getQuestion().');
}
/**
* Implements \Drupal\Core\Form\ConfirmFormBase::getCancelPath().
*/
protected function getCancelPath() {
return 'admin';
}
/**
* Overrides \Drupal\Core\Form\ConfirmFormBase::getDescription().
*/
protected function getDescription() {
return t('ConfirmFormTestForm::getDescription().');
}
/**
* Overrides \Drupal\Core\Form\ConfirmFormBase::getConfirmText().
*/
protected function getConfirmText() {
return t('ConfirmFormTestForm::getConfirmText().');
}
/**
* Overrides \Drupal\Core\Form\ConfirmFormBase::getCancelText().
*/
protected function getCancelText() {
return t('ConfirmFormTestForm::getCancelText().');
}
/**
* Overrides \Drupal\Core\Form\ConfirmFormBase::buildForm().
*/
public function buildForm(array $form, array &$form_state) {
$form['element'] = array('#markup' => '<p>The ConfirmFormTestForm::buildForm() method was used for this form.</p>');
return parent::buildForm($form, $form_state);
}
/**
* Implements \Drupal\Core\Form\FormInterface::submitForm().
*/
public function submitForm(array &$form, array &$form_state) {
drupal_set_message(t('The ConfirmFormTestForm::submitForm() method was used for this form.'));
$form_state['redirect'] = '';
}
}
......@@ -10,7 +10,7 @@
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\ControllerInterface;
use Drupal\Core\Form\FormInterface;
use Drupal\Core\Form\ConfirmFormBase;
use Drupal\views\ViewStorageInterface;
use Drupal\Core\Entity\EntityManager;
use Drupal\user\TempStoreFactory;
......@@ -18,7 +18,7 @@
/**
* Builds the form to break the lock of an edited view.
*/
class BreakLockForm implements FormInterface, ControllerInterface {
class BreakLockForm extends ConfirmFormBase implements ControllerInterface {
/**
* Stores the Entity manager.
......@@ -34,6 +34,13 @@ class BreakLockForm implements FormInterface, ControllerInterface {
*/
protected $tempStore;
/**
* The view being deleted.
*
* @var \Drupal\views\ViewStorageInterface
*/
protected $view;
/**
* Constructs a \Drupal\views_ui\Form\BreakLockForm object.
*
......@@ -65,39 +72,53 @@ public function getFormID() {
}
/**
* Implements \Drupal\Core\Form\FormInterface::buildForm().
* Implements \Drupal\Core\Form\ConfirmFormBase::getQuestion().
*/
public function buildForm(array $form, array &$form_state, ViewStorageInterface $view = NULL) {
$form_state['view_id'] = $view->id();
$locked = $this->tempStore->getMetadata($form_state['view_id']);
if (!$locked) {
$form['message']['#markup'] = t('There is no lock on view %name to break.', array('%name' => $form_state['view_id']));
return $form;
}
protected function getQuestion() {
return t('Do you want to break the lock on view %name?', array('%name' => $this->view->id()));
}
/**
* Implements \Drupal\Core\Form\ConfirmFormBase::getDescription().
*/
protected function getDescription() {
$locked = $this->tempStore->getMetadata($this->view->id());
$account = $this->entityManager->getStorageController('user')->load(array($locked->owner));
return confirm_form($form,
t('Do you want to break the lock on view %name?', array('%name' => $form_state['view_id'])),
'admin/structure/views/view/' . $form_state['view_id'],
t('By breaking this lock, any unsaved changes made by !user will be lost.', array('!user' => theme('username', array('account' => reset($account))))),
t('Break lock'),
t('Cancel')
);
return t('By breaking this lock, any unsaved changes made by !user will be lost.', array('!user' => theme('username', array('account' => reset($account)))));
}
/**
* Implements \Drupal\Core\Form\FormInterface::validateForm().
* Implements \Drupal\Core\Form\ConfirmFormBase::getCancelPath().
*/
public function validateForm(array &$form, array &$form_state) {
protected function getCancelPath() {
return 'admin/structure/views/view/' . $this->view->id();
}
/**
* Implements \Drupal\Core\Form\ConfirmFormBase::getConfirmText().
*/
protected function getConfirmText() {
return t('Break lock');
}
/**
* Implements \Drupal\Core\Form\FormInterface::buildForm().
*/
public function buildForm(array $form, array &$form_state, ViewStorageInterface $view = NULL) {
$this->view = $view;
if (!$this->tempStore->getMetadata($this->view->id())) {
$form['message']['#markup'] = t('There is no lock on view %name to break.', array('%name' => $this->view->id()));
return $form;
}
return parent::buildForm($form, $form_state);
}
/**
* Implements \Drupal\Core\Form\FormInterface::submitForm().
*/
public function submitForm(array &$form, array &$form_state) {
$this->tempStore->delete($form_state['view_id']);
$form_state['redirect'] = 'admin/structure/views/view/' . $form_state['view_id'];
$this->tempStore->delete($this->view->id());
$form_state['redirect'] = 'admin/structure/views/view/' . $this->view->id();
drupal_set_message(t('The lock has been broken and you may now edit this view.'));
}
......
......@@ -7,13 +7,41 @@
namespace Drupal\views_ui\Form;
use Drupal\Core\Form\FormInterface;
use Drupal\Core\Form\ConfirmFormBase;
use Drupal\views\ViewStorageInterface;
/**
* Builds the form to delete a view.
*/
class DeleteForm implements FormInterface {
class DeleteForm extends ConfirmFormBase {
/**
* The view being deleted.
*
* @var \Drupal\views\ViewStorageInterface
*/
protected $view;
/**
* Implements \Drupal\Core\Form\ConfirmFormBase::getQuestion().
*/
protected function getQuestion() {
return t('Are you sure you want to delete the %name view?', array('%name' => $this->view->getHumanName()));
}
/**
* Implements \Drupal\Core\Form\ConfirmFormBase::getCancelPath().
*/
protected function getCancelPath() {
return 'admin/structure/views';
}
/**
* Implements \Drupal\Core\Form\ConfirmFormBase::getConfirmText().
*/
protected function getConfirmText() {
return t('Delete');
}
/**
* Implements \Drupal\Core\Form\FormInterface::getFormID().
......@@ -26,14 +54,8 @@ public function getFormID() {
* Implements \Drupal\Core\Form\FormInterface::buildForm().
*/
public function buildForm(array $form, array &$form_state, ViewStorageInterface $view = NULL) {
$form_state['view'] = $view;
return confirm_form($form,
t('Are you sure you want to delete the %name view?', array('%name' => $view->getHumanName())),
'admin/structure/views',
t('This action cannot be undone.'),
t('Delete'),
t('Cancel')
);
$this->view = $view;
return parent::buildForm($form, $form_state);
}
/**
......@@ -46,7 +68,7 @@ public function validateForm(array &$form, array &$form_state) {
* Implements \Drupal\Core\Form\FormInterface::submitForm().
*/
public function submitForm(array &$form, array &$form_state) {
$form_state['view']->delete();
$this->view->delete();
$form_state['redirect'] = 'admin/structure/views';
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment