Skip to content
Snippets Groups Projects
Commit 002b8aee authored by Mark Fullmer's avatar Mark Fullmer Committed by Mark Fullmer
Browse files

Issue #3359375 by mrshowerman, mark_fullmer: Query fragments or anchors on...

Issue #3359375 by mrshowerman, mark_fullmer: Query fragments or anchors on internal URLs in link fields get lost on re-editing
parent 26de83be
No related branches found
No related tags found
3 merge requests!78Issue #3472672: Fix the issues reported by PHP_CodeSniffer,!59Revert "Issue #2900766 by rubendello: Not all autocomplete values are...,!40Issue #3262401 by Dom., Berdir: Deprecated function : Return type of...
......@@ -163,7 +163,18 @@ class LinkitWidget extends LinkWidget {
$element['uri']['#default_value'] = \Drupal::service('file_url_generator')->generateString($entity->getFileUri());
}
elseif ($entity instanceof EntityInterface) {
$element['uri']['#default_value'] = $entity->toUrl()->toString();
$uri_parts = parse_url($uri);
$uri_options = [];
// Extract query parameters and fragment and merge them into $uri_options.
if (isset($uri_parts['fragment']) && $uri_parts['fragment'] !== '') {
$uri_options += ['fragment' => $uri_parts['fragment']];
}
if (!empty($uri_parts['query'])) {
$uri_query = [];
parse_str($uri_parts['query'], $uri_query);
$uri_options['query'] = isset($uri_options['query']) ? $uri_options['query'] + $uri_query : $uri_query;
}
$element['uri']['#default_value'] = $entity->toUrl()->setOptions($uri_options)->toString();
}
// Change the URI field to use the linkit profile.
$element['uri']['#type'] = 'linkit';
......
......@@ -177,7 +177,7 @@ class LinkFieldTest extends WebDriverTestBase {
$assert_session->elementAttributeContains('xpath', '//article/div/div/div[2]/a', 'target', "_blank");
$assert_session->linkExists('Foo');
// Test internal entity targets with anchors.
// Test internal entity targets with anchors and query parameters.
/** @var \Drupal\Core\Entity\EntityInterface $entity */
$entity2 = EntityTestMul::create(['name' => 'Anchored Entity']);
$entity2->save();
......@@ -202,7 +202,7 @@ class LinkFieldTest extends WebDriverTestBase {
$this->assertEquals('Anchored Entity', $title_input->getValue());
// Add an anchor to the URL field.
$url_input->setValue($entity2->toUrl()->toString() . '#with-anchor');
$url_input->setValue($entity2->toUrl()->toString() . '#with-anchor?search=1');
// Give the node a title and save the page.
$page->fillField('title[0][value]', 'Host test node 2');
......@@ -211,9 +211,14 @@ class LinkFieldTest extends WebDriverTestBase {
// Check that we are viewing the node, and the formatter displays what we
// expect.
$assert_session->linkByHrefExists("/entity_test_mul/manage/{$entity2->id()}#with-anchor");
$assert_session->linkByHrefExists("/entity_test_mul/manage/{$entity2->id()}#with-anchor?search=1");
$assert_session->linkExists('Anchored Entity');
// Verify anchor persists when visiting the edit form.
$this->drupalGet('node/2/edit');
$url_input = $assert_session->elementExists('css', 'input[name="field_test_link[0][uri]"]', $widget_wrapper);
$this->assertEquals($entity2->toUrl()->toString() . '#with-anchor?search=1', $url_input->getValue());
// Test external URLs.
$this->drupalGet('node/add/page');
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment