From 8cc5717c202a510b998663b78c8f3370f311ece6 Mon Sep 17 00:00:00 2001 From: Steve Boyd Date: Wed, 24 Jan 2024 14:20:45 +1300 Subject: [PATCH] ENH Hide the open in new window checkbox from phone and email links --- README.md | 6 +++++- src/Controllers/LinkFieldController.php | 4 ++-- src/Models/EmailLink.php | 2 ++ src/Models/Link.php | 16 ++++++++++++++-- src/Models/PhoneLink.php | 2 ++ .../php/Controllers/LinkFieldControllerTest.php | 4 ++-- 6 files changed, 27 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index f7fbfbfd..9001969d 100644 --- a/README.md +++ b/README.md @@ -106,10 +106,10 @@ class ExternalLinkExtension extends Extension $defaultLinkTitle = sprintf('External link: %s', $this->owner->ExternalUrl); } } - ``` ## Controlling what type of links can be created in a LinkField + By default, all `Link` subclasses can be created by a LinkField. This includes any custom `Link` subclasses defined in your projects or via third party module. Developers can control the link types allowed for individual `LinkField`. The `setAllowedTypes` method only allow link types that have been provided as parameters. @@ -141,6 +141,7 @@ Link::remove_extension(Versioned::class); ## Additional features The developer can customise the position of the link type in the menu by setting the `$menu_priority` value. The priority is in ascending order (i.e. a link with a higher priority value will be displayed lower in the list). + The developer can also set an icon that will correspond to a specific type of link by setting the value of the `$icon` configuration property. The value of this configuration corresponds to the css class of the icon to be used. ```yml @@ -148,6 +149,7 @@ SilverStripe\LinkField\Models\PhoneLink: icon: 'font-icon-menu-help' menu_priority: 1 ``` + The developer can also define these values for a new link type. ```php @@ -162,6 +164,8 @@ class MyCustomLink extends Link } ``` +The "Open in new window?" checkbox can be hidden from custom link types by adding `protected bool $showOpenInNewWindowField = false;` to the custom link class. + ## Migrating from Shae Dawson's Linkable module https://github.com/sheadawson/silverstripe-linkable diff --git a/src/Controllers/LinkFieldController.php b/src/Controllers/LinkFieldController.php index 6b7d8873..86f10495 100644 --- a/src/Controllers/LinkFieldController.php +++ b/src/Controllers/LinkFieldController.php @@ -356,8 +356,8 @@ private function createLinkForm(Link $link, string $operation): Form // Add save action button $title = $id - ? _t(__CLASS__ . '.UPDATE_LINK', 'Update link') - : _t(__CLASS__ . '.CREATE_LINK', 'Create link'); + ? _t(__CLASS__ . '.UPDATE_LINK', 'Update link') + : _t(__CLASS__ . '.CREATE_LINK', 'Create link'); $actions = FieldList::create([ FormAction::create('save', $title) ->setSchemaData(['data' => ['buttonStyle' => 'primary']]), diff --git a/src/Models/EmailLink.php b/src/Models/EmailLink.php index a43eea3a..a35c5f58 100644 --- a/src/Models/EmailLink.php +++ b/src/Models/EmailLink.php @@ -25,6 +25,8 @@ class EmailLink extends Link private static $icon = 'font-icon-p-mail'; + protected bool $showOpenInNewWindowField = false; + public function getCMSFields(): FieldList { $this->beforeUpdateCMSFields(function (FieldList $fields) { diff --git a/src/Models/Link.php b/src/Models/Link.php index cb8fc2db..2836dafc 100644 --- a/src/Models/Link.php +++ b/src/Models/Link.php @@ -70,6 +70,11 @@ class Link extends DataObject */ private static $icon = 'font-icon-link'; + /** + * Whether or not to show the "Open in new window?" checkbox field in the link edit modal + */ + protected bool $showOpenInNewWindowField = true; + public function getDescription(): string { return ''; @@ -103,8 +108,6 @@ public function getCMSFields(): FieldList $fields->removeByName('Sort'); - $openInNewField = $fields->dataFieldByName('OpenInNew'); - $openInNewField->setTitle(_t(__CLASS__ . '.OPEN_IN_NEW_TITLE', 'Open in new window?')); if (static::class === self::class) { // Add a link type selection field for generic links @@ -123,6 +126,15 @@ public function getCMSFields(): FieldList $linkTypeField->setEmptyString('-- select type --'); } }); + $this->afterUpdateCMSFields(function (FieldList $fields) { + // Move the OpenInNew field to the bottom of the form and update its title + $openInNewField = $fields->dataFieldByName('OpenInNew'); + $fields->removeByName('OpenInNew'); + if ($this->showOpenInNewWindowField) { + $fields->addFieldToTab('Root.Main', $openInNewField); + $openInNewField->setTitle(_t(__CLASS__ . '.OPEN_IN_NEW_TITLE', 'Open in new window?')); + } + }); return parent::getCMSFields(); } diff --git a/src/Models/PhoneLink.php b/src/Models/PhoneLink.php index 26004255..8cf5930d 100644 --- a/src/Models/PhoneLink.php +++ b/src/Models/PhoneLink.php @@ -22,6 +22,8 @@ class PhoneLink extends Link private static $icon = 'font-icon-mobile'; + protected bool $showOpenInNewWindowField = false; + public function getCMSFields(): FieldList { $this->beforeUpdateCMSFields(function (FieldList $fields) { diff --git a/tests/php/Controllers/LinkFieldControllerTest.php b/tests/php/Controllers/LinkFieldControllerTest.php index 9c97883e..f384fac8 100644 --- a/tests/php/Controllers/LinkFieldControllerTest.php +++ b/tests/php/Controllers/LinkFieldControllerTest.php @@ -85,10 +85,10 @@ public function testLinkFormGetSchema( . "&ownerRelation=$ownerRelation"; $this->assertSame($expectedAction, $formSchema['schema']['action']); // schema is nested and retains 'Root' and 'Main' tab hierarchy - $this->assertSame('Phone', $formSchema['schema']['fields'][0]['children'][0]['children'][2]['name']); + $this->assertSame('Phone', $formSchema['schema']['fields'][0]['children'][0]['children'][1]['name']); $this->assertSame('action_save', $formSchema['schema']['actions'][0]['name']); // state node is flattened, unlike schema node - $this->assertSame($expectedValue, $formSchema['state']['fields'][4]['value']); + $this->assertSame($expectedValue, $formSchema['state']['fields'][3]['value']); $this->assertFalse(array_key_exists('errors', $formSchema)); if ($idType === 'new-record') { $this->assertSame('OwnerID', $formSchema['state']['fields'][6]['name']);