diff --git a/lang/en.yml b/lang/en.yml index abbba2b1..3e99bb1f 100644 --- a/lang/en.yml +++ b/lang/en.yml @@ -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' diff --git a/src/Form/Traits/AllowedLinkClassesTrait.php b/src/Form/Traits/AllowedLinkClassesTrait.php index d21fef27..ac1f6823 100644 --- a/src/Form/Traits/AllowedLinkClassesTrait.php +++ b/src/Form/Traits/AllowedLinkClassesTrait.php @@ -85,7 +85,7 @@ 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()) { @@ -93,7 +93,7 @@ public function getTypesProps(): string } $typesList[$key] = [ 'key' => $key, - 'title' => $type->i18n_singular_name(), + 'title' => $type->getMenuTitle(), 'handlerName' => $type->LinkTypeHandlerName(), ]; } @@ -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; + } + } diff --git a/src/Models/EmailLink.php b/src/Models/EmailLink.php index 2410af4a..c73cbf78 100644 --- a/src/Models/EmailLink.php +++ b/src/Models/EmailLink.php @@ -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'); + } } diff --git a/src/Models/ExternalLink.php b/src/Models/ExternalLink.php index 27c167db..f5418756 100644 --- a/src/Models/ExternalLink.php +++ b/src/Models/ExternalLink.php @@ -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'); + } } diff --git a/src/Models/FileLink.php b/src/Models/FileLink.php index 0d118b8e..b36a11f3 100644 --- a/src/Models/FileLink.php +++ b/src/Models/FileLink.php @@ -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'); + } } diff --git a/src/Models/Link.php b/src/Models/Link.php index 2beea0b2..842bdba1 100644 --- a/src/Models/Link.php +++ b/src/Models/Link.php @@ -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(); + } } diff --git a/src/Models/PhoneLink.php b/src/Models/PhoneLink.php index 63fdf45d..0168eb81 100644 --- a/src/Models/PhoneLink.php +++ b/src/Models/PhoneLink.php @@ -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'); + } } diff --git a/src/Models/SiteTreeLink.php b/src/Models/SiteTreeLink.php index 95dce8b1..bd1798bf 100644 --- a/src/Models/SiteTreeLink.php +++ b/src/Models/SiteTreeLink.php @@ -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'); + } }