Issue #1963394 by tim.plunkett, jibran, effulgentsia: Fixed ConfirmFormBase::getCancelPath() should allow for a route.

8.0.x
webchick 2013-09-12 21:51:47 -07:00
parent 670e43b51e
commit ca7ae5992a
54 changed files with 469 additions and 176 deletions

View File

@ -7,7 +7,7 @@
namespace Drupal\Core\Entity; namespace Drupal\Core\Entity;
use Drupal\Component\Utility\Url; use Drupal\Core\Form\ConfirmFormHelper;
use Drupal\Core\Form\ConfirmFormInterface; use Drupal\Core\Form\ConfirmFormInterface;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
@ -85,24 +85,8 @@ abstract class EntityConfirmFormBase extends EntityFormController implements Con
$actions['submit']['#value'] = $this->getConfirmText(); $actions['submit']['#value'] = $this->getConfirmText();
unset($actions['delete']); unset($actions['delete']);
$path = $this->getCancelPath();
// Prepare cancel link. // Prepare cancel link.
$query = $this->getRequest()->query; $actions['cancel'] = ConfirmFormHelper::buildCancelLink($this, $this->getRequest());
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,
);
return $actions; return $actions;
} }

View File

@ -410,6 +410,33 @@ class EntityManager extends PluginManagerBase {
return $admin_path; 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. * Gets an array of entity field definitions.
* *

View File

@ -7,7 +7,7 @@
namespace Drupal\Core\Entity; namespace Drupal\Core\Entity;
use Drupal\Component\Utility\Url; use Drupal\Core\Form\ConfirmFormHelper;
use Drupal\Core\Form\ConfirmFormInterface; use Drupal\Core\Form\ConfirmFormInterface;
/** /**
@ -92,24 +92,9 @@ abstract class EntityNGConfirmFormBase extends EntityFormControllerNG implements
$actions['submit']['#value'] = $this->getConfirmText(); $actions['submit']['#value'] = $this->getConfirmText();
unset($actions['delete']); unset($actions['delete']);
$path = $this->getCancelPath();
// Prepare cancel link. // Prepare cancel link.
$query = $this->getRequest()->query; $actions['cancel'] = ConfirmFormHelper::buildCancelLink($this, $this->getRequest());
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,
);
return $actions; return $actions;
} }

View File

@ -7,8 +7,6 @@
namespace Drupal\Core\Form; namespace Drupal\Core\Form;
use Drupal\Component\Utility\Url;
/** /**
* Provides an generic base class for a confirmation form. * Provides an generic base class for a confirmation form.
*/ */
@ -46,19 +44,6 @@ abstract class ConfirmFormBase extends FormBase implements ConfirmFormInterface
* {@inheritdoc} * {@inheritdoc}
*/ */
public function buildForm(array $form, array &$form_state) { 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); drupal_set_title($this->getQuestion(), PASS_THROUGH);
$form['#attributes']['class'][] = 'confirmation'; $form['#attributes']['class'][] = 'confirmation';
@ -70,12 +55,9 @@ abstract class ConfirmFormBase extends FormBase implements ConfirmFormInterface
'#type' => 'submit', '#type' => 'submit',
'#value' => $this->getConfirmText(), '#value' => $this->getConfirmText(),
); );
$form['actions']['cancel'] = array(
'#type' => 'link', $form['actions']['cancel'] = ConfirmFormHelper::buildCancelLink($this, $this->getRequest());
'#title' => $this->getCancelText(),
'#href' => $options['path'],
'#options' => $options,
);
// By default, render the form using theme_confirm_form(). // By default, render the form using theme_confirm_form().
if (!isset($form['#theme'])) { if (!isset($form['#theme'])) {
$form['#theme'] = 'confirm_form'; $form['#theme'] = 'confirm_form';

View File

@ -0,0 +1,67 @@
<?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;
}
}

View File

@ -21,17 +21,18 @@ interface ConfirmFormInterface extends FormInterface {
public function getQuestion(); 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 * @return array
* This can be either: * An associative array with the following keys:
* - A string containing a Drupal path. * - route_name: The name of the route.
* - An associative array with a 'path' key. Additional array values are * - route_parameters: (optional) An associative array of parameter names
* passed as the $options parameter to l(). * and values.
* If the 'destination' query parameter is set in the URL when viewing a * - options: (optional) An associative array of additional options. See
* confirmation form, that value will be used instead of this path. * \Drupal\Core\Routing\UrlGeneratorInterface::generateFromRoute() for
* comprehensive documentation.
*/ */
public function getCancelPath(); public function getCancelRoute();
/** /**
* Returns additional text to display as a description. * Returns additional text to display as a description.

View File

@ -31,8 +31,10 @@ class ActionDeleteForm extends EntityConfirmFormBase {
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function getCancelPath() { public function getCancelRoute() {
return 'admin/config/system/actions'; return array(
'route_name' => 'action_admin',
);
} }
/** /**

View File

@ -85,8 +85,10 @@ class CategoryDeleteForm extends ConfirmFormBase implements ContainerInjectionIn
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function getCancelPath() { public function getCancelRoute() {
return 'admin/config/services/aggregator'; return array(
'route_name' => 'aggregator_admin_overview',
);
} }
/** /**

View File

@ -24,8 +24,10 @@ class FeedDeleteForm extends EntityNGConfirmFormBase {
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function getCancelPath() { public function getCancelRoute() {
return 'admin/config/services/aggregator'; return array(
'route_name' => 'aggregator_admin_overview',
);
} }
/** /**

View File

@ -24,8 +24,10 @@ class FeedItemsRemoveForm extends EntityNGConfirmFormBase {
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function getCancelPath() { public function getCancelRoute() {
return 'admin/config/services/aggregator'; return array(
'route_name' => 'aggregator_admin_overview',
);
} }
/** /**

View File

@ -67,8 +67,10 @@ class BanDelete extends ConfirmFormBase {
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function getCancelPath() { public function getCancelRoute() {
return 'admin/config/people/ban'; return array(
'route_name' => 'ban_admin_page',
);
} }
/** /**

View File

@ -25,8 +25,10 @@ class CustomBlockDeleteForm extends EntityNGConfirmFormBase {
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function getCancelPath() { public function getCancelRoute() {
return 'admin/structure/block'; return array(
'route_name' => 'block_admin_display',
);
} }
/** /**

View File

@ -52,8 +52,10 @@ class CustomBlockTypeDeleteForm extends EntityConfirmFormBase {
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function getCancelPath() { public function getCancelRoute() {
return 'admin/structure/block/custom-blocks/types'; return array(
'route_name' => 'custom_block_type_list',
);
} }
/** /**

View File

@ -24,8 +24,10 @@ class BlockDeleteForm extends EntityConfirmFormBase {
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function getCancelPath() { public function getCancelRoute() {
return 'admin/structure/block'; return array(
'route_name' => 'block_admin_display',
);
} }
/** /**

View File

@ -70,8 +70,7 @@ class ConfirmDeleteMultiple extends ConfirmFormBase implements ContainerInjectio
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function getCancelPath() { public function getCancelRoute() {
return 'admin/content/comment';
} }
/** /**
@ -112,7 +111,11 @@ class ConfirmDeleteMultiple extends ConfirmFormBase implements ContainerInjectio
$form_state['redirect'] = 'admin/content/comment'; $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;
} }
/** /**

View File

@ -26,8 +26,19 @@ class DeleteForm extends EntityNGConfirmFormBase {
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function getCancelPath() { protected function actions(array $form, array &$form_state) {
return 'node/' . $this->entity->nid->target_id; $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() {
} }
/** /**

View File

@ -31,8 +31,10 @@ class ConfigTestDeleteForm extends EntityConfirmFormBase {
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function getCancelPath() { public function getCancelRoute() {
return 'admin/structure/config_test'; return array(
'route_name' => 'config_test_list_page',
);
} }
/** /**

View File

@ -24,8 +24,10 @@ class CategoryDeleteForm extends EntityConfirmFormBase {
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function getCancelPath() { public function getCancelRoute() {
return 'admin/structure/contact'; return array(
'route_name' => 'contact_category_list',
);
} }
/** /**

View File

@ -66,8 +66,10 @@ class TranslatableForm extends ConfirmFormBase {
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function getCancelPath() { public function getCancelRoute() {
return ''; return array(
'route_name' => '<front>',
);
} }
/** /**

View File

@ -17,9 +17,10 @@ class EntityDisplayModeDeleteForm extends EntityConfirmFormBase {
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function getCancelPath() { public function getCancelRoute() {
$short_type = str_replace('_mode', '', $this->entity->entityType()); return array(
return "admin/structure/display-modes/$short_type"; 'route_name' => 'entity_' . $this->entity->entityType() . '.list',
);
} }
/** /**

View File

@ -59,8 +59,8 @@ class FieldDeleteForm extends EntityConfirmFormBase {
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function getCancelPath() { public function getCancelRoute() {
return $this->entityManager->getAdminPath($this->entity->entity_type, $this->entity->bundle) . '/fields'; return $this->entityManager->getAdminRouteInfo($this->entity->entity_type, $this->entity->bundle);
} }
/** /**

View File

@ -24,8 +24,10 @@ class FilterDisableForm extends EntityConfirmFormBase {
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function getCancelPath() { public function getCancelRoute() {
return 'admin/config/content/formats'; return array(
'route_name' => 'filter_admin_overview',
);
} }
/** /**

View File

@ -39,8 +39,7 @@ class DeleteForm extends ConfirmFormBase {
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function getCancelPath() { public function getCancelRoute() {
return 'admin/structure/forum';
} }
/** /**
@ -56,7 +55,11 @@ class DeleteForm extends ConfirmFormBase {
public function buildForm(array $form, array &$form_state, TermInterface $taxonomy_term = NULL) { public function buildForm(array $form, array &$form_state, TermInterface $taxonomy_term = NULL) {
$this->taxonomyTerm = $taxonomy_term; $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;
} }
/** /**

View File

@ -46,8 +46,13 @@ class ImageEffectDeleteForm extends ConfirmFormBase {
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function getCancelPath() { public function getCancelRoute() {
return 'admin/config/media/image-styles/manage/' . $this->imageStyle->id(); return array(
'route_name' => 'image_style_edit',
'route_parameters' => array(
'image_style' => $this->imageStyle->id(),
),
);
} }
/** /**

View File

@ -31,8 +31,10 @@ class ImageStyleDeleteForm extends EntityConfirmFormBase {
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function getCancelPath() { public function getCancelRoute() {
return 'admin/config/media/image-styles'; return array(
'route_name' => 'image_style_list',
);
} }
/** /**

View File

@ -55,8 +55,10 @@ class LanguageDeleteForm extends EntityConfirmFormBase {
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function getCancelPath() { public function getCancelRoute() {
return 'admin/config/regional/language'; return array(
'route_name' => 'language_admin_overview',
);
} }
/** /**

View File

@ -32,8 +32,7 @@ class NegotiationBrowserDeleteForm extends ConfirmFormBase {
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function getCancelPath() { public function getCancelRoute() {
return 'admin/config/regional/language/detection/browser';
} }
/** /**
@ -49,7 +48,11 @@ class NegotiationBrowserDeleteForm extends ConfirmFormBase {
public function buildForm(array $form, array &$form_state, $browser_langcode = NULL) { public function buildForm(array $form, array &$form_state, $browser_langcode = NULL) {
$this->browserLangcode = $browser_langcode; $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;
} }
/** /**

View File

@ -64,8 +64,13 @@ class MenuDeleteForm extends EntityConfirmFormBase {
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function getCancelPath() { public function getCancelRoute() {
return 'admin/structure/menu/manage/' . $this->entity->id(); return array(
'route_name' => 'menu_menu_edit',
'route_parameters' => array(
'menu' => $this->entity->id(),
),
);
} }
/** /**

View File

@ -24,8 +24,13 @@ class MenuLinkDeleteForm extends EntityConfirmFormBase {
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function getCancelPath() { public function getCancelRoute() {
return 'admin/structure/menu/manage/' . $this->entity->menu_name; return array(
'route_name' => 'menu_menu_edit',
'route_parameters' => array(
'menu' => $this->entity->menu_name,
),
);
} }
/** /**

View File

@ -24,8 +24,13 @@ class MenuLinkResetForm extends EntityConfirmFormBase {
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function getCancelPath() { public function getCancelRoute() {
return 'admin/structure/menu/manage/' . $this->entity->menu_name; return array(
'route_name' => 'menu_menu_edit',
'route_parameters' => array(
'menu' => $this->entity->menu_name,
),
);
} }
/** /**

View File

@ -81,8 +81,7 @@ class DeleteMultiple extends ConfirmFormBase implements ContainerInjectionInterf
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function getCancelPath() { public function getCancelRoute() {
return 'admin/content';
} }
/** /**
@ -98,7 +97,7 @@ class DeleteMultiple extends ConfirmFormBase implements ContainerInjectionInterf
public function buildForm(array $form, array &$form_state) { public function buildForm(array $form, array &$form_state) {
$this->nodes = $this->tempStoreFactory->get('node_multiple_delete_confirm')->get($GLOBALS['user']->id()); $this->nodes = $this->tempStoreFactory->get('node_multiple_delete_confirm')->get($GLOBALS['user']->id());
if (empty($this->nodes)) { 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( $form['nodes'] = array(
@ -107,7 +106,11 @@ class DeleteMultiple extends ConfirmFormBase implements ContainerInjectionInterf
return String::checkPlain($node->label()); return String::checkPlain($node->label());
}, $this->nodes), }, $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;
} }
/** /**

View File

@ -64,9 +64,20 @@ class NodeDeleteForm extends EntityNGConfirmFormBase {
/** /**
* {@inheritdoc} * {@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(); $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() {
} }
/** /**

View File

@ -92,8 +92,7 @@ class NodeRevisionDeleteForm extends ConfirmFormBase implements ContainerInjecti
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function getCancelPath() { public function getCancelRoute() {
return 'node/' . $this->revision->id() . '/revisions';
} }
/** /**
@ -108,7 +107,11 @@ class NodeRevisionDeleteForm extends ConfirmFormBase implements ContainerInjecti
*/ */
public function buildForm(array $form, array &$form_state, $node_revision = NULL) { public function buildForm(array $form, array &$form_state, $node_revision = NULL) {
$this->revision = $this->nodeStorage->loadRevision($node_revision); $this->revision = $this->nodeStorage->loadRevision($node_revision);
return parent::buildForm($form, $form_state); $form = parent::buildForm($form, $form_state);
// @todo Convert to getCancelRoute() after http://drupal.org/node/1863906.
$form['actions']['cancel']['#href'] = 'node/' . $this->revision->id() . '/revisions';
return $form;
} }
/** /**

View File

@ -68,8 +68,7 @@ class NodeRevisionRevertForm extends ConfirmFormBase implements ContainerInjecti
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function getCancelPath() { public function getCancelRoute() {
return 'node/' . $this->revision->id() . '/revisions';
} }
/** /**
@ -91,7 +90,11 @@ class NodeRevisionRevertForm extends ConfirmFormBase implements ContainerInjecti
*/ */
public function buildForm(array $form, array &$form_state, $node_revision = NULL) { public function buildForm(array $form, array &$form_state, $node_revision = NULL) {
$this->revision = $this->nodeStorage->loadRevision($node_revision); $this->revision = $this->nodeStorage->loadRevision($node_revision);
return parent::buildForm($form, $form_state); $form = parent::buildForm($form, $form_state);
// @todo Convert to getCancelRoute() after http://drupal.org/node/1863906.
$form['actions']['cancel']['#href'] = 'node/' . $this->revision->id() . '/revisions';
return $form;
} }
/** /**

View File

@ -52,8 +52,10 @@ class NodeTypeDeleteConfirm extends EntityConfirmFormBase {
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function getCancelPath() { public function getCancelRoute() {
return 'admin/structure/types'; return array(
'route_name' => 'node_overview_types',
);
} }
/** /**
@ -87,7 +89,7 @@ class NodeTypeDeleteConfirm extends EntityConfirmFormBase {
drupal_set_message(t('The content type %name has been deleted.', $t_args)); drupal_set_message(t('The content type %name has been deleted.', $t_args));
watchdog('node', 'Deleted content type %name.', $t_args, WATCHDOG_NOTICE); watchdog('node', 'Deleted content type %name.', $t_args, WATCHDOG_NOTICE);
$form_state['redirect'] = $this->getCancelPath(); $form_state['redirect'] = 'admin/structure/types';
} }
} }

View File

@ -28,8 +28,10 @@ class RebuildPermissionsForm extends ConfirmFormBase {
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function getCancelPath() { public function getCancelRoute() {
return 'admin/reports/status'; return array(
'route_name' => 'system_status',
);
} }
/** /**

View File

@ -65,10 +65,9 @@ class DeleteForm extends ConfirmFormBase implements ContainerInjectionInterface
} }
/** /**
* Implements \Drupal\Core\Form\ConfirmFormBase::getCancelPath(). * {@inheritdoc}
*/ */
public function getCancelPath() { public function getCancelRoute() {
return 'admin/config/search/path';
} }
/** /**
@ -77,7 +76,11 @@ class DeleteForm extends ConfirmFormBase implements ContainerInjectionInterface
public function buildForm(array $form, array &$form_state, $pid = NULL) { public function buildForm(array $form, array &$form_state, $pid = NULL) {
$this->pathAlias = $this->path->load(array('pid' => $pid)); $this->pathAlias = $this->path->load(array('pid' => $pid));
return parent::buildForm($form, $form_state); $form = parent::buildForm($form, $form_state);
// @todo Convert to getCancelRoute() after http://drupal.org/node/1987802.
$form['actions']['cancel']['#href'] = 'admin/config/search/path';
return $form;
} }
/** /**

View File

@ -21,8 +21,10 @@ class PictureMappingDeleteForm extends EntityConfirmFormBase {
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function getCancelPath() { public function getCancelRoute() {
return 'admin/config/media/picturemapping'; return array(
'route_name' => 'picture_mapping_page',
);
} }
/** /**

View File

@ -50,10 +50,12 @@ class ReindexConfirm extends ConfirmFormBase {
} }
/** /**
* Implements \Drupal\Core\Form\ConfirmFormBase::getCancelPath(). * {@inheritdoc}
*/ */
public function getCancelPath() { public function getCancelRoute() {
return 'admin/config/search/settings'; return array(
'route_name' => 'search_settings',
);
} }
/** /**

View File

@ -39,8 +39,13 @@ class LinkDelete extends ConfirmFormBase {
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function getCancelPath() { public function getCancelRoute() {
return 'admin/config/user-interface/shortcut/manage/' . $this->menuLink->menu_name; return array(
'route_name' => 'shortcut_set_customize',
'route_parameters' => array(
'shortcut_set' => str_replace('shortcut-', '', $this->menuLink->menu_name),
),
);
} }
/** /**

View File

@ -59,8 +59,13 @@ class ShortcutSetDeleteForm extends EntityConfirmFormBase {
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function getCancelPath() { public function getCancelRoute() {
return 'admin/config/user-interface/shortcut/manage/' . $this->entity->id(); return array(
'route_name' => 'shortcut_set_customize',
'route_parameters' => array(
'shortcut_set' => $this->entity->id(),
),
);
} }
/** /**

View File

@ -62,8 +62,10 @@ class DateFormatDeleteForm extends EntityConfirmFormBase {
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function getCancelPath() { public function getCancelRoute() {
return 'admin/config/regional/date-time'; return array(
'route_name' => 'date_format_list',
);
} }
/** /**

View File

@ -76,8 +76,10 @@ class DateFormatLocalizeResetForm extends ConfirmFormBase implements ContainerIn
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function getCancelPath() { public function getCancelRoute() {
return 'admin/config/regional/date-time/locale'; return array(
'route_name' => 'date_format_language_overview',
);
} }
/** /**

View File

@ -73,8 +73,10 @@ class ModulesListConfirmForm extends ConfirmFormBase implements ContainerInjecti
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function getCancelPath() { public function getCancelRoute() {
return 'admin/modules'; return array(
'route_name' => 'system_modules_list',
);
} }
/** /**
@ -107,7 +109,7 @@ class ModulesListConfirmForm extends ConfirmFormBase implements ContainerInjecti
// Redirect to the modules list page if the key value store is empty. // Redirect to the modules list page if the key value store is empty.
if (!$this->modules) { if (!$this->modules) {
return new RedirectResponse(url($this->getCancelPath(), array('absolute' => TRUE))); return new RedirectResponse($this->urlGenerator()->generate('system_modules_list', array(), TRUE));
} }
$items = array(); $items = array();
@ -161,7 +163,7 @@ class ModulesListConfirmForm extends ConfirmFormBase implements ContainerInjecti
drupal_set_message($this->t('The configuration options have been saved.')); drupal_set_message($this->t('The configuration options have been saved.'));
} }
$form_state['redirect'] = $this->getCancelPath(); $form_state['redirect'] = 'admin/modules';
} }
} }

View File

@ -80,8 +80,10 @@ class ModulesUninstallConfirmForm extends ConfirmFormBase implements ContainerIn
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function getCancelPath() { public function getCancelRoute() {
return 'admin/modules/uninstall'; return array(
'route_name' => 'system_modules_uninstall',
);
} }
/** /**

View File

@ -22,11 +22,13 @@ class ConfirmFormArrayPathTestForm extends ConfirmFormTestForm {
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function getCancelPath() { public function getCancelRoute() {
return array( return array(
'path' => 'admin', 'route_name' => 'system_admin',
'query' => array( 'options' => array(
'destination' => 'admin/config', 'query' => array(
'destination' => 'admin/config',
),
), ),
); );
} }

View File

@ -31,8 +31,10 @@ class ConfirmFormTestForm extends ConfirmFormBase {
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function getCancelPath() { public function getCancelRoute() {
return 'admin'; return array(
'route_name' => 'system_admin',
);
} }
/** /**

View File

@ -60,8 +60,10 @@ class TermDeleteForm extends EntityNGConfirmFormBase {
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function getCancelPath() { public function getCancelRoute() {
return 'admin/structure/taxonomy'; return array(
'route_name' => 'taxonomy_vocabulary_list',
);
} }
/** /**

View File

@ -32,8 +32,10 @@ class VocabularyDeleteForm extends EntityConfirmFormBase {
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function getCancelPath() { public function getCancelRoute() {
return 'admin/structure/taxonomy'; return array(
'route_name' => 'taxonomy_vocabulary_list',
);
} }
/** /**

View File

@ -56,8 +56,13 @@ class VocabularyResetForm extends EntityConfirmFormBase {
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function getCancelPath() { public function getCancelRoute() {
return 'admin/structure/taxonomy/manage/' . $this->entity->id(); return array(
'route_name' => 'taxonomy_overview_terms',
'route_parameters' => array(
'taxonomy_vocabulary' => $this->entity->id(),
),
);
} }
/** /**

View File

@ -24,8 +24,10 @@ class UserRoleDelete extends EntityConfirmFormBase {
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function getCancelPath() { public function getCancelRoute() {
return 'admin/people/roles'; return array(
'route_name' => 'user_role_list',
);
} }
/** /**

View File

@ -84,8 +84,13 @@ class BreakLockForm extends EntityConfirmFormBase {
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function getCancelPath() { public function getCancelRoute() {
return 'admin/structure/views/view/' . $this->entity->id(); return array(
'route_name' => 'views_ui.edit',
'route_parameters' => array(
'view' => $this->entity->id(),
),
);
} }
/** /**

View File

@ -24,8 +24,10 @@ class ViewDeleteFormController extends EntityConfirmFormBase {
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function getCancelPath() { public function getCancelRoute() {
return 'admin/structure/views'; return array(
'route_name' => 'views_ui.list',
);
} }
/** /**

View File

@ -0,0 +1,107 @@
<?php
/**
* @file
* Contains \Drupal\Tests\Core\Form\ConfirmFormHelperTest.
*/
namespace Drupal\Tests\Core\Form;
use Drupal\Core\Form\ConfirmFormHelper;
use Drupal\Tests\UnitTestCase;
use Symfony\Component\HttpFoundation\Request;
/**
* Tests the confirm form helper class.
*
* @see \Drupal\Core\Form\ConfirmFormHelper
*
* @group Drupal
* @group Form
*/
class ConfirmFormHelperTest extends UnitTestCase {
public static function getInfo() {
return array(
'name' => 'Confirm form helper test',
'description' => 'Tests the confirm form helper class.',
'group' => 'Form API',
);
}
/**
* Tests the cancel link title.
*/
public function testCancelLinkTitle() {
$cancel_text = 'Cancel text';
$form = $this->getMock('Drupal\Core\Form\ConfirmFormInterface');
$form->expects($this->any())
->method('getCancelText')
->will($this->returnValue($cancel_text));
$link = ConfirmFormHelper::buildCancelLink($form, new Request());
$this->assertSame($cancel_text, $link['#title']);
}
/**
* Tests a cancel link route.
*/
public function testCancelLinkRoute() {
$cancel_route = array(
'route_name' => 'foo_bar',
);
$form = $this->getMock('Drupal\Core\Form\ConfirmFormInterface');
$form->expects($this->any())
->method('getCancelRoute')
->will($this->returnValue($cancel_route));
$link = ConfirmFormHelper::buildCancelLink($form, new Request());
$this->assertSame($cancel_route['route_name'], $link['#route_name']);
}
/**
* Tests a cancel link route with parameters.
*/
public function testCancelLinkRouteWithParams() {
$cancel_route = array(
'route_name' => 'foo_bar/{baz}',
'route_parameters' => array(
'baz' => 'banana',
),
'options' => array(
'html' => TRUE,
),
);
$form = $this->getMock('Drupal\Core\Form\ConfirmFormInterface');
$form->expects($this->any())
->method('getCancelRoute')
->will($this->returnValue($cancel_route));
$link = ConfirmFormHelper::buildCancelLink($form, new Request());
$this->assertSame($cancel_route['route_name'], $link['#route_name']);
$this->assertSame($cancel_route['route_parameters'], $link['#route_parameters']);
$this->assertSame($cancel_route['options'], $link['#options']);
}
/**
* Tests an invalid cancel link route.
*
* @expectedException \UnexpectedValueException
*/
public function testCancelLinkInvalidRoute() {
$form = $this->getMock('Drupal\Core\Form\ConfirmFormInterface');
$form->expects($this->any())
->method('getCancelRoute')
->will($this->returnValue(array('invalid' => 'foo_bar')));
ConfirmFormHelper::buildCancelLink($form, new Request());
}
/**
* Tests a cancel link provided by the destination.
*/
public function testCancelLinkDestination() {
$query = array('destination' => 'baz');
$form = $this->getMock('Drupal\Core\Form\ConfirmFormInterface');
$link = ConfirmFormHelper::buildCancelLink($form, new Request($query));
$this->assertSame($query['destination'], $link['#href']);
}
}