Skip to content

Commit

Permalink
NEW Default link title for each link type
Browse files Browse the repository at this point in the history
  • Loading branch information
Sabina Talipova committed Dec 4, 2023
1 parent 9debde6 commit d72d856
Show file tree
Hide file tree
Showing 12 changed files with 184 additions and 1 deletion.
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,29 @@ class Page extends SiteTree

Note that you also need to add a `has_one` relation on the `Link` model to match your `has_many` here. See [official docs about `has_many`](https://docs.silverstripe.org/en/developer_guides/model/relations/#has-many)

## Default title for each link type

By default, if the title for the link has not been set, then the default title will be used instead according to the type of link that is used. Default link is not stored in the database as link title. This value is used only when rendering page content.

The developer also can set his own default title value using an extension by using `updateDefaultLinkTitle` method for each link type class.
*app/src/ExternalLinkExtension*
```php
<?php

namespace App\Extensions;

use SilverStripe\Core\Extension;

class ExternalLinkExtension extends Extension
{
public function updateDefaultLinkTitle(&$defaultLinkTitle): void
{
$defaultLinkTitle = sprintf('External link: %s', $this->owner->ExternalUrl);
}
}

```

## Migrating from Shae Dawson's Linkable module

https://github.com/sheadawson/silverstripe-linkable
Expand Down
3 changes: 3 additions & 0 deletions lang/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ en:
KEYS_ARE_NOT_ARRAY: 'If `keys` is provdied, it must be an array'
LINK_TYPE_TITLE: 'Link Type'
LINK_FIELD_TITLE: 'Title'
LINK_FIELD_TITLE_DESCRIPTION: 'If left blank, default title will be used'
NO_CLASSNAME: '"{class}": All types should reference a valid classname'
NOT_REGISTERED_LINKTYPE: '"{class}": "{typekey}" is not a registered Link Type.'
NOTHING_TO_PROCESS: "Nothing to process for `{table}`\r\n"
Expand All @@ -32,3 +33,5 @@ en:
UNAUTHORIZED: 'Unauthorized'
UPDATE_LINK: 'Update link'
VERSIONED_STATUS_MISMATCH: 'Linkable and LinkField do not have matching Versioned applications. Make sure that both are either un-Versioned or Versioned'
SilverStripe\LinkField\Models\SiteTreeLink:
MISSING_DEFAULT_TITLE: 'Page missing'
5 changes: 5 additions & 0 deletions src/Models/EmailLink.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,9 @@ public function getURL(): string
{
return $this->Email ? sprintf('mailto:%s', $this->Email) : '';
}

public function getDefaultTitle(): string
{
return $this->getDescription();
}
}
5 changes: 5 additions & 0 deletions src/Models/ExternalLink.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,9 @@ public function getURL(): string
{
return $this->ExternalUrl ?: '';
}

public function getDefaultTitle(): string
{
return $this->getDescription();
}
}
5 changes: 5 additions & 0 deletions src/Models/FileLink.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,9 @@ public function getURL(): string
$file = $this->File();
return $file->exists() ? (string) $file->getURL() : '';
}

public function getDefaultTitle(): string
{
return $this->getDescription();
}
}
23 changes: 23 additions & 0 deletions src/Models/Link.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use InvalidArgumentException;
use ReflectionException;
use SilverStripe\Core\ClassInfo;
use SilverStripe\Core\Config\Config;
use SilverStripe\Core\Injector\Injector;
use SilverStripe\Forms\CompositeValidator;
use SilverStripe\Forms\DropdownField;
Expand All @@ -30,6 +31,8 @@ class Link extends DataObject
'OpenInNew' => 'Boolean',
];

private static string $default_link_title = 'Link';

/**
* In-memory only property used to change link type
* This case is relevant for CMS edit form which doesn't use React driven UI
Expand Down Expand Up @@ -68,6 +71,7 @@ public function getCMSFields(): FieldList

$titleField = $fields->dataFieldByName('Title');
$titleField->setTitle(_t('LinkField.LINK_FIELD_TITLE', 'Title'));
$titleField->setDescription(_t('LinkField.LINK_FIELD_TITLE_DESCRIPTION', 'If left blank, default title will be used'));

$openInNewField = $fields->dataFieldByName('OpenInNew');
$openInNewField->setTitle(_t('LinkField.OPEN_IN_NEW_TITLE', 'Open in new window?'));
Expand Down Expand Up @@ -290,4 +294,23 @@ private function getLinkTypes(): array

return $types;
}

public function getDisplayTitle(): string
{
// If we have a title or don't have link, we can just bail out without any changes
if ($this->Title) {
return $this->Title;
}

$defaultLinkTitle = $this->getDefaultTitle();

$this->extend('updateDefaultLinkTitle', $defaultLinkTitle);

return $defaultLinkTitle;
}

public function getDefaultTitle(): string
{
return $this->getDescription() ?: $this->getURL();
}
}
5 changes: 5 additions & 0 deletions src/Models/PhoneLink.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,9 @@ public function getURL(): string
{
return $this->Phone ? sprintf('tel:%s', $this->Phone) : '';
}

public function getDefaultTitle(): string
{
return $this->getDescription();
}
}
16 changes: 16 additions & 0 deletions src/Models/SiteTreeLink.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,4 +134,20 @@ public function getTitle(): ?string
// Use page title as a default value in case CMS user didn't provide the title
return $page->Title;
}

public function getDefaultTitle(): string
{
$page = $this->Page();
$pageExist = $this->Page()->exists();

// If the page doesn't exist, we can't get the title
if (!$pageExist) {
return _t(
static::class . '.MISSING_DEFAULT_TITLE',
'Page missing',
);
}

return $page->Title;
}
}
2 changes: 1 addition & 1 deletion templates/SilverStripe/LinkField/Models/Link.ss
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<a href="$URL" <% if $OpenInNew %>target="_blank" rel="noopener noreferrer"<% end_if %>>$Title</a>
<a href="$URL" <% if $OpenInNew %>target="_blank" rel="noopener noreferrer"<% end_if %>>$DisplayTitle</a>
14 changes: 14 additions & 0 deletions tests/php/Models/Extensions/ExternalLinkExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace SilverStripe\LinkField\Tests\Models\Extensions;

use SilverStripe\Dev\TestOnly;
use SilverStripe\ORM\DataExtension;

class ExternalLinkExtension extends DataExtension implements TestOnly
{
public function updateDefaultLinkTitle(&$defaultLinkTitle): void
{
$defaultLinkTitle = sprintf('External Link: %s', $this->owner->getURL());
}
}
74 changes: 74 additions & 0 deletions tests/php/Models/LinkTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use SilverStripe\ORM\DataObject;
use SilverStripe\ORM\ValidationException;
use SilverStripe\Versioned\Versioned;
use SilverStripe\LinkField\Tests\Models\Extensions\ExternalLinkExtension;

class LinkTest extends SapphireTest
{
Expand All @@ -28,6 +29,12 @@ class LinkTest extends SapphireTest
*/
protected static $fixture_file = 'LinkTest.yml';

protected static $required_extensions = [
ExternalLink::class => [
ExternalLinkExtension::class,
],
];

protected function setUp(): void
{
parent::setUp();
Expand Down Expand Up @@ -329,4 +336,71 @@ public function linkUrlCasesDataProvider(): array
],
];
}

function linkDefaultTitleDataProvider(): array
{
return [
'page link' => [
'page-link-1',
SiteTreeLink::class,
'PageLink1'
],
'email link' => [
'email-link-with-email',
EmailLink::class,
'EmailLinkWithEmail'
],
'external link' => [
'external-link-with-url',
ExternalLink::class,
'ExternalLinkWithUrl'
],
'phone link' => [
'phone-link-with-phone',
PhoneLink::class,
'PhoneLinkWithPhone'
],
'file link' => [
'file-link-no-image',
FileLink::class,
'FileLinkNoImage'
],
'page link with default title' => [
'page-link-with-default-title',
SiteTreeLink::class,
'Page1'
],
'email link with default title' => [
'email-link-with-default-title',
EmailLink::class,
'maxime@silverstripe.com'
],
'external link with default title' => [
'external-link-with-default-title',
ExternalLink::class,
'External Link: https://google.com'
],
'phone link with default title' => [
'phone-link-with-default-title',
PhoneLink::class,
'+64 4 978 7330'
],
'file link with default title' => [
'file-link-with-default-title',
FileLink::class,
'600x400.png'
],
];
}

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

$this->assertEquals($expected, $link->getDisplayTitle());
}
}
10 changes: 10 additions & 0 deletions tests/php/Models/LinkTest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,31 +36,41 @@ SilverStripe\LinkField\Models\SiteTreeLink:
QueryString: 'param1=value1&param2=option2'
Anchor: 'my-anchor'
Page: =>SilverStripe\CMS\Model\SiteTree.page-1
page-link-with-default-title:
Page: =>SilverStripe\CMS\Model\SiteTree.page-1

SilverStripe\LinkField\Models\EmailLink:
email-link-with-email:
Title: 'EmailLinkWithEmail'
Email: 'maxime@silverstripe.com'
email-link-no-email:
Title: 'EmailLinkNoEmail'
email-link-with-default-title:
Email: 'maxime@silverstripe.com'

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

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

SilverStripe\LinkField\Models\FileLink:
file-link-with-image:
Title: 'FileLinkWithImage'
File: =>SilverStripe\Assets\Image.image-1
file-link-no-image:
Title: 'FileLinkNoImage'
file-link-with-default-title:
File: =>SilverStripe\Assets\Image.image-1

0 comments on commit d72d856

Please sign in to comment.