diff --git a/core/modules/block_content/src/BlockContentForm.php b/core/modules/block_content/src/BlockContentForm.php index 7c97c6a6d1d287b5cf3b15e5b4748f89e4198335..1510577f5b414b8badb662d9763560482c2d4661 100644 --- a/core/modules/block_content/src/BlockContentForm.php +++ b/core/modules/block_content/src/BlockContentForm.php @@ -115,6 +115,7 @@ public function save(array $form, FormStateInterface $form_state) { [ 'plugin_id' => 'block_content:' . $block->uuid(), 'theme' => $theme, + 'region' => $this->getRequest()->query->getString('region'), ] ); } diff --git a/core/modules/block_content/src/Plugin/Menu/LocalAction/BlockContentAddLocalAction.php b/core/modules/block_content/src/Plugin/Menu/LocalAction/BlockContentAddLocalAction.php index 9b1e220796cb99cef93aa3b23385b9cefbf6e474..844e06895cc583c2075294f33ec3143f16e5cb4d 100644 --- a/core/modules/block_content/src/Plugin/Menu/LocalAction/BlockContentAddLocalAction.php +++ b/core/modules/block_content/src/Plugin/Menu/LocalAction/BlockContentAddLocalAction.php @@ -4,13 +4,42 @@ use Drupal\Core\Menu\LocalActionDefault; use Drupal\Core\Routing\RouteMatchInterface; +use Drupal\Core\Routing\RouteProviderInterface; use Drupal\Core\Url; +use Symfony\Component\DependencyInjection\ContainerInterface; +use Symfony\Component\HttpFoundation\RequestStack; /** * Modifies the 'Add content block' local action. */ class BlockContentAddLocalAction extends LocalActionDefault { + /** + * Constructs a BlockContentAddLocalAction object. + */ + public function __construct( + array $configuration, + $plugin_id, + $plugin_definition, + RouteProviderInterface $routeProvider, + protected RequestStack $requestStack, + ) { + parent::__construct($configuration, $plugin_id, $plugin_definition, $routeProvider); + } + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { + return new static( + $configuration, + $plugin_id, + $plugin_definition, + $container->get('router.route_provider'), + $container->get('request_stack'), + ); + } + /** * {@inheritdoc} */ @@ -20,6 +49,12 @@ public function getOptions(RouteMatchInterface $route_match) { if ($theme = $route_match->getParameter('theme')) { $options['query']['theme'] = $theme; } + + // If the current request has a region, append it to the query string. + if ($region = $this->requestStack->getCurrentRequest()->query->getString('region')) { + $options['query']['region'] = $region; + } + // Adds a destination on content block listing. if ($route_match->getRouteName() == 'entity.block_content.collection') { $options['query']['destination'] = Url::fromRoute('<current>')->toString(); diff --git a/core/modules/block_content/tests/src/Functional/BlockContentListTest.php b/core/modules/block_content/tests/src/Functional/BlockContentListTest.php index 4271b54daaf8e7d16b6d62948f22915c782e5715..15b5a3c49a78ec9c52fddd6bf8c5238c0eb2d04e 100644 --- a/core/modules/block_content/tests/src/Functional/BlockContentListTest.php +++ b/core/modules/block_content/tests/src/Functional/BlockContentListTest.php @@ -71,6 +71,22 @@ protected function setUp(): void { ]); } + /** + * Tests the region value when a new block is saved. + */ + public function testBlockRegionPlacement(): void { + $this->drupalLogin($this->drupalCreateUser($this->permissions)); + $this->drupalGet("admin/structure/block/library/stark", ['query' => ['region' => 'content']]); + + $this->clickLink('Add content block'); + $this->assertSession()->statusCodeEquals(200); + $edit = [ + 'info[0][value]' => 'foo', + ]; + $this->submitForm($edit, 'Save'); + $this->assertSession()->fieldValueEquals('region', 'content'); + } + /** * Tests the content block listing page with different permissions. */