Commit ca7ae599 authored by webchick's avatar webchick

Issue #1963394 by tim.plunkett, jibran, effulgentsia: Fixed...

Issue #1963394 by tim.plunkett, jibran, effulgentsia: Fixed ConfirmFormBase::getCancelPath() should allow for a route.
parent 670e43b5
......@@ -7,7 +7,7 @@
namespace Drupal\Core\Entity;
use Drupal\Component\Utility\Url;
use Drupal\Core\Form\ConfirmFormHelper;
use Drupal\Core\Form\ConfirmFormInterface;
use Symfony\Component\HttpFoundation\Request;
......@@ -85,24 +85,8 @@ protected function actions(array $form, array &$form_state) {
$actions['submit']['#value'] = $this->getConfirmText();
unset($actions['delete']);
$path = $this->getCancelPath();
// Prepare cancel link.
$query = $this->getRequest()->query;
if ($query->has('destination')) {
$options = Url::parse($query->get('destination'));
}
elseif (is_array($path)) {
$options = $path;
}
else {
$options = array('path' => $path);
}
$actions['cancel'] = array(
'#type' => 'link',
'#title' => $this->getCancelText(),
'#href' => $options['path'],
'#options' => $options,
);
$actions['cancel'] = ConfirmFormHelper::buildCancelLink($this, $this->getRequest());
return $actions;
}
......
......@@ -410,6 +410,33 @@ public function getAdminPath($entity_type, $bundle) {
return $admin_path;
}
/**
* Returns the route information for an entity type's bundle.
*
* @param string $entity_type
* The entity type.
* @param string $bundle
* The name of the bundle.
*
* @return array
* An associative array with the following keys:
* - route_name: The name of the route.
* - route_parameters: (optional) An associative array of parameter names
* and values.
*/
public function getAdminRouteInfo($entity_type, $bundle) {
$entity_info = $this->getDefinition($entity_type);
if (isset($entity_info['bundle_prefix'])) {
$bundle = str_replace($entity_info['bundle_prefix'], '', $bundle);
}
return array(
'route_name' => 'field_ui.overview.' . $entity_type,
'route_parameters' => array(
'bundle' => $bundle,
)
);
}
/**
* Gets an array of entity field definitions.
*
......
......@@ -7,7 +7,7 @@
namespace Drupal\Core\Entity;
use Drupal\Component\Utility\Url;
use Drupal\Core\Form\ConfirmFormHelper;
use Drupal\Core\Form\ConfirmFormInterface;
/**
......@@ -92,24 +92,9 @@ protected function actions(array $form, array &$form_state) {
$actions['submit']['#value'] = $this->getConfirmText();
unset($actions['delete']);
$path = $this->getCancelPath();
// Prepare cancel link.
$query = $this->getRequest()->query;
if ($query->has('destination')) {
$options = Url::parse($query->get('destination'));
}
elseif (is_array($path)) {
$options = $path;
}
else {
$options = array('path' => $path);
}
$actions['cancel'] = array(
'#type' => 'link',
'#title' => $this->getCancelText(),
'#href' => $options['path'],
'#options' => $options,
);
$actions['cancel'] = ConfirmFormHelper::buildCancelLink($this, $this->getRequest());
return $actions;
}
......
......@@ -7,8 +7,6 @@
namespace Drupal\Core\Form;
use Drupal\Component\Utility\Url;
/**
* Provides an generic base class for a confirmation form.
*/
......@@ -46,19 +44,6 @@ public function getFormName() {
* {@inheritdoc}
*/
public function buildForm(array $form, array &$form_state) {
$path = $this->getCancelPath();
// Prepare cancel link.
$query = $this->getRequest()->query;
if ($query->has('destination')) {
$options = Url::parse($query->get('destination'));
}
elseif (is_array($path)) {
$options = $path;
}
else {
$options = array('path' => $path);
}
drupal_set_title($this->getQuestion(), PASS_THROUGH);
$form['#attributes']['class'][] = 'confirmation';
......@@ -70,12 +55,9 @@ public function buildForm(array $form, array &$form_state) {
'#type' => 'submit',
'#value' => $this->getConfirmText(),
);
$form['actions']['cancel'] = array(
'#type' => 'link',
'#title' => $this->getCancelText(),
'#href' => $options['path'],
'#options' => $options,
);
$form['actions']['cancel'] = ConfirmFormHelper::buildCancelLink($this, $this->getRequest());
// By default, render the form using theme_confirm_form().
if (!isset($form['#theme'])) {
$form['#theme'] = 'confirm_form';
......
<?php
/**
* @file
* Contains \Drupal\Core\Form\ConfirmFormHelper.
*/
namespace Drupal\Core\Form;
use Drupal\Component\Utility\String;
use Drupal\Component\Utility\Url;
use Symfony\Component\HttpFoundation\Request;
/**
* Provides common functionality to confirmation forms.
*/
class ConfirmFormHelper {
/**
* Builds the cancel link for a confirmation form.
*
* @param \Drupal\Core\Form\ConfirmFormInterface $form
* The confirmation form.
* @param \Symfony\Component\HttpFoundation\Request $request
* The current request.
*
* @return array
* The link render array for the cancel form.
*
* @throws \UnexpectedValueException
* Ensures that \Drupal\Core\Form\ConfirmFormInterface::getCancelRoute()
* returns an array containing a route name.
*/
public static function buildCancelLink(ConfirmFormInterface $form, Request $request) {
// Prepare cancel link.
$query = $request->query;
// If a destination is specified, that serves as the cancel link.
if ($query->has('destination')) {
$options = Url::parse($query->get('destination'));
$link = array(
'#href' => $options['path'],
'#options' => $options,
);
}
// Check for a route-based cancel link.
elseif ($route = $form->getCancelRoute()) {
if (empty($route['route_name'])) {
throw new \UnexpectedValueException(String::format('Missing route name in !class::getCancelRoute().', array('!class' => get_class($form))));
}
// Ensure there is something to pass as the params and options.
$route += array(
'route_parameters' => array(),
'options' => array(),
);
$link = array(
'#route_name' => $route['route_name'],
'#route_parameters' => $route['route_parameters'],
'#options' => $route['options'],
);
}
$link['#type'] = 'link';
$link['#title'] = $form->getCancelText();
return $link;
}
}
......@@ -21,17 +21,18 @@ interface ConfirmFormInterface extends FormInterface {
public function getQuestion();
/**
* Returns the page to go to if the user cancels the action.
* Returns the route 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.
* @return array
* An associative array with the following keys:
* - route_name: The name of the route.
* - route_parameters: (optional) An associative array of parameter names
* and values.
* - options: (optional) An associative array of additional options. See
* \Drupal\Core\Routing\UrlGeneratorInterface::generateFromRoute() for
* comprehensive documentation.
*/
public function getCancelPath();
public function getCancelRoute();
/**
* Returns additional text to display as a description.
......
......@@ -31,8 +31,10 @@ public function getConfirmText() {
/**
* {@inheritdoc}
*/
public function getCancelPath() {
return 'admin/config/system/actions';
public function getCancelRoute() {
return array(
'route_name' => 'action_admin',
);
}
/**
......
......@@ -85,8 +85,10 @@ public function getQuestion() {
/**
* {@inheritdoc}
*/
public function getCancelPath() {
return 'admin/config/services/aggregator';
public function getCancelRoute() {
return array(
'route_name' => 'aggregator_admin_overview',
);
}
/**
......
......@@ -24,8 +24,10 @@ public function getQuestion() {
/**
* {@inheritdoc}
*/
public function getCancelPath() {
return 'admin/config/services/aggregator';
public function getCancelRoute() {
return array(
'route_name' => 'aggregator_admin_overview',
);
}
/**
......
......@@ -24,8 +24,10 @@ public function getQuestion() {
/**
* {@inheritdoc}
*/
public function getCancelPath() {
return 'admin/config/services/aggregator';
public function getCancelRoute() {
return array(
'route_name' => 'aggregator_admin_overview',
);
}
/**
......
......@@ -67,8 +67,10 @@ public function getConfirmText() {
/**
* {@inheritdoc}
*/
public function getCancelPath() {
return 'admin/config/people/ban';
public function getCancelRoute() {
return array(
'route_name' => 'ban_admin_page',
);
}
/**
......
......@@ -25,8 +25,10 @@ public function getQuestion() {
/**
* {@inheritdoc}
*/
public function getCancelPath() {
return 'admin/structure/block';
public function getCancelRoute() {
return array(
'route_name' => 'block_admin_display',
);
}
/**
......
......@@ -52,8 +52,10 @@ public function getQuestion() {
/**
* {@inheritdoc}
*/
public function getCancelPath() {
return 'admin/structure/block/custom-blocks/types';
public function getCancelRoute() {
return array(
'route_name' => 'custom_block_type_list',
);
}
/**
......
......@@ -24,8 +24,10 @@ public function getQuestion() {
/**
* {@inheritdoc}
*/
public function getCancelPath() {
return 'admin/structure/block';
public function getCancelRoute() {
return array(
'route_name' => 'block_admin_display',
);
}
/**
......
......@@ -70,8 +70,7 @@ public function getQuestion() {
/**
* {@inheritdoc}
*/
public function getCancelPath() {
return 'admin/content/comment';
public function getCancelRoute() {
}
/**
......@@ -112,7 +111,11 @@ public function buildForm(array $form, array &$form_state, Request $request = NU
$form_state['redirect'] = 'admin/content/comment';
}
return parent::buildForm($form, $form_state, $request);
$form = parent::buildForm($form, $form_state);
// @todo Convert to getCancelRoute() after http://drupal.org/node/1986606.
$form['actions']['cancel']['#href'] = 'admin/content/comment';
return $form;
}
/**
......
......@@ -26,8 +26,19 @@ public function getQuestion() {
/**
* {@inheritdoc}
*/
public function getCancelPath() {
return 'node/' . $this->entity->nid->target_id;
protected function actions(array $form, array &$form_state) {
$actions = parent::actions($form, $form_state);
// @todo Convert to getCancelRoute() after http://drupal.org/node/1987778.
$actions['cancel']['#href'] = 'node/' . $this->entity->nid->target_id;
return $actions;
}
/**
* {@inheritdoc}
*/
public function getCancelRoute() {
}
/**
......
......@@ -31,8 +31,10 @@ public function getConfirmText() {
/**
* {@inheritdoc}
*/
public function getCancelPath() {
return 'admin/structure/config_test';
public function getCancelRoute() {
return array(
'route_name' => 'config_test_list_page',
);
}
/**
......
......@@ -24,8 +24,10 @@ public function getQuestion() {
/**
* {@inheritdoc}
*/
public function getCancelPath() {
return 'admin/structure/contact';
public function getCancelRoute() {
return array(
'route_name' => 'contact_category_list',
);
}
/**
......
......@@ -66,8 +66,10 @@ public function getDescription() {
/**
* {@inheritdoc}
*/
public function getCancelPath() {
return '';
public function getCancelRoute() {
return array(
'route_name' => '<front>',
);
}
/**
......
......@@ -17,9 +17,10 @@ class EntityDisplayModeDeleteForm extends EntityConfirmFormBase {
/**
* {@inheritdoc}
*/
public function getCancelPath() {
$short_type = str_replace('_mode', '', $this->entity->entityType());
return "admin/structure/display-modes/$short_type";
public function getCancelRoute() {
return array(
'route_name' => 'entity_' . $this->entity->entityType() . '.list',
);
}
/**
......
......@@ -59,8 +59,8 @@ public function getConfirmText() {
/**
* {@inheritdoc}
*/
public function getCancelPath() {
return $this->entityManager->getAdminPath($this->entity->entity_type, $this->entity->bundle) . '/fields';
public function getCancelRoute() {
return $this->entityManager->getAdminRouteInfo($this->entity->entity_type, $this->entity->bundle);
}
/**
......
......@@ -24,8 +24,10 @@ public function getQuestion() {
/**
* {@inheritdoc}
*/
public function getCancelPath() {
return 'admin/config/content/formats';
public function getCancelRoute() {
return array(
'route_name' => 'filter_admin_overview',
);
}
/**
......
......@@ -39,8 +39,7 @@ public function getQuestion() {
/**
* {@inheritdoc}
*/
public function getCancelPath() {
return 'admin/structure/forum';
public function getCancelRoute() {
}
/**
......@@ -56,7 +55,11 @@ public function getConfirmText() {
public function buildForm(array $form, array &$form_state, TermInterface $taxonomy_term = NULL) {
$this->taxonomyTerm = $taxonomy_term;
return parent::buildForm($form, $form_state);
$form = parent::buildForm($form, $form_state);
// @todo Convert to getCancelRoute() after http://drupal.org/node/1974210.
$form['actions']['cancel']['#href'] = 'admin/structure/forum';
return $form;
}
/**
......
......@@ -46,8 +46,13 @@ public function getConfirmText() {
/**
* {@inheritdoc}
*/
public function getCancelPath() {
return 'admin/config/media/image-styles/manage/' . $this->imageStyle->id();
public function getCancelRoute() {
return array(
'route_name' => 'image_style_edit',
'route_parameters' => array(
'image_style' => $this->imageStyle->id(),
),
);
}
/**
......
......@@ -31,8 +31,10 @@ public function getConfirmText() {
/**
* {@inheritdoc}
*/
public function getCancelPath() {
return 'admin/config/media/image-styles';
public function getCancelRoute() {
return array(
'route_name' => 'image_style_list',
);
}
/**
......
......@@ -55,8 +55,10 @@ public function getQuestion() {
/**
* {@inheritdoc}
*/
public function getCancelPath() {
return 'admin/config/regional/language';
public function getCancelRoute() {
return array(
'route_name' => 'language_admin_overview',
);
}
/**
......
......@@ -32,8 +32,7 @@ public function getQuestion() {
/**
* {@inheritdoc}
*/
public function getCancelPath() {
return 'admin/config/regional/language/detection/browser';
public function getCancelRoute() {
}
/**
......@@ -49,7 +48,11 @@ public function getFormID() {
public function buildForm(array $form, array &$form_state, $browser_langcode = NULL) {
$this->browserLangcode = $browser_langcode;
return parent::buildForm($form, $form_state);
$form = parent::buildForm($form, $form_state);
// @todo Convert to getCancelRoute() after http://drupal.org/node/2082071.
$form['actions']['cancel']['#href'] = 'admin/config/regional/language/detection/browser';
return $form;
}
/**
......
......@@ -64,8 +64,13 @@ public function getQuestion() {
/**
* {@inheritdoc}
*/
public function getCancelPath() {
return 'admin/structure/menu/manage/' . $this->entity->id();
public function getCancelRoute() {
return array(
'route_name' => 'menu_menu_edit',
'route_parameters' => array(
'menu' => $this->entity->id(),
),
);
}
/**
......
......@@ -24,8 +24,13 @@ public function getQuestion() {
/**
* {@inheritdoc}
*/
public function getCancelPath() {
return 'admin/structure/menu/manage/' . $this->entity->menu_name;
public function getCancelRoute() {
return array(
'route_name' => 'menu_menu_edit',
'route_parameters' => array(
'menu' => $this->entity->menu_name,
),
);
}
/**
......
......@@ -24,8 +24,13 @@ public function getQuestion() {
/**
* {@inheritdoc}
*/
public function getCancelPath() {
return 'admin/structure/menu/manage/' . $this->entity->menu_name;
public function getCancelRoute() {
return array(
'route_name' => 'menu_menu_edit',
'route_parameters' => array(
'menu' => $this->entity->menu_name,
),
);
}
/**
......
......@@ -81,8 +81,7 @@ public function getQuestion() {
/**
* {@inheritdoc}
*/
public function getCancelPath() {
return 'admin/content';
public function getCancelRoute() {
}
/**
......@@ -98,7 +97,7 @@ public function getConfirmText() {
public function buildForm(array $form, array &$form_state) {
$this->nodes = $this->tempStoreFactory->get('node_multiple_delete_confirm')->get($GLOBALS['user']->id());
if (empty($this->nodes)) {
return new RedirectResponse(url($this->getCancelPath(), array('absolute' => TRUE)));
return new RedirectResponse(url('admin/content', array('absolute' => TRUE)));
}
$form['nodes'] = array(
......@@ -107,7 +106,11 @@ public function buildForm(array $form, array &$form_state) {
return String::checkPlain($node->label());
}, $this->nodes),
);
return parent::buildForm($form, $form_state);
$form = parent::buildForm($form, $form_state);
// @todo Convert to getCancelRoute() after http://drupal.org/node/2021161.
$form['actions']['cancel']['#href'] = 'admin/content';
return $form;
}
/**
......
......@@ -64,9 +64,20 @@ public function getQuestion() {
/**
* {@inheritdoc}
*/
public function getCancelPath() {
protected function actions(array $form, array &$form_state) {
$actions = parent::actions($form, $form_state);
// @todo Convert to getCancelRoute() after http://drupal.org/node/1987778.
$uri = $this->entity->uri();
return $this->urlGenerator->generateFromPath($uri['path'], $uri['options']);
$actions['cancel']['#href'] = $this->urlGenerator->generateFromPath($uri['path'], $uri['options']);
return $actions;
}
/**
* {@inheritdoc}
*/
public function getCancelRoute() {
}
/**
......
......@@ -92,8 +92,7 @@ public function getQuestion() {
/**
* {@inheritdoc}
*/
public function getCancelPath() {
return 'node/' . $this->revision->id() . '/revisions';
public function getCancelRoute() {
}
/**
......@@ -108,7 +107,11 @@ public function getConfirmText() {
*/
public function buildForm(array $form, array &$form_state, $node_revision = NULL) {