Commit f6ecafb3 authored by webchick's avatar webchick

Issue #2745911 by tim.plunkett, pwolanin: Block add links should respect destination

parent 12a6963f
...@@ -8,6 +8,7 @@ ...@@ -8,6 +8,7 @@
use Drupal\Core\EventSubscriber\MainContentViewSubscriber; use Drupal\Core\EventSubscriber\MainContentViewSubscriber;
use Drupal\Core\Menu\LocalActionManagerInterface; use Drupal\Core\Menu\LocalActionManagerInterface;
use Drupal\Core\Plugin\Context\LazyContextRepository; use Drupal\Core\Plugin\Context\LazyContextRepository;
use Drupal\Core\Routing\RedirectDestinationInterface;
use Drupal\Core\Routing\RouteMatchInterface; use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Url; use Drupal\Core\Url;
use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\DependencyInjection\ContainerInterface;
...@@ -46,6 +47,13 @@ class BlockLibraryController extends ControllerBase { ...@@ -46,6 +47,13 @@ class BlockLibraryController extends ControllerBase {
*/ */
protected $localActionManager; protected $localActionManager;
/**
* The redirect destination.
*
* @var \Drupal\Core\Routing\RedirectDestinationInterface
*/
protected $redirectDestination;
/** /**
* Constructs a BlockLibraryController object. * Constructs a BlockLibraryController object.
* *
...@@ -57,12 +65,15 @@ class BlockLibraryController extends ControllerBase { ...@@ -57,12 +65,15 @@ class BlockLibraryController extends ControllerBase {
* The current route match. * The current route match.
* @param \Drupal\Core\Menu\LocalActionManagerInterface $local_action_manager * @param \Drupal\Core\Menu\LocalActionManagerInterface $local_action_manager
* The local action manager. * The local action manager.
* @param \Drupal\Core\Routing\RedirectDestinationInterface $redirect_destination
* The redirect destination.
*/ */
public function __construct(BlockManagerInterface $block_manager, LazyContextRepository $context_repository, RouteMatchInterface $route_match, LocalActionManagerInterface $local_action_manager) { public function __construct(BlockManagerInterface $block_manager, LazyContextRepository $context_repository, RouteMatchInterface $route_match, LocalActionManagerInterface $local_action_manager, RedirectDestinationInterface $redirect_destination) {
$this->blockManager = $block_manager; $this->blockManager = $block_manager;
$this->routeMatch = $route_match; $this->routeMatch = $route_match;
$this->localActionManager = $local_action_manager; $this->localActionManager = $local_action_manager;
$this->contextRepository = $context_repository; $this->contextRepository = $context_repository;
$this->redirectDestination = $redirect_destination;
} }
/** /**
...@@ -73,7 +84,8 @@ public static function create(ContainerInterface $container) { ...@@ -73,7 +84,8 @@ public static function create(ContainerInterface $container) {
$container->get('plugin.manager.block'), $container->get('plugin.manager.block'),
$container->get('context.repository'), $container->get('context.repository'),
$container->get('current_route_match'), $container->get('current_route_match'),
$container->get('plugin.manager.menu.local_action') $container->get('plugin.manager.menu.local_action'),
$container->get('redirect.destination')
); );
} }
...@@ -132,6 +144,10 @@ public function listBlocks(Request $request, $theme) { ...@@ -132,6 +144,10 @@ public function listBlocks(Request $request, $theme) {
if ($region) { if ($region) {
$links['add']['query']['region'] = $region; $links['add']['query']['region'] = $region;
} }
$destination = $this->redirectDestination->get();
if ($destination) {
$links['add']['query']['destination'] = $destination;
}
$row['operations']['data'] = [ $row['operations']['data'] = [
'#type' => 'operations', '#type' => 'operations',
'#links' => $links, '#links' => $links,
......
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
use Drupal\Component\Utility\Html; use Drupal\Component\Utility\Html;
use Drupal\block\Entity\Block; use Drupal\block\Entity\Block;
use Drupal\Core\Url;
use Drupal\user\Entity\Role; use Drupal\user\Entity\Role;
use Drupal\user\RoleInterface; use Drupal\user\RoleInterface;
...@@ -127,6 +128,49 @@ function testBlockVisibilityListedEmpty() { ...@@ -127,6 +128,49 @@ function testBlockVisibilityListedEmpty() {
$this->assertNoText($title, 'Block was not displayed to anonymous users on the front page.'); $this->assertNoText($title, 'Block was not displayed to anonymous users on the front page.');
} }
/**
* Tests adding a block from the library page with a destination query string.
*/
public function testAddBlockFromLibrary() {
$default_theme = $this->config('system.theme')->get('default');
$help_url = Url::fromRoute('help.page', ['name' => 'block']);
// Set up the request so we land on the block help page after creation.
$options = [
'query' => [
'region' => 'sidebar_first',
'destination' => $help_url->toString(),
],
];
$this->drupalGet(Url::fromRoute('block.admin_library', ['theme' => $default_theme], $options));
$block_name = 'system_powered_by_block';
$add_url = Url::fromRoute('block.admin_add', ['plugin_id' => $block_name, 'theme' => $default_theme]);
$links = $this->xpath('//a[contains(@href, :href)]', [':href' => $add_url->toString()]);
$this->assertEqual(1, count($links), 'Found one matching link');
list($path, $query_string) = explode('?', $links[0]['href'], 2);
parse_str($query_string, $query_parts);
$this->assertEqual(t('Place block'), (string) $links[0]);
$this->assertEqual($help_url->toString(), $query_parts['destination'], 'Expected destination query string is in href');
// Create a random title for the block.
$title = $this->randomMachineName(8);
$block_id = strtolower($this->randomMachineName(8));
$edit = [
'id' => $block_id,
'settings[label]' => $title,
];
// Create the block using the link parsed from the library page.
$this->drupalPostForm($this->getAbsoluteUrl($links[0]['href']), $edit, t('Save block'));
// Verify that we are redirected according to the original request.
$this->assertUrl($help_url);
// Ensure that the block was created.
/** @var \Drupal\block\BlockInterface $block */
$block = Block::load($block_id);
$this->assertEqual($title, $block->label(), 'Found the block with expected title.');
}
/** /**
* Test configuring and moving a module-define block to specific regions. * Test configuring and moving a module-define block to specific regions.
*/ */
......
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