Skip to content

Commit

Permalink
ENH Add sorting by link type
Browse files Browse the repository at this point in the history
  • Loading branch information
Sabina Talipova committed Jan 11, 2024
1 parent 5db919e commit 713b256
Show file tree
Hide file tree
Showing 9 changed files with 149 additions and 2 deletions.
9 changes: 9 additions & 0 deletions lang/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,17 @@ en:
MISSING_DEFAULT_TITLE: 'No link provided'
SilverStripe\LinkField\Models\SiteTreeLink:
MISSING_DEFAULT_TITLE: 'Page missing'
LINKLABEL: 'Page on this site'
SilverStripe\LinkField\Models\FileLink:
MISSING_DEFAULT_TITLE: 'File missing'
LINKLABEL: 'Link to a file'
SilverStripe\LinkField\Models\EmailLink:
LINKLABEL: 'Link to email address'
SilverStripe\LinkField\Models\ExternalLink:
LINKLABEL: 'Link to external URL'
SilverStripe\LinkField\Models\PhoneLink:
LINKLABEL: 'Phone number'

SilverStripe\LinkField\Form\Traits\AllowedLinkClassesTrait:
INVALID_TYPECLASS: '"{class}": {typeclass} is not a valid Link Type'
INVALID_TYPECLASS_EMPTY: '"{class}": Allowed types cannot be empty'
7 changes: 5 additions & 2 deletions src/Form/Traits/AllowedLinkClassesTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,14 @@ public function getTypesProps(): string
}
$typesList[$key] = [
'key' => $key,
'title' => $type->i18n_singular_name(),
'title' => $type->getMenuTitle(),
'handlerName' => $type->LinkTypeHandlerName(),
'priority' => $class::config()->get('menu_priority'),
];
}
uasort($typesList, function ($a, $b) {
return $a['priority'] - $b['priority'];
});

return json_encode($typesList);
}
Expand All @@ -121,7 +125,6 @@ private function genarateAllowedTypes(): array
$result[$type] = $class;
}
}

return $result;
}
}
14 changes: 14 additions & 0 deletions src/Models/EmailLink.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ class EmailLink extends Link
'Email' => 'Varchar(255)',
];

/**
* Set the priority of this link type in the CMS menu
*/
private static int $menu_priority = 30;

public function getCMSFields(): FieldList
{
$this->beforeUpdateCMSFields(function (FieldList $fields) {
Expand All @@ -38,4 +43,13 @@ public function getURL(): string
{
return $this->Email ? sprintf('mailto:%s', $this->Email) : '';
}

/**
* The title that will be displayed in the dropdown
* for selecting the link type to create.
*/
public function getMenuTitle(): string
{
return _t(__CLASS__ . '.LINKLABEL', 'Link to email address');
}
}
14 changes: 14 additions & 0 deletions src/Models/ExternalLink.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ class ExternalLink extends Link
'ExternalUrl' => 'Varchar',
];

/**
* Set the priority of this link type in the CMS menu
*/
private static int $menu_priority = 20;

public function getCMSFields(): FieldList
{
$this->beforeUpdateCMSFields(function (FieldList $fields) {
Expand All @@ -35,4 +40,13 @@ public function getURL(): string
{
return $this->ExternalUrl ?: '';
}

/**
* The title that will be displayed in the dropdown
* for selecting the link type to create.
*/
public function getMenuTitle(): string
{
return _t(__CLASS__ . '.LINKLABEL', 'Link to external URL');
}
}
14 changes: 14 additions & 0 deletions src/Models/FileLink.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ class FileLink extends Link
'File' => File::class,
];

/**
* Set the priority of this link type in the CMS menu
*/
private static int $menu_priority = 10;

public function getCMSFields(): FieldList
{
$this->beforeUpdateCMSFields(function (FieldList $fields) {
Expand Down Expand Up @@ -55,4 +60,13 @@ public function getDefaultTitle(): string

return (string) $this->getDescription();
}

/**
* The title that will be displayed in the dropdown
* for selecting the link type to create.
*/
public function getMenuTitle(): string
{
return _t(__CLASS__ . '.LINKLABEL', 'Link to a file');
}
}
16 changes: 16 additions & 0 deletions src/Models/Link.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ class Link extends DataObject
*/
private ?string $linkType = null;

/**
* Set the priority of this link type in the CMS menu
*/
private static int $menu_priority = 100;

public function getDescription(): string
{
return '';
Expand Down Expand Up @@ -454,4 +459,15 @@ public function getShortCode(): string
{
return strtolower(rtrim(ClassInfo::shortName($this), 'Link')) ?? '';
}

/**
* The title that will be displayed in the dropdown
* for selecting the link type to create.
* Subclasses should override this.
* It will use the singular_name by default.
*/
public function getMenuTitle(): string
{
return $this->i18n_singular_name();
}
}
14 changes: 14 additions & 0 deletions src/Models/PhoneLink.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ class PhoneLink extends Link
'Phone' => 'Varchar(255)',
];

/**
* Set the priority of this link type in the CMS menu
*/
private static int $menu_priority = 40;

public function getCMSFields(): FieldList
{
$this->beforeUpdateCMSFields(function (FieldList $fields) {
Expand All @@ -33,4 +38,13 @@ public function getURL(): string
{
return $this->Phone ? sprintf('tel:%s', $this->Phone) : '';
}

/**
* The title that will be displayed in the dropdown
* for selecting the link type to create.
*/
public function getMenuTitle(): string
{
return _t(__CLASS__ . '.LINKLABEL', 'Phone number');
}
}
14 changes: 14 additions & 0 deletions src/Models/SiteTreeLink.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ class SiteTreeLink extends Link
'Page' => SiteTree::class,
];

/**
* Set the priority of this link type in the CMS menu
*/
private static int $menu_priority = 0;

public function getDescription(): string
{
$page = $this->Page();
Expand Down Expand Up @@ -131,4 +136,13 @@ public function getDefaultTitle(): string
}
return $page->Title;
}

/**
* The title that will be displayed in the dropdown
* for selecting the link type to create.
*/
public function getMenuTitle(): string
{
return _t(__CLASS__ . '.LINKLABEL', 'Page on this site');
}
}
49 changes: 49 additions & 0 deletions tests/php/Traits/AllowedLinkClassesTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use ArrayIterator;
use InvalidArgumentException;
use ReflectionMethod;
use SilverStripe\Core\Injector\Injector;
use SilverStripe\Dev\SapphireTest;
use SilverStripe\LinkField\Form\Traits\AllowedLinkClassesTrait;
use SilverStripe\LinkField\Form\LinkField;
Expand Down Expand Up @@ -105,6 +106,54 @@ public function provideTypesExceptionDataProvider() : array
];
}

public function sortedTypesDataProvider() : array
{
return [
'sort all allowed Link classes' => [
'enabled' => [
SiteTreeLink::class,
ExternalLink::class,
FileLink::class,
EmailLink::class,
PhoneLink::class,
TestPhoneLink::class,
],
'expected' => [
'sitetree',
'testphone',
'file',
'external',
'email',
'phone',
],
],
'sort only particular allowed Link class' => [
'enabled' => [
SiteTreeLink::class,
TestPhoneLink::class,
EmailLink::class,
],
'expected' => [
'sitetree',
'testphone',
'email',
],
],
];
}

/**
* @dataProvider sortedTypesDataProvider
*/
public function testGetTypesProps(array $enabled, array $expected): void
{
Injector::inst()->get(TestPhoneLink::class)->config()->set('menu_priority', 5);
$linkField = LinkField::create('LinkField');
$linkField->setAllowedTypes($enabled);
$json = json_decode($linkField->getTypesProps(), true);
$this->assertEquals(array_keys($json), $expected);
}

public function testGetTypesPropsCanCreate(): void
{
$linkField = LinkField::create('LinkField');
Expand Down

0 comments on commit 713b256

Please sign in to comment.