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 8cc5717
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 7 deletions.
6 changes: 5 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 All @@ -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
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
2 changes: 2 additions & 0 deletions src/Models/EmailLink.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
16 changes: 14 additions & 2 deletions src/Models/Link.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 '';
Expand Down Expand Up @@ -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
Expand All @@ -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();
}
Expand Down
2 changes: 2 additions & 0 deletions src/Models/PhoneLink.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
4 changes: 2 additions & 2 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

0 comments on commit 8cc5717

Please sign in to comment.