Removed unnecessary BetterNavigatorShouldDisplay and adjusted readme around using custom logic to control the display of the navigator

This commit is contained in:
UndefinedOffset 2022-07-06 15:11:16 -03:00
parent 8d85d5df36
commit a9576bb520
No known key found for this signature in database
GPG Key ID: 59C4EE2B6468B796
2 changed files with 5 additions and 30 deletions

View File

@ -33,10 +33,10 @@ The navigator is auto-injected into your template, and no code changes are neede
If your website uses caching, make sure BetterNavigator's output is excluded.
## Disabling the navigator
## Custom navigator display logic
You can disable the navigator using your own custom logic by defining a `showBetterNavigator(): bool`
method in any controller with the extension applied.
You can customise the navigator display logic using your own custom logic by defining a `showBetterNavigator(): bool`
method in any controller with the extension applied. By default the navigator will only show on controllers that have a `dataRecord` property that is an instance of `SilverStripe\CMS\Model\SiteTree`.
```php
public function showBetterNavigator()
@ -101,25 +101,6 @@ public function BetterNavigatorEditLink()
(This example uses [sunnysideup/cms_edit_link_field](https://github.com/sunnysideup/silverstripe-cms_edit_link_field) to automatically find an edit link for a specified DataObject, but you can return any URL.)
## Overriding whether better navigator should be shown or not
There may be occasions when you wish to override whether better navigator should be shown at all, for example on custom data objects. To do so simply add a `BetterNavigatorShouldDisplay()` method to your Controller, e.g.:
```php
// EventController.php
/**
* Detect whether better navigator should be displayed or not
* @return bool
*/
public function BetterNavigatorShouldDisplay()
{
return $this->dataRecord
&& $this->dataRecord instanceof Event
&& $this->dataRecord->ID > 0
&& (Director::isDev() || Permission::check('CMS_ACCESS_' . EventAdmin::class));
}
```
## Overriding the permissions required for the cms edit link
By default users are required to have at least the `CMS_ACCESS_CMSMain` permission in order to see the edit link in better navigator, you can override this by setting the `better_navigator_edit_permission` configuration option on your controller to another permission code or an array of permission codes, e.g.:

View File

@ -66,7 +66,7 @@ class BetterNavigatorExtension extends DataExtension
*/
public function showBetterNavigator()
{
return true;
return $this->isAPage();
}
/**
@ -165,14 +165,8 @@ class BetterNavigatorExtension extends DataExtension
return $this->owner->getField('_betterNavigatorShouldDisplay');
}
if ($this->owner->hasMethod('BetterNavigatorShouldDisplay')) {
$result = $this->owner->BetterNavigatorShouldDisplay();
$this->owner->setField('_betterNavigatorShouldDisplay', $result);
return $result;
}
// Make sure this is a page
if (!$this->isAPage() || !$this->owner->showBetterNavigator()) {
if (!$this->owner->showBetterNavigator()) {
$this->owner->setField('_betterNavigatorShouldDisplay', false);
return false;
}