Skip to content

Commit

Permalink
ENH: Query string support.
Browse files Browse the repository at this point in the history
  • Loading branch information
mfendeksilverstripe committed Jun 21, 2023
1 parent 6b69001 commit ba6293a
Show file tree
Hide file tree
Showing 4 changed files with 152 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/Models/ExternalLink.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@ public function generateLinkDescription(array $data): string

public function getURL()
{
return $this->ExternalUrl;
return (string) $this->ExternalUrl;
}
}
28 changes: 21 additions & 7 deletions src/Models/SiteTreeLink.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,26 @@

use SilverStripe\CMS\Forms\AnchorSelectorField;
use SilverStripe\CMS\Model\SiteTree;
use SilverStripe\Control\Controller;
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\TextField;
use SilverStripe\Forms\TreeDropdownField;

/**
* A link to a Page in the CMS
*
* @property int $PageID
* @property string $Anchor
* @property string $QueryString
* @method SiteTree Page()
*/
class SiteTreeLink extends Link
{
private static $table_name = 'LinkField_SiteTreeLink';

private static $db = [
'Anchor' => 'Varchar'
'Anchor' => 'Varchar',
'QueryString' => 'Varchar',
];

private static $has_one = [
Expand All @@ -45,6 +49,7 @@ public function getCMSFields(): FieldList
$fields->removeByName([
'PageID',
'Anchor',
'QueryString',
]);

$fields->insertAfter(
Expand All @@ -60,7 +65,18 @@ public function getCMSFields(): FieldList

$fields->insertAfter(
'PageID',
AnchorSelectorField::create('Anchor')
$queryStringField = TextField::create('QueryString')
);

$queryStringField->setDescription('Do not prepend "?". EG: "option1=value&option2=value2"');

$fields->insertAfter(
'QueryString',
$anchorField = AnchorSelectorField::create('Anchor')
);

$anchorField->setDescription(
'Do not prepend "#". Anchor suggestions will be displayed once the linked page is attached.'
);
});

Expand All @@ -71,12 +87,10 @@ public function getURL(): ?string
{
$page = $this->Page();
$url = $page->exists() ? $page->Link() : '';
$anchorSegment = $this->Anchor ? '#' . $this->Anchor : '';
$queryStringSegment = $this->QueryString ? '?' . $this->QueryString : '';

if ($this->Anchor) {
$url .= '#' . $this->Anchor;
}

return $url;
return Controller::join_links($url, $anchorSegment, $queryStringSegment);
}

public function onBeforeWrite(): void
Expand Down
81 changes: 81 additions & 0 deletions tests/php/Models/LinkTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -204,4 +204,85 @@ public function linkTypeEnabledProvider(): array
],
];
}

/**
* @param string $identifier
* @param string $class
* @param string $expected
* @return void
* @dataProvider linkUrlCasesDataProvider
*/
public function testGetUrl(string $identifier, string $class, string $expected): void
{
/** @var Link $link */
$link = $this->objFromFixture($class, $identifier);

$this->assertSame($expected, $link->getURL(), 'We expect specific URL value');
}

public function linkUrlCasesDataProvider(): array
{
return [
'internal link / page only' => [
'page-link-page-only',
SiteTreeLink::class,
'/page-1/',
],
'internal link / anchor only' => [
'page-link-anchor-only',
SiteTreeLink::class,
'#my-anchor',
],
'internal link / query string only' => [
'page-link-query-string-only',
SiteTreeLink::class,
'?param1=value1&param2=option2',
],
'internal link / with anchor' => [
'page-link-with-anchor',
SiteTreeLink::class,
'/page-1/#my-anchor',
],
'internal link / with query string' => [
'page-link-with-query-string',
SiteTreeLink::class,
'/page-1/?param1=value1&param2=option2',
],
'internal link / with query string and anchor' => [
'page-link-with-query-string-and-anchor',
SiteTreeLink::class,
'/page-1/?param1=value1&param2=option2#my-anchor',
],
'email link / with email' => [
'email-link-with-email',
EmailLink::class,
'mailto:maxime@silverstripe.com',
],
'email link / no email' => [
'email-link-no-email',
EmailLink::class,
'',
],
'external link / with URL' => [
'external-link-with-url',
ExternalLink::class,
'https://google.com',
],
'external link / no URL' => [
'external-link-no-url',
ExternalLink::class,
'',
],
'phone link / with phone' => [
'phone-link-with-phone',
PhoneLink::class,
'tel:+64 4 978 7330',
],
'phone link / no phone' => [
'phone-link-no-phone',
PhoneLink::class,
'',
],
];
}
}
54 changes: 49 additions & 5 deletions tests/php/Models/LinkTest.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,55 @@
SilverStripe\CMS\Model\SiteTree:
page-1:
Title: 'Page1'
URLSegment: 'page-1'

SilverStripe\LinkField\Models\Link:
link-1:
Title: Link1
Title: 'Link1'

SilverStripe\LinkField\Models\SiteTreeLink:
page-link-1:
Title: PageLink1
Title: 'PageLink1'
page-link-page-only:
Title: 'PageLinkPageOnly'
Page: =>SilverStripe\CMS\Model\SiteTree.page-1
page-link-anchor-only:
Title: 'PageLinkAnchorOnly'
Anchor: 'my-anchor'
page-link-query-string-only:
Title: 'PageLinkQueryStringOnly'
QueryString: 'param1=value1&param2=option2'
page-link-with-anchor:
Title: 'PageLinkWithAnchor'
Anchor: 'my-anchor'
Page: =>SilverStripe\CMS\Model\SiteTree.page-1
page-link-with-query-string:
Title: 'PageLinkWithQueryString'
QueryString: 'param1=value1&param2=option2'
Page: =>SilverStripe\CMS\Model\SiteTree.page-1
page-link-with-query-string-and-anchor:
Title: 'PageLinkWithQueryStringAndAnchor'
QueryString: 'param1=value1&param2=option2'
Anchor: 'my-anchor'
Page: =>SilverStripe\CMS\Model\SiteTree.page-1

SilverStripe\CMS\Model\SiteTree:
page-1:
Title: Page1
SilverStripe\LinkField\Models\EmailLink:
email-link-with-email:
Title: 'EmailLinkWithEmail'
Email: 'maxime@silverstripe.com'
email-link-no-email:
Title: 'EmailLinkNoEmail'

SilverStripe\LinkField\Models\ExternalLink:
external-link-with-url:
Title: 'ExternalLinkWithUrl'
ExternalUrl: 'https://google.com'
external-link-no-url:
Title: 'ExternalLinkNoUrl'

SilverStripe\LinkField\Models\PhoneLink:
phone-link-with-phone:
Title: 'PhoneLinkWithPhone'
Phone: '+64 4 978 7330'
phone-link-no-phone:
Title: 'PhoneLinkNoPhone'

0 comments on commit ba6293a

Please sign in to comment.