diff --git a/core/lib/Drupal/Core/Routing/RedirectDestinationTrait.php b/core/lib/Drupal/Core/Routing/RedirectDestinationTrait.php new file mode 100644 index 0000000..f6f7f41 --- /dev/null +++ b/core/lib/Drupal/Core/Routing/RedirectDestinationTrait.php @@ -0,0 +1,75 @@ +getRedirectDestination()->getAsArray(); + } + + /** + * Returns the redirect destination service. + * + * @return \Drupal\Core\Routing\RedirectDestinationInterface + * The redirect destination helper. + */ + protected function getRedirectDestination() { + if (!isset($this->redirectDestination)) { + $this->redirectDestination = \Drupal::destination(); + } + + return $this->redirectDestination; + } + + /** + * Sets the redirect destination service. + * + * @param \Drupal\Core\Routing\RedirectDestinationInterface $redirect_destination + * The link generator service. + * + * @return $this + */ + public function setRedirectDestination(RedirectDestinationInterface $redirect_destination) { + $this->redirectDestination = $redirect_destination; + + return $this; + } + +} diff --git a/core/modules/comment/src/Plugin/views/field/Link.php b/core/modules/comment/src/Plugin/views/field/Link.php index 377ab4a..70c5c0e 100644 --- a/core/modules/comment/src/Plugin/views/field/Link.php +++ b/core/modules/comment/src/Plugin/views/field/Link.php @@ -11,7 +11,6 @@ use Drupal\Core\Form\FormStateInterface; use Drupal\views\Plugin\views\field\FieldPluginBase; use Drupal\Core\Plugin\ContainerFactoryPluginInterface; -use Drupal\Core\Routing\RedirectDestinationInterface; use Drupal\Core\Url; use Drupal\views\ResultRow; use Symfony\Component\DependencyInjection\ContainerInterface; @@ -33,13 +32,6 @@ class Link extends FieldPluginBase { protected $entityManager; /** - * The redirect destination service. - * - * @var \Drupal\Core\Routing\RedirectDestinationInterface - */ - protected $redirectDestination; - - /** * {@inheritdoc} */ public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { @@ -47,8 +39,7 @@ public static function create(ContainerInterface $container, array $configuratio $configuration, $plugin_id, $plugin_definition, - $container->get('entity.manager'), - $container->get('redirect.destination') + $container->get('entity.manager') ); } @@ -63,13 +54,10 @@ public static function create(ContainerInterface $container, array $configuratio * The plugin implementation definition. * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager * The entity manager service. - * @param \Drupal\Core\Routing\RedirectDestinationInterface $redirect_destination - * The redirect destination service. */ - public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityManagerInterface $entity_manager, RedirectDestinationInterface $redirect_destination) { + public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityManagerInterface $entity_manager) { parent::__construct($configuration, $plugin_id, $plugin_definition); $this->entityManager = $entity_manager; - $this->redirectDestination = $redirect_destination; } /** diff --git a/core/modules/comment/src/Plugin/views/field/LinkApprove.php b/core/modules/comment/src/Plugin/views/field/LinkApprove.php index fabcb74..4311be2 100644 --- a/core/modules/comment/src/Plugin/views/field/LinkApprove.php +++ b/core/modules/comment/src/Plugin/views/field/LinkApprove.php @@ -9,6 +9,7 @@ use Drupal\comment\CommentInterface; use Drupal\Core\Session\AccountInterface; +use Drupal\Core\Routing\RedirectDestinationTrait; use Drupal\Core\Url; use Drupal\views\ResultRow; @@ -21,6 +22,8 @@ */ class LinkApprove extends Link { + use RedirectDestinationTrait; + /** * {@inheritdoc} */ @@ -53,7 +56,7 @@ protected function renderLink($data, ResultRow $values) { $this->options['alter']['make_link'] = TRUE; $this->options['alter']['url'] = Url::fromRoute('comment.approve', ['comment' => $comment->id()]); - $this->options['alter']['query'] = $this->redirectDestination->getAsArray() + array('token' => \Drupal::csrfToken()->get($this->options['alter']['url']->toString())); + $this->options['alter']['query'] = $this->getDestination() + array('token' => \Drupal::csrfToken()->get($this->options['alter']['url']->toString())); return $text; } diff --git a/core/modules/comment/src/Plugin/views/field/LinkDelete.php b/core/modules/comment/src/Plugin/views/field/LinkDelete.php index fb723ed..17d670b 100644 --- a/core/modules/comment/src/Plugin/views/field/LinkDelete.php +++ b/core/modules/comment/src/Plugin/views/field/LinkDelete.php @@ -7,6 +7,7 @@ namespace Drupal\comment\Plugin\views\field; +use Drupal\Core\Routing\RedirectDestinationTrait; use Drupal\Core\Session\AccountInterface; use Drupal\views\ResultRow; @@ -19,6 +20,8 @@ */ class LinkDelete extends Link { + use RedirectDestinationTrait; + /** * {@inheritdoc} */ @@ -44,7 +47,7 @@ protected function renderLink($data, ResultRow $values) { $this->options['alter']['make_link'] = TRUE; $this->options['alter']['url'] = $comment->urlInfo('delete-form'); - $this->options['alter']['query'] = $this->redirectDestination->getAsArray(); + $this->options['alter']['query'] = $this->getDestination(); return $text; } diff --git a/core/modules/comment/src/Plugin/views/field/LinkEdit.php b/core/modules/comment/src/Plugin/views/field/LinkEdit.php index 11f8909..52347b3 100644 --- a/core/modules/comment/src/Plugin/views/field/LinkEdit.php +++ b/core/modules/comment/src/Plugin/views/field/LinkEdit.php @@ -8,6 +8,7 @@ namespace Drupal\comment\Plugin\views\field; use Drupal\Core\Form\FormStateInterface; +use Drupal\Core\Routing\RedirectDestinationTrait; use Drupal\views\ResultRow; /** @@ -19,6 +20,8 @@ */ class LinkEdit extends Link { + use RedirectDestinationTrait; + protected function defineOptions() { $options = parent::defineOptions(); $options['destination'] = array('default' => FALSE); @@ -60,7 +63,7 @@ protected function renderLink($data, ResultRow $values) { unset($this->options['alter']['fragment']); if (!empty($this->options['destination'])) { - $this->options['alter']['query'] = $this->redirectDestination->getAsArray(); + $this->options['alter']['query'] = $this->getDestination(); } $this->options['alter']['url'] = $comment->urlInfo('edit-form'); diff --git a/core/modules/contact/src/Plugin/views/field/ContactLink.php b/core/modules/contact/src/Plugin/views/field/ContactLink.php index 5097d6a..f746e00 100644 --- a/core/modules/contact/src/Plugin/views/field/ContactLink.php +++ b/core/modules/contact/src/Plugin/views/field/ContactLink.php @@ -10,7 +10,6 @@ use Drupal\Core\Access\AccessManagerInterface; use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Form\FormStateInterface; -use Drupal\Core\Routing\RedirectDestinationInterface; use Drupal\Core\Session\AccountInterface; use Drupal\Core\Url; use Drupal\user\Plugin\views\field\Link; @@ -66,11 +65,9 @@ protected function currentUser() { * The plugin implementation definition. * @param \Drupal\Core\Access\AccessManagerInterface $access_manager * The access manager. - * @param \Drupal\Core\Routing\RedirectDestinationInterface $redirect_destination - * The redirect destination service. */ - public function __construct(array $configuration, $plugin_id, $plugin_definition, AccessManagerInterface $access_manager, RedirectDestinationInterface $redirect_destination) { - parent::__construct($configuration, $plugin_id, $plugin_definition, $redirect_destination); + public function __construct(array $configuration, $plugin_id, $plugin_definition, AccessManagerInterface $access_manager) { + parent::__construct($configuration, $plugin_id, $plugin_definition); $this->accessManager = $access_manager; } @@ -82,8 +79,7 @@ public static function create(ContainerInterface $container, array $configuratio $configuration, $plugin_id, $plugin_definition, - $container->get('access_manager'), - $container->get('redirect.destination') + $container->get('access_manager') ); } diff --git a/core/modules/contextual/src/Plugin/views/field/ContextualLinks.php b/core/modules/contextual/src/Plugin/views/field/ContextualLinks.php index e67b4a3..20ca2dd 100644 --- a/core/modules/contextual/src/Plugin/views/field/ContextualLinks.php +++ b/core/modules/contextual/src/Plugin/views/field/ContextualLinks.php @@ -11,7 +11,7 @@ use Drupal\Component\Utility\String; use Drupal\Component\Utility\UrlHelper; use Drupal\Core\Form\FormStateInterface; -use Drupal\Core\Routing\RedirectDestinationInterface; +use Drupal\Core\Routing\RedirectDestinationTrait; use Drupal\Core\Url; use Drupal\views\Plugin\views\field\FieldPluginBase; use Drupal\views\ResultRow; @@ -25,29 +25,7 @@ */ class ContextualLinks extends FieldPluginBase { - /** - * The redirect destination service. - * - * @var \Drupal\Core\Routing\RedirectDestinationInterface - */ - protected $redirectDestination; - - /** - * Constructs a Link field plugin. - * - * @param array $configuration - * A configuration array containing information about the plugin instance. - * @param string $plugin_id - * The plugin_id for the plugin instance. - * @param mixed $plugin_definition - * The plugin implementation definition. - * @param \Drupal\Core\Routing\RedirectDestinationInterface $redirect_destination - * The redirect destination service. - */ - public function __construct(array $configuration, $plugin_id, $plugin_definition, RedirectDestinationInterface $redirect_destination) { - parent::__construct($configuration, $plugin_id, $plugin_definition); - $this->redirectDestination = $redirect_destination; - } + use RedirectDestinationTrait; /** * {@inheritdoc} @@ -144,7 +122,7 @@ public function render(ResultRow $values) { 'title' => $title, ); if (!empty($this->options['destination'])) { - $links[$field]['query'] = $this->redirectDestination->getAsArray(); + $links[$field]['query'] = $this->getDestination(); } } } diff --git a/core/modules/node/src/Plugin/views/field/LinkDelete.php b/core/modules/node/src/Plugin/views/field/LinkDelete.php index 2737ea7..dfb8abb 100644 --- a/core/modules/node/src/Plugin/views/field/LinkDelete.php +++ b/core/modules/node/src/Plugin/views/field/LinkDelete.php @@ -7,6 +7,7 @@ namespace Drupal\node\Plugin\views\field; +use Drupal\Core\Routing\RedirectDestinationTrait; use Drupal\node\Plugin\views\field\Link; use Drupal\views\ResultRow; @@ -19,6 +20,8 @@ */ class LinkDelete extends Link { + use RedirectDestinationTrait; + /** * Prepares the link to delete a node. * @@ -38,7 +41,7 @@ protected function renderLink($node, ResultRow $values) { $this->options['alter']['make_link'] = TRUE; $this->options['alter']['url'] = $node->urlInfo('delete-form'); - $this->options['alter']['query'] = $this->redirectDestination->getAsArray(); + $this->options['alter']['query'] = $this->getDestination(); $text = !empty($this->options['text']) ? $this->options['text'] : $this->t('Delete'); return $text; diff --git a/core/modules/node/src/Plugin/views/field/LinkEdit.php b/core/modules/node/src/Plugin/views/field/LinkEdit.php index 279dcd0..e8007e1 100644 --- a/core/modules/node/src/Plugin/views/field/LinkEdit.php +++ b/core/modules/node/src/Plugin/views/field/LinkEdit.php @@ -7,6 +7,7 @@ namespace Drupal\node\Plugin\views\field; +use Drupal\Core\Routing\RedirectDestinationTrait; use Drupal\Core\Url; use Drupal\node\Plugin\views\field\Link; use Drupal\views\ResultRow; @@ -20,6 +21,8 @@ */ class LinkEdit extends Link { + use RedirectDestinationTrait; + /** * Prepares the link to the node. * @@ -39,7 +42,7 @@ protected function renderLink($node, ResultRow $values) { $this->options['alter']['make_link'] = TRUE; $this->options['alter']['url'] = $node->urlInfo('edit-form'); - $this->options['alter']['query'] = $this->redirectDestination->getAsArray(); + $this->options['alter']['query'] = $this->getDestination(); $text = !empty($this->options['text']) ? $this->options['text'] : $this->t('Edit'); return $text; diff --git a/core/modules/node/src/Plugin/views/field/RevisionLink.php b/core/modules/node/src/Plugin/views/field/RevisionLink.php index 5c7c4dc..c354694 100644 --- a/core/modules/node/src/Plugin/views/field/RevisionLink.php +++ b/core/modules/node/src/Plugin/views/field/RevisionLink.php @@ -8,6 +8,7 @@ namespace Drupal\node\Plugin\views\field; use Drupal\Core\Session\AccountInterface; +use Drupal\Core\Routing\RedirectDestinationTrait; use Drupal\Core\Url; use Drupal\node\Plugin\views\field\Link; use Drupal\views\Plugin\views\display\DisplayPluginBase; @@ -23,6 +24,8 @@ */ class RevisionLink extends Link { + use RedirectDestinationTrait; + /** * Overrides Drupal\views\Plugin\views\field\FieldPluginBase::init(). */ @@ -66,7 +69,7 @@ protected function renderLink($data, ResultRow $values) { $this->options['alter']['make_link'] = TRUE; $this->options['alter']['url'] = $url; - $this->options['alter']['query'] = $this->redirectDestination->getAsArray(); + $this->options['alter']['query'] = $this->getDestination(); return !empty($this->options['text']) ? $this->options['text'] : $this->t('View'); } diff --git a/core/modules/node/src/Plugin/views/field/RevisionLinkDelete.php b/core/modules/node/src/Plugin/views/field/RevisionLinkDelete.php index 773e764..32a94f0 100644 --- a/core/modules/node/src/Plugin/views/field/RevisionLinkDelete.php +++ b/core/modules/node/src/Plugin/views/field/RevisionLinkDelete.php @@ -52,7 +52,7 @@ protected function renderLink($data, ResultRow $values) { $this->options['alter']['make_link'] = TRUE; $this->options['alter']['url'] = Url::fromRoute('node.revision_delete_confirm', ['node' => $node->id(), 'node_revision' => $vid]); - $this->options['alter']['query'] = $this->redirectDestination->getAsArray(); + $this->options['alter']['query'] = $this->getDestination(); return !empty($this->options['text']) ? $this->options['text'] : $this->t('Delete'); } diff --git a/core/modules/node/src/Plugin/views/field/RevisionLinkRevert.php b/core/modules/node/src/Plugin/views/field/RevisionLinkRevert.php index 163d636..ac28c6d 100644 --- a/core/modules/node/src/Plugin/views/field/RevisionLinkRevert.php +++ b/core/modules/node/src/Plugin/views/field/RevisionLinkRevert.php @@ -52,7 +52,7 @@ protected function renderLink($data, ResultRow $values) { $this->options['alter']['make_link'] = TRUE; $this->options['alter']['url'] = Url::fromRoute('node.revision_revert_confirm', ['node' => $node->id(), 'node_revision' => $vid]); - $this->options['alter']['query'] = $this->redirectDestination->getAsArray(); + $this->options['alter']['query'] = $this->getDestination(); return !empty($this->options['text']) ? $this->options['text'] : $this->t('Revert'); } diff --git a/core/modules/node/tests/src/Unit/Plugin/views/field/NodeBulkFormTest.php b/core/modules/node/tests/src/Unit/Plugin/views/field/NodeBulkFormTest.php index 4f2ee00..8c5456c 100644 --- a/core/modules/node/tests/src/Unit/Plugin/views/field/NodeBulkFormTest.php +++ b/core/modules/node/tests/src/Unit/Plugin/views/field/NodeBulkFormTest.php @@ -51,8 +51,6 @@ public function testConstructor() { ->method('loadMultiple') ->will($this->returnValue($actions)); - $redirect_destination = $this->getMock('\Drupal\Core\Routing\RedirectDestinationInterface'); - $views_data = $this->getMockBuilder('Drupal\views\ViewsData') ->disableOriginalConstructor() ->getMock(); @@ -83,7 +81,7 @@ public function testConstructor() { $definition['title'] = ''; $options = array(); - $node_bulk_form = new NodeBulkForm(array(), 'node_bulk_form', $definition, $entity_storage, $redirect_destination); + $node_bulk_form = new NodeBulkForm(array(), 'node_bulk_form', $definition, $entity_storage); $node_bulk_form->init($executable, $display, $options); $this->assertAttributeEquals(array_slice($actions, 0, -1, TRUE), 'actions', $node_bulk_form); diff --git a/core/modules/system/src/Plugin/views/field/BulkForm.php b/core/modules/system/src/Plugin/views/field/BulkForm.php index 35d8669..d857401 100644 --- a/core/modules/system/src/Plugin/views/field/BulkForm.php +++ b/core/modules/system/src/Plugin/views/field/BulkForm.php @@ -9,7 +9,7 @@ use Drupal\Core\Entity\EntityStorageInterface; use Drupal\Core\Form\FormStateInterface; -use Drupal\Core\Routing\RedirectDestinationInterface; +use Drupal\Core\Routing\RedirectDestinationTrait; use Drupal\views\Plugin\views\display\DisplayPluginBase; use Drupal\views\Plugin\views\field\FieldPluginBase; use Drupal\views\Plugin\views\style\Table; @@ -25,6 +25,7 @@ */ class BulkForm extends FieldPluginBase { + use RedirectDestinationTrait; /** * The action storage. * @@ -40,13 +41,6 @@ class BulkForm extends FieldPluginBase { protected $actions = array(); /** - * The redirect destination service. - * - * @var \Drupal\Core\Routing\RedirectDestinationInterface; - */ - protected $redirectDestination; - - /** * Constructs a new BulkForm object. * * @param array $configuration @@ -57,21 +51,18 @@ class BulkForm extends FieldPluginBase { * The plugin implementation definition. * @param \Drupal\Core\Entity\EntityStorageInterface $storage * The action storage. - * @param \Drupal\Core\Routing\RedirectDestinationInterface $redirect_destination - * The redirect destination service. */ - public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityStorageInterface $storage, RedirectDestinationInterface $redirect_destination) { + public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityStorageInterface $storage) { parent::__construct($configuration, $plugin_id, $plugin_definition); $this->actionStorage = $storage; - $this->redirectDestination = $redirect_destination; } /** * {@inheritdoc} */ public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { - return new static($configuration, $plugin_id, $plugin_definition, $container->get('entity.manager')->getStorage('action'), $container->get('redirect.destination')); + return new static($configuration, $plugin_id, $plugin_definition, $container->get('entity.manager')->getStorage('action')); } /** @@ -295,7 +286,7 @@ public function viewsFormSubmit(&$form, FormStateInterface $form_state) { $operation_definition = $action->getPluginDefinition(); if (!empty($operation_definition['confirm_form_route_name'])) { $options = array( - 'query' => $this->redirectDestination->getAsArray(), + 'query' => $this->getDestination(), ); $form_state->setRedirect($operation_definition['confirm_form_route_name'], array(), $options); } diff --git a/core/modules/taxonomy/src/Plugin/views/field/LinkEdit.php b/core/modules/taxonomy/src/Plugin/views/field/LinkEdit.php index 5aa2396..7581c00 100644 --- a/core/modules/taxonomy/src/Plugin/views/field/LinkEdit.php +++ b/core/modules/taxonomy/src/Plugin/views/field/LinkEdit.php @@ -8,7 +8,7 @@ namespace Drupal\taxonomy\Plugin\views\field; use Drupal\Core\Form\FormStateInterface; -use Drupal\Core\Routing\RedirectDestinationInterface; +use Drupal\Core\Routing\RedirectDestinationTrait; use Drupal\Core\Url; use Drupal\views\Plugin\views\field\FieldPluginBase; use Drupal\views\Plugin\views\display\DisplayPluginBase; @@ -24,41 +24,7 @@ */ class LinkEdit extends FieldPluginBase { - /** - * The redirect destination service. - * - * @var \Drupal\Core\Routing\RedirectDestinationInterface - */ - protected $redirectDestination; - - /** - * Constructs a Link field plugin. - * - * @param array $configuration - * A configuration array containing information about the plugin instance. - * @param string $plugin_id - * The plugin_id for the plugin instance. - * @param mixed $plugin_definition - * The plugin implementation definition. - * @param \Drupal\Core\Routing\RedirectDestinationInterface $redirect_destination - * The redirect destination service. - */ - public function __construct(array $configuration, $plugin_id, $plugin_definition, RedirectDestinationInterface $redirect_destination) { - parent::__construct($configuration, $plugin_id, $plugin_definition); - $this->redirectDestination = $redirect_destination; - } - - /** - * {@inheritdoc} - */ - public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { - return new static( - $configuration, - $plugin_id, - $plugin_definition, - $container->get('redirect.destination') - ); - } + use RedirectDestinationTrait; /** * {@inheritdoc} @@ -115,7 +81,7 @@ public function render(ResultRow $values) { )); if ($term->access('update')) { $text = !empty($this->options['text']) ? $this->options['text'] : $this->t('Edit'); - return \Drupal::l($text, new Url('entity.taxonomy.edit_form', ['taxonomy_term' => $tid], array('query' => $this->redirectDestination->getAsArray()))); + return \Drupal::l($text, new Url('entity.taxonomy.edit_form', ['taxonomy_term' => $tid], array('query' => $this->getDestination()))); } } } diff --git a/core/modules/user/src/Plugin/Block/UserLoginBlock.php b/core/modules/user/src/Plugin/Block/UserLoginBlock.php index f1b0149..9a0efb5 100644 --- a/core/modules/user/src/Plugin/Block/UserLoginBlock.php +++ b/core/modules/user/src/Plugin/Block/UserLoginBlock.php @@ -8,6 +8,7 @@ namespace Drupal\user\Plugin\Block; use Drupal\Core\Plugin\ContainerFactoryPluginInterface; +use Drupal\Core\Routing\RedirectDestinationTrait; use Drupal\Core\Routing\RouteMatchInterface; use Drupal\Core\Routing\UrlGeneratorTrait; use Drupal\Core\Url; @@ -27,6 +28,7 @@ class UserLoginBlock extends BlockBase implements ContainerFactoryPluginInterface { use UrlGeneratorTrait; + use RedirectDestinationTrait; /** * The route match. @@ -87,7 +89,7 @@ public function build() { unset($form['pass']['#description']); $form['name']['#size'] = 15; $form['pass']['#size'] = 15; - $form['#action'] = $this->url('', [], ['query' => \Drupal::service('redirect.destination')->getAsArray(), 'external' => FALSE]); + $form['#action'] = $this->url('', [], ['query' => $this->getDestination(), 'external' => FALSE]); // Build action links. $items = array(); if (\Drupal::config('user.settings')->get('register') != USER_REGISTER_ADMINISTRATORS_ONLY) { diff --git a/core/modules/user/src/Plugin/views/field/LinkCancel.php b/core/modules/user/src/Plugin/views/field/LinkCancel.php index d41f179..489ac58 100644 --- a/core/modules/user/src/Plugin/views/field/LinkCancel.php +++ b/core/modules/user/src/Plugin/views/field/LinkCancel.php @@ -7,6 +7,7 @@ namespace Drupal\user\Plugin\views\field; +use Drupal\Core\Routing\RedirectDestinationTrait; use Drupal\Core\Entity\EntityInterface; use Drupal\views\ResultRow; @@ -19,6 +20,8 @@ */ class LinkCancel extends Link { + use RedirectDestinationTrait; + /** * {@inheritdoc} */ @@ -29,7 +32,7 @@ protected function renderLink(EntityInterface $entity, ResultRow $values) { $text = !empty($this->options['text']) ? $this->options['text'] : $this->t('Cancel account'); $this->options['alter']['url'] = $entity->urlInfo('cancel-form'); - $this->options['alter']['query'] = $this->redirectDestination->getAsArray(); + $this->options['alter']['query'] = $this->getDestination(); return $text; } diff --git a/core/modules/user/src/Plugin/views/field/LinkEdit.php b/core/modules/user/src/Plugin/views/field/LinkEdit.php index b2c9215..dd3b4cd 100644 --- a/core/modules/user/src/Plugin/views/field/LinkEdit.php +++ b/core/modules/user/src/Plugin/views/field/LinkEdit.php @@ -8,6 +8,7 @@ namespace Drupal\user\Plugin\views\field; use Drupal\Core\Entity\EntityInterface; +use Drupal\Core\Routing\RedirectDestinationTrait; use Drupal\views\ResultRow; /** @@ -19,6 +20,8 @@ */ class LinkEdit extends Link { + use RedirectDestinationTrait; + /** * {@inheritdoc} */ @@ -28,7 +31,7 @@ protected function renderLink(EntityInterface $entity, ResultRow $values) { $text = !empty($this->options['text']) ? $this->options['text'] : $this->t('Edit'); - $this->options['alter']['url'] = $entity->urlInfo('edit-form', ['query' => ['destination' => $this->redirectDestination->getAsArray()]]); + $this->options['alter']['url'] = $entity->urlInfo('edit-form', ['query' => ['destination' => $this->getDestination()]]); return $text; } diff --git a/core/modules/user/tests/src/Unit/Plugin/views/field/UserBulkFormTest.php b/core/modules/user/tests/src/Unit/Plugin/views/field/UserBulkFormTest.php index 4e442fd..0c389cd 100644 --- a/core/modules/user/tests/src/Unit/Plugin/views/field/UserBulkFormTest.php +++ b/core/modules/user/tests/src/Unit/Plugin/views/field/UserBulkFormTest.php @@ -51,8 +51,6 @@ public function testConstructor() { ->method('loadMultiple') ->will($this->returnValue($actions)); - $redirect_destination = $this->getMock('\Drupal\Core\Routing\RedirectDestinationInterface'); - $views_data = $this->getMockBuilder('Drupal\views\ViewsData') ->disableOriginalConstructor() ->getMock(); @@ -83,7 +81,7 @@ public function testConstructor() { $definition['title'] = ''; $options = array(); - $user_bulk_form = new UserBulkForm(array(), 'user_bulk_form', $definition, $entity_storage, $redirect_destination); + $user_bulk_form = new UserBulkForm(array(), 'user_bulk_form', $definition, $entity_storage); $user_bulk_form->init($executable, $display, $options); $this->assertAttributeEquals(array_slice($actions, 0, -1, TRUE), 'actions', $user_bulk_form); diff --git a/core/modules/views/src/Plugin/views/field/EntityOperations.php b/core/modules/views/src/Plugin/views/field/EntityOperations.php index 4a732a6..7678cae 100644 --- a/core/modules/views/src/Plugin/views/field/EntityOperations.php +++ b/core/modules/views/src/Plugin/views/field/EntityOperations.php @@ -9,7 +9,7 @@ use Drupal\Core\Entity\EntityManagerInterface; use Drupal\Core\Form\FormStateInterface; -use Drupal\Core\Routing\RedirectDestinationInterface; +use Drupal\Core\Routing\RedirectDestinationTrait; use Drupal\views\ResultRow; use Symfony\Component\DependencyInjection\ContainerInterface; @@ -22,6 +22,8 @@ */ class EntityOperations extends FieldPluginBase { + use RedirectDestinationTrait; + /** * The entity manager. * @@ -30,13 +32,6 @@ class EntityOperations extends FieldPluginBase { protected $entityManager; /** - * The redirect destination service. - * - * @var \Drupal\Core\Routing\RedirectDestinationInterface - */ - protected $redirectDestination; - - /** * Constructor. * * @param array $configuration @@ -47,13 +42,10 @@ class EntityOperations extends FieldPluginBase { * The plugin implementation definition. * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager * The entity manager. - * @param \Drupal\Core\Routing\RedirectDestinationInterface $redirect_destination - * The redirect destination service. */ - public function __construct(array $configuration, $plugin_id, array $plugin_definition, EntityManagerInterface $entity_manager, RedirectDestinationInterface $redirect_destination) { + public function __construct(array $configuration, $plugin_id, array $plugin_definition, EntityManagerInterface $entity_manager) { parent::__construct($configuration, $plugin_id, $plugin_definition); $this->entityManager = $entity_manager; - $this->redirectDestination = $redirect_destination; } /** @@ -64,8 +56,7 @@ public static function create(ContainerInterface $container, array $configuratio $configuration, $plugin_id, $plugin_definition, - $container->get('entity.manager'), - $container->get('redirect.destination') + $container->get('entity.manager') ); } @@ -114,7 +105,7 @@ public function render(ResultRow $values) { if (!isset($operation['query'])) { $operation['query'] = array(); } - $operation['query'] += $this->redirectDestination->getAsArray(); + $operation['query'] += $this->getDestination(); } } $build = array( diff --git a/core/modules/views/src/Tests/Plugin/views/field/EntityOperationsUnitTest.php b/core/modules/views/src/Tests/Plugin/views/field/EntityOperationsUnitTest.php index 2b6f24e..0f9ab96 100644 --- a/core/modules/views/src/Tests/Plugin/views/field/EntityOperationsUnitTest.php +++ b/core/modules/views/src/Tests/Plugin/views/field/EntityOperationsUnitTest.php @@ -8,7 +8,6 @@ namespace Drupal\views\Tests\Plugin\views\field { use Drupal\Tests\UnitTestCase; -use Drupal\views\Plugin\views\field\EntityOperations; use Drupal\views\ResultRow; /** @@ -25,16 +24,9 @@ class EntityOperationsUnitTest extends UnitTestCase { protected $entityManager; /** - * The mocked redirect.destination service. - * - * @var \Drupal\Core\Routing\RedirectDestinationInterface|\PHPUnit_Framework_MockObject_MockObject - */ - protected $redirectDestination; - - /** * The plugin under test. * - * @var \Drupal\views\Plugin\views\field\EntityOperations + * @var \Drupal\views\Plugin\views\field\EntityOperations|\PHPUnit_Framework_MockObject_MockObject */ protected $plugin; @@ -45,18 +37,21 @@ class EntityOperationsUnitTest extends UnitTestCase { */ public function setUp() { $this->entityManager = $this->getMock('\Drupal\Core\Entity\EntityManagerInterface'); - $this->redirectDestination = $this->getMock('\Drupal\Core\Routing\RedirectDestinationInterface'); - - $this->redirectDestination->expects($this->any()) - ->method('getAsArray') - ->willReturn(['destination' => 'foobar']); $configuration = array(); $plugin_id = $this->randomMachineName(); $plugin_definition = array( 'title' => $this->randomMachineName(), ); - $this->plugin = new EntityOperations($configuration, $plugin_id, $plugin_definition, $this->entityManager, $this->redirectDestination); + + $this->plugin = $this->getMockBuilder('Drupal\views\Plugin\views\field\EntityOperations') + ->setConstructorArgs([$configuration, $plugin_id, $plugin_definition, $this->entityManager]) + ->setMethods(array('getDestination')) + ->getMock(); + $this->plugin->expects($this->any()) + ->method('getDestination') + ->willReturn(['destination' => 'foobar']); + $view = $this->getMockBuilder('\Drupal\views\ViewExecutable') ->disableOriginalConstructor() ->getMock(); @@ -120,7 +115,7 @@ public function testRenderWithDestination() { '#type' => 'operations', '#links' => $operations ); - $expected_build['#links']['foo']['query'] = $this->redirectDestination->getAsArray(); + $expected_build['#links']['foo']['query'] = ['destination' => 'foobar']; $build = $this->plugin->render($result); $this->assertSame($expected_build, $build); }