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 452285a
Show file tree
Hide file tree
Showing 8 changed files with 81 additions and 3 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'
27 changes: 24 additions & 3 deletions src/Form/Traits/AllowedLinkClassesTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,15 +85,15 @@ private function validateTypes(array $types): bool
public function getTypesProps(): string
{
$typesList = [];
$typeDefinitions = $this->genarateAllowedTypes();
$typeDefinitions = $this->sortLinkTypes($this->genarateAllowedTypes());
foreach ($typeDefinitions as $key => $class) {
$type = Injector::inst()->get($class);
if (!$type->canCreate()) {
continue;
}
$typesList[$key] = [
'key' => $key,
'title' => $type->i18n_singular_name(),
'title' => $type->getMenuTitle(),
'handlerName' => $type->LinkTypeHandlerName(),
];
}
Expand Down Expand Up @@ -121,7 +121,28 @@ private function genarateAllowedTypes(): array
$result[$type] = $class;
}
}

return $result;
}

private function sortLinkTypes(array $types): array
{
$order = [
'sitetree',
'file',
'external',
'email',
'phone',
];

$orderedArray = [];
foreach ($order as $key) {
$orderedArray[$key] = $types[$key];
}
// Combine the rest of the types with the ordered array
$diff = array_diff_key($types, $orderedArray);
$orderedArray = array_merge($orderedArray, $diff);

return $orderedArray;
}

}
8 changes: 8 additions & 0 deletions src/Models/EmailLink.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,12 @@ public function getURL(): string
{
return $this->Email ? sprintf('mailto:%s', $this->Email) : '';
}

/**
* Create menu title for Email link type
*/
public function getMenuTitle(): string
{
return _t(__CLASS__ . '.LINKLABEL', 'Link to email address');
}
}
8 changes: 8 additions & 0 deletions src/Models/ExternalLink.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,12 @@ public function getURL(): string
{
return $this->ExternalUrl ?: '';
}

/**
* Create menu title for External link type
*/
public function getMenuTitle(): string
{
return _t(__CLASS__ . '.LINKLABEL', 'Link to external URL');
}
}
8 changes: 8 additions & 0 deletions src/Models/FileLink.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,12 @@ public function getDefaultTitle(): string

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

/**
* Create menu title for File link type
*/
public function getMenuTitle(): string
{
return _t(__CLASS__ . '.LINKLABEL', 'Link to a file');
}
}
8 changes: 8 additions & 0 deletions src/Models/Link.php
Original file line number Diff line number Diff line change
Expand Up @@ -454,4 +454,12 @@ public function getShortCode(): string
{
return strtolower(rtrim(ClassInfo::shortName($this), 'Link')) ?? '';
}

/**
* Create menu title from singular_name
*/
public function getMenuTitle(): string
{
return $this->i18n_singular_name();
}
}
8 changes: 8 additions & 0 deletions src/Models/PhoneLink.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,12 @@ public function getURL(): string
{
return $this->Phone ? sprintf('tel:%s', $this->Phone) : '';
}

/**
* Create menu title for Phone link type
*/
public function getMenuTitle(): string
{
return _t(__CLASS__ . '.LINKLABEL', 'Phone number');
}
}
8 changes: 8 additions & 0 deletions src/Models/SiteTreeLink.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,4 +131,12 @@ public function getDefaultTitle(): string
}
return $page->Title;
}

/**
* Create menu title for SiteTree link type
*/
public function getMenuTitle(): string
{
return _t(__CLASS__ . '.LINKLABEL', 'Page on this site');
}
}

0 comments on commit 452285a

Please sign in to comment.