Skip to content

Commit 3045d01

Browse files
author
Nathaniel Catchpole
committed
Issue #3018942 by welly, alexpott, jibran, Krzysztof Domański, floydm: Domain URL language detection - InvalidArgumentException: The user-entered string must begin with a '/', '?', or '#'
(cherry picked from commit 79e94b4)
1 parent cd9ddea commit 3045d01

File tree

3 files changed

+45
-47
lines changed

3 files changed

+45
-47
lines changed

modules/block/src/BlockListBuilder.php

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,9 @@ protected function buildBlocksForm() {
193193
if ($this->request->query->has('block-placement')) {
194194
$placement = $this->request->query->get('block-placement');
195195
$form['#attached']['drupalSettings']['blockPlacement'] = $placement;
196+
// Remove the block placement from the current request so that it is not
197+
// passed on to any redirect destinations.
198+
$this->request->query->remove('block-placement');
196199
}
197200

198201
// Loop over each region and build blocks.
@@ -354,23 +357,6 @@ public function getDefaultOperations(EntityInterface $entity) {
354357

355358
if (isset($operations['delete'])) {
356359
$operations['delete']['title'] = $this->t('Remove');
357-
// Block operation links should have the `block-placement` query string
358-
// parameter removed to ensure that JavaScript does not receive a block
359-
// name that has been recently removed.
360-
foreach ($operations as $operation) {
361-
/** @var \Drupal\Core\Url $url */
362-
$url = $operation['url'];
363-
$query = $url->getOption('query');
364-
$destination = $query['destination'];
365-
366-
$destinationUrl = Url::fromUserInput($destination);
367-
$destinationQuery = $destinationUrl->getOption('query');
368-
unset($destinationQuery['block-placement']);
369-
370-
$destinationUrl->setOption('query', $destinationQuery);
371-
$query['destination'] = $destinationUrl->toString();
372-
$url->setOption('query', $query);
373-
}
374360
}
375361
return $operations;
376362
}
@@ -395,9 +381,6 @@ public function submitForm(array &$form, FormStateInterface $form_state) {
395381
$entity->save();
396382
}
397383
$this->messenger->addStatus($this->t('The block settings have been updated.'));
398-
399-
// Remove any previously set block placement.
400-
$this->request->query->remove('block-placement');
401384
}
402385

403386
/**

modules/block/tests/src/Functional/BlockTest.php

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -238,30 +238,6 @@ public function testBlock() {
238238
$this->assertNoRaw($block->id());
239239
}
240240

241-
/**
242-
* Tests the block operation links.
243-
*/
244-
public function testBlockOperationLinks() {
245-
$this->drupalGet('admin/structure/block');
246-
// Go to the select block form.
247-
$this->clickLink('Place block');
248-
// Select the first available block.
249-
$this->clickLink('Place block');
250-
// Finally place the block
251-
$this->submitForm([], 'Save block');
252-
253-
$url = $this->getUrl();
254-
$parsed = parse_url($url);
255-
$this->assertContains('block-placement', $parsed['query']);
256-
257-
$this->clickLink('Remove');
258-
$this->submitForm([], 'Remove');
259-
260-
$url = $this->getUrl();
261-
$parsed = parse_url($url);
262-
$this->assertTrue(empty($parsed['query']));
263-
}
264-
265241
/**
266242
* Tests that the block form has a theme selector when not passed via the URL.
267243
*/

modules/block/tests/src/Functional/BlockUiTest.php

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
namespace Drupal\Tests\block\Functional;
44

55
use Drupal\Component\Utility\Html;
6+
use Drupal\language\Entity\ConfigurableLanguage;
7+
use Drupal\language\Plugin\LanguageNegotiation\LanguageNegotiationUrl;
68
use Drupal\Tests\BrowserTestBase;
79

810
/**
@@ -288,6 +290,24 @@ public function testMachineNameSuggestion() {
288290
* Tests the block placement indicator.
289291
*/
290292
public function testBlockPlacementIndicator() {
293+
// Test the block placement indicator with using the domain as URL language
294+
// indicator. This causes destination query parameters to be absolute URLs.
295+
\Drupal::service('module_installer')->install(['language', 'locale']);
296+
$this->container = \Drupal::getContainer();
297+
ConfigurableLanguage::createFromLangcode('it')->save();
298+
$config = $this->config('language.types');
299+
$config->set('negotiation.language_interface.enabled', [
300+
LanguageNegotiationUrl::METHOD_ID => -10,
301+
]);
302+
$config->save();
303+
$config = $this->config('language.negotiation');
304+
$config->set('url.source', LanguageNegotiationUrl::CONFIG_DOMAIN);
305+
$config->set('url.domains', [
306+
'en' => \Drupal::request()->getHost(),
307+
'it' => 'it.example.com',
308+
]);
309+
$config->save();
310+
291311
// Select the 'Powered by Drupal' block to be placed.
292312
$block = [];
293313
$block['id'] = strtolower($this->randomMachineName());
@@ -296,11 +316,30 @@ public function testBlockPlacementIndicator() {
296316

297317
// After adding a block, it will indicate which block was just added.
298318
$this->drupalPostForm('admin/structure/block/add/system_powered_by_block', $block, t('Save block'));
299-
$this->assertUrl('admin/structure/block/list/classy?block-placement=' . Html::getClass($block['id']));
319+
$this->assertSession()->addressEquals('admin/structure/block/list/classy?block-placement=' . Html::getClass($block['id']));
300320

301-
// Resaving the block page will remove the block indicator.
321+
// Resaving the block page will remove the block placement indicator.
302322
$this->drupalPostForm(NULL, [], t('Save blocks'));
303-
$this->assertUrl('admin/structure/block/list/classy');
323+
$this->assertSession()->addressEquals('admin/structure/block/list/classy');
324+
325+
// Place another block and test the remove functionality works with the
326+
// block placement indicator. Click the first 'Place block' link to bring up
327+
// the list of blocks to place in the first available region.
328+
$this->clickLink('Place block');
329+
// Select the first available block.
330+
$this->clickLink('Place block');
331+
$block = [];
332+
$block['id'] = strtolower($this->randomMachineName());
333+
$block['theme'] = 'classy';
334+
$this->submitForm([], 'Save block');
335+
$this->assertSession()->addressEquals('admin/structure/block/list/classy?block-placement=' . Html::getClass($block['id']));
336+
337+
// Removing a block will remove the block placement indicator.
338+
$this->clickLink('Remove');
339+
$this->submitForm([], 'Remove');
340+
// @todo https://www.drupal.org/project/drupal/issues/2980527 this should be
341+
// 'admin/structure/block/list/classy' but there is a bug.
342+
$this->assertSession()->addressEquals('admin/structure/block');
304343
}
305344

306345
/**

0 commit comments

Comments
 (0)