Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add possibility for query parameters in sulu link #6440

Merged
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 33 additions & 10 deletions src/Sulu/Bundle/MarkupBundle/Markup/LinkTag.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,10 @@ public function parseAll(array $attributesByTag, $locale)
$provider = $this->getValue($attributes, 'provider', self::DEFAULT_PROVIDER);
$validationState = $attributes['sulu-validation-state'] ?? null;

$hrefParts = $this->getUuidAndAnchorFromHref($attributes['href'] ?? null);
$hrefParts = $this->getPartsFromHref($attributes['href'] ?? null);
$uuid = $hrefParts['uuid'];
$anchor = $hrefParts['anchor'];
$query = $hrefParts['query'];

if ($uuid && \array_key_exists($provider . '-' . $uuid, $contents)) {
$item = $contents[$provider . '-' . $uuid];
Expand All @@ -77,6 +78,10 @@ public function parseAll(array $attributesByTag, $locale)
$url = $this->urlHelper->getAbsoluteUrl($url);
}

if ($query) {
$url .= '?' . $query;
}

if ($anchor) {
$url .= '#' . $anchor;
}
Expand Down Expand Up @@ -125,7 +130,7 @@ public function validateAll(array $attributesByTag, $locale)
$result = [];
foreach ($attributesByTag as $tag => $attributes) {
$provider = $this->getValue($attributes, 'provider', self::DEFAULT_PROVIDER);
$uuid = $this->getUuidAndAnchorFromHref($attributes['href'] ?? null)['uuid'];
$uuid = $this->getPartsFromHref($attributes['href'] ?? null)['uuid'];

if (!$uuid || !\array_key_exists($provider . '-' . $uuid, $items)) {
$result[$tag] = self::VALIDATE_REMOVED;
Expand Down Expand Up @@ -155,7 +160,7 @@ private function preload($attributesByTag, $locale, $published = true)
$uuidsByType[$provider] = [];
}

$uuid = $this->getUuidAndAnchorFromHref($attributes['href'] ?? null)['uuid'];
$uuid = $this->getPartsFromHref($attributes['href'] ?? null)['uuid'];

if ($uuid) {
$uuidsByType[$provider][] = $uuid;
Expand Down Expand Up @@ -209,21 +214,39 @@ private function getContent(array $attributes)
/**
* @param mixed $href
*
* @return array{uuid: string|null, anchor: string|null}
* @return array{uuid: string|null, query: string|null, anchor: string|null}
*/
private function getUuidAndAnchorFromHref($href): array
private function getPartsFromHref($href): array
{
$href = (string) $href ?: null;

/** @var string[] $hrefParts */
$hrefParts = $href ? \explode('#', $href, 2) : [];

$uuid = $hrefParts[0] ?? null;
$anchor = $hrefParts[1] ?? null;
$query = null;
$anchor = null;

$hasQuery = strpos($href, '?');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@exastion Something like my.url/#hello?page=1 means that hello?page=1 is the value of the anchor and it still a valid anchor. This is as example used in Single Page Application and are valid anchors. Sulu example use this kind of special anchor for frontend navigation.

That's why its important first to split the string by # and then go with split the first part by ? and not the other way around. So there is no problem also from backward compatibility we are not allowed to change to seperate attributes and as said its not really required.

So the following examples should show you best how the end result should look like:

<uuid>?test=query#anchor

ID: <uuid>
query: test=query
anchor: anchor
<uuid>#anchor?test=query

ID: <uuid>
query: 
anchor: anchor?test=query
<uuid>?test=query

ID: <uuid>
query: test=query
anchor: 

See https://3v4l.org/j94IN#v7.2.34 how to correctly split the string. A ? is a valid symbol as part of the anchor so we first need to split by # and then split the part before the anchor by #.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did not know about these specifics up to now, I even thought in the beginning, query and anchor would be the other way around.. Well you always learn :)
This made things also easier.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also nice to know the #anchor is never send to a server its only available for the browser. So if your browser requests https://sulu.io/services/support#chat a request is only send to https://sulu.io/services/support and browsers is handling #chat to scroll to the correct part. Or a javascript library is handling the #anchor like its done in the sulu admin.

$hasAnchor = strpos($href, '#');
if ($hasQuery === false && $hasAnchor === false) {
$uuid = $href;
} else if ($hasQuery === false) { // and $hasAnchor !== false
$parts = \explode('#', $href, 2);
$uuid = $parts[0];
$anchor = $parts[1];
} else if ($hasAnchor === false) { // and $hasQuery !== false
$parts = \explode('?', $href, 2);
$uuid = $parts[0];
$query = $parts[1];
} else { // both !== false
$parts = \explode('?', $href, 2);
$uuid = $parts[0];
$parts = \explode('#', $parts[1], 2);
$query = $parts[0];
$anchor = $parts[1];
}

return [
'uuid' => $uuid,
'anchor' => $anchor,
'query' => $query,
];
}
}
24 changes: 23 additions & 1 deletion src/Sulu/Bundle/MarkupBundle/Tests/Unit/Markup/LinkTagTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,28 @@ public function provideParseData()
[new LinkItem('123-123-123', 'Page-Title', '/de/test', true)],
'<a href="http://sulu.lo/de/test#anchor" title="Test-Title">Test-Content</a>',
],
[
'<sulu-link href="123-123-123?query=parameter" provider="article" title="Test-Title">Test-Content</sulu-link>',
[
'href' => '123-123-123?query=parameter',
'title' => 'Test-Title',
'provider' => 'article',
'content' => 'Test-Content',
],
[new LinkItem('123-123-123', 'Page-Title', '/de/test', true)],
'<a href="http://sulu.lo/de/test?query=parameter" title="Test-Title">Test-Content</a>',
],
[
'<sulu-link href="123-123-123?query=parameter#anchor" provider="article" title="Test-Title">Test-Content</sulu-link>',
[
'href' => '123-123-123?query=parameter#anchor',
'title' => 'Test-Title',
'provider' => 'article',
'content' => 'Test-Content',
],
[new LinkItem('123-123-123', 'Page-Title', '/de/test', true)],
'<a href="http://sulu.lo/de/test?query=parameter#anchor" title="Test-Title">Test-Content</a>',
],
];
}

Expand All @@ -161,7 +183,7 @@ public function provideParseData()
public function testParseAll($tag, $attributes, $items, $expected)
{
$uuids = [
\explode('#', $attributes['href'], 2)[0],
\preg_split('/[#?]/', $attributes['href'], 2)[0],
];

$this->providers[$attributes['provider']]->preload($uuids, 'de', true)->willReturn($items);
Expand Down