Skip to content

Commit

Permalink
ENH Hide the open in new window checkbox from phone and email links
Browse files Browse the repository at this point in the history
  • Loading branch information
emteknetnz committed Jan 24, 2024
1 parent fce5499 commit 0c01fcf
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 26 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down Expand Up @@ -141,13 +141,15 @@ 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
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
Expand Down
4 changes: 2 additions & 2 deletions src/Controllers/LinkFieldController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']]),
Expand Down
1 change: 1 addition & 0 deletions src/Models/EmailLink.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public function getCMSFields(): FieldList
'Email',
_t(__CLASS__ . '.EMAIL_FIELD', 'Email address'),
));
$fields->removeByName('OpenInNew');
});
return parent::getCMSFields();
}
Expand Down
9 changes: 9 additions & 0 deletions src/Models/Link.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,15 @@ public function getCMSFields(): FieldList
$linkTypeField->setEmptyString('-- select type --');
}
});
$this->afterUpdateCMSFields(function (FieldList $fields) {
// Move the OpenInNew field to the bottom of the form if it hasn't been removed in
// a subclasses getCMSFields() method
$openInNewField = $fields->dataFieldByName('OpenInNew');
if ($openInNewField) {
$fields->removeByName('OpenInNew');
$fields->addFieldToTab('Root.Main', $openInNewField);
}
});

return parent::getCMSFields();
}
Expand Down
1 change: 1 addition & 0 deletions src/Models/PhoneLink.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public function getCMSFields(): FieldList
$this->beforeUpdateCMSFields(function (FieldList $fields) {
$linkField = $fields->dataFieldByName('Phone');
$linkField->setTitle(_t(__CLASS__ . '.PHONE_FIELD', 'Phone'));
$fields->removeByName('OpenInNew');
});
return parent::getCMSFields();
}
Expand Down
8 changes: 4 additions & 4 deletions tests/php/Controllers/LinkFieldControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
Expand Down Expand Up @@ -238,10 +238,10 @@ public function testLinkFormPost(
. "&ownerRelation=$ownerRelation";
$this->assertSame($expectedUrl, $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('9876543210', $formSchema['state']['fields'][4]['value']);
$this->assertSame('9876543210', $formSchema['state']['fields'][3]['value']);
if ($fail) {
$this->assertSame($expectedMessage, $formSchema['errors'][0]['value']);
// Phone was note updated on PhoneLink dataobject
Expand Down
53 changes: 34 additions & 19 deletions tests/php/Traits/AllowedLinkClassesTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ class AllowedLinkClassesTraitTest extends SapphireTest
* Need to include only known Link subclasses.
*/
private $link_types = [
SiteTreeLink::class,
ExternalLink::class,
FileLink::class,
EmailLink::class,
PhoneLink::class,
TestPhoneLink::class,
'sitetree' => SiteTreeLink::class,
'external' => ExternalLink::class,
'file' => FileLink::class,
'email' => EmailLink::class,
'phone' => PhoneLink::class,
'testphone' => TestPhoneLink::class,
];

public function setUp(): void
Expand Down Expand Up @@ -58,20 +58,20 @@ public function allowedTypesDataProvider() : array
return [
'allow all Link classes' => [
'enabled' => [
SiteTreeLink::class,
ExternalLink::class,
FileLink::class,
EmailLink::class,
PhoneLink::class,
TestPhoneLink::class,
SiteTreeLink::class,
ExternalLink::class,
FileLink::class,
EmailLink::class,
PhoneLink::class,
TestPhoneLink::class,
],
'expected' => [
SiteTreeLink::class,
ExternalLink::class,
FileLink::class,
EmailLink::class,
PhoneLink::class,
TestPhoneLink::class,
SiteTreeLink::class,
ExternalLink::class,
FileLink::class,
EmailLink::class,
PhoneLink::class,
TestPhoneLink::class,
],
],
'allow only SiteTreeLink class' => [
Expand Down Expand Up @@ -174,10 +174,11 @@ public function testGetSortedTypeProps(array $enabled, array $expected, bool $re
if ($reorder) {
Injector::inst()->get(TestPhoneLink::class)->config()->set('menu_priority', 5);
}

$linkField = LinkField::create('LinkField');
$linkField->setAllowedTypes($enabled);
$json = json_decode($linkField->getTypesProps(), true);
$json = $this->removeCustomLinkTypes($json);
$this->assertEquals(array_keys($json), $expected);
}

Expand Down Expand Up @@ -277,4 +278,18 @@ public function testGetTypesProps(
$this->assertEquals($icon, $json[$key]['icon']);
$this->assertEquals($allowed, $json[$key]['allowed']);
}

/**
* Remove any classes defined at the project level that interfere with running unit-tests locally
*/
private function removeCustomLinkTypes(array $json): array
{
$newJson = [];
foreach ($json as $key => $value) {
if (array_key_exists($key, $this->link_types)) {
$newJson[$key] = $value;
}
}
return $newJson;
}
}

0 comments on commit 0c01fcf

Please sign in to comment.