Skip to content

Commit

Permalink
Merge pull request #11 from wedevelopnl/feature/ss-5
Browse files Browse the repository at this point in the history
Update for SS5 compatibilty
  • Loading branch information
Dennisprins93 committed Jul 27, 2023
2 parents 9e50075 + 1061eaf commit 67e983a
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 58 deletions.
13 changes: 6 additions & 7 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,18 @@
"minimum-stability": "dev",
"prefer-stable": true,
"require": {
"php": ">=8.1",
"silverstripe/cms": "^4@stable",
"silverstripe/vendor-plugin": "^1.0",
"silverstripe/display-logic": "^2.0",
"symbiote/silverstripe-gridfieldextensions": "^3.5"
"php": "^8.1",
"silverstripe/cms": "^5",
"silverstripe/display-logic": "^3.0",
"symbiote/silverstripe-gridfieldextensions": "^4.0.2"
},
"require-dev": {
"friendsofphp/php-cs-fixer": "^3.4"
"friendsofphp/php-cs-fixer": "^3.20"
},
"extra": {
"installer-name": "silverstripe-menustructure",
"branch-alias": {
"dev-main": "3.x-dev"
"dev-main": "4.x-dev"
}
},
"autoload": {
Expand Down
31 changes: 13 additions & 18 deletions src/Model/Menu.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@
use SilverStripe\Security\Permission;
use SilverStripe\View\Parsers\URLSegmentFilter;
use SilverStripe\View\TemplateGlobalProvider;
use SilverStripe\View\ViewableData;
use Symbiote\GridFieldExtensions\GridFieldOrderableRows;

/**
* @property string $Title
* @property string $Slug
* @method MenuItem|HasManyList Items()
* @method HasManyList Items()
*/
class Menu extends DataObject implements TemplateGlobalProvider
{
Expand Down Expand Up @@ -127,9 +128,6 @@ public function canEdit($member = null): bool
return parent::canEdit($member);
}

/**
* @param null|int|Member $member
*/
public function canDelete($member = null): bool
{
if ($this->IsProtected()) {
Expand All @@ -148,32 +146,29 @@ public function forTemplate(): DBHTMLText
return $this->renderWith(self::class);
}

/**
* @return DataObject|DBHTMLText|Menu|null
* @throws \Exception
*/
public static function MenustructureMenu(string $slug, string $template = null)
public static function ViewableMenustructureMenu(string $slug, string $template): ?ViewableData
{
$menu = Menu::get()->filter([
'Slug' => $slug,
])->first();
$menu = self::MenustructureMenu($slug);

if (!$menu instanceof Menu) {
throw new \Exception('Menu with slug ' . $slug . ' is not found');
}

if ($template && $menu) {
return $menu->renderWith($template);
if ($menu instanceof Menu) {
return $menu->forTemplate($template);
}

return $menu;
}

public static function MenustructureMenu(string $slug): ?Menu
{
return Menu::get()->filter([
'Slug' => $slug,
])->first();
}

public static function get_template_global_variables(): array
{
return [
'MenustructureMenu',
'ViewableMenustructureMenu',
];
}
}
66 changes: 33 additions & 33 deletions src/Model/MenuItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,19 @@
* @property int $LinkedPageID
* @method File File()
* @method MenuItem ParentItem()
* @method MenuItem|HasManyList Items()
* @method HasManyList Items()
* @method SiteTree LinkedPage()
*/
class MenuItem extends DataObject
{
private const LINK_TYPE_PAGE = 'page';

private const LINK_TYPE_URL = 'url';

private const LINK_TYPE_FILE = 'file';

private const LINK_TYPE_NO_LINK = 'no-link';

/** @config */
private static string $table_name = 'Menustructure_MenuItem';

Expand Down Expand Up @@ -71,10 +79,10 @@ class MenuItem extends DataObject
];

private static array $link_types = [
'page' => 'Page',
'url' => 'URL',
'file' => 'File',
'no-link' => 'Not linked',
self::LINK_TYPE_PAGE => 'Page',
self::LINK_TYPE_URL => 'URL',
self::LINK_TYPE_FILE => 'File',
self::LINK_TYPE_NO_LINK => 'Not linked',
];

/** @config */
Expand All @@ -98,23 +106,23 @@ public function getCMSFields(): FieldList
$fields->replaceField('LinkType', DropdownField::create('LinkType', $this->fieldLabel('LinkType'), $this->getLinkTypes()));
$fields->replaceField('LinkedPageID', $linkedPageWrapper = Wrapper::create(TreeDropdownField::create('LinkedPageID', $this->fieldLabel('LinkedPage'), SiteTree::class)));

$linkedPageWrapper->displayIf('LinkType')->isEqualTo('page');
$fields->dataFieldByName('File')->displayIf('LinkType')->isEqualTo('file');
$fields->dataFieldByName('Url')->displayIf('LinkType')->isEqualTo('url');
$fields->dataFieldByName('OpenInNewWindow')->displayIf('LinkType')->isEqualTo('page')->orIf('LinkType')->isEqualTo('url')->orIf('LinkType')->isEqualTo('file');
$linkedPageWrapper->displayIf('LinkType')->isEqualTo(self::LINK_TYPE_PAGE);
$fields->dataFieldByName('File')->displayIf('LinkType')->isEqualTo(self::LINK_TYPE_FILE);
$fields->dataFieldByName('Url')->displayIf('LinkType')->isEqualTo(self::LINK_TYPE_URL);
$fields->dataFieldByName('OpenInNewWindow')->displayIf('LinkType')->isEqualTo(self::LINK_TYPE_PAGE)->orIf('LinkType')->isEqualTo(self::LINK_TYPE_URL)->orIf('LinkType')->isEqualTo(self::LINK_TYPE_FILE);

if (self::config()->enable_query_string) {
/** @var TextField $queryStringField */
$queryStringField = $fields->dataFieldByName('QueryString');
$queryStringField->displayIf('LinkType')->isEqualTo('page');
$queryStringField->displayIf('LinkType')->isEqualTo(self::LINK_TYPE_PAGE);
$queryStringField->setDescription('Example: <code>foo=bar&john=doe</code>');
$fields->addFieldToTab('Root.Main', $queryStringField);
} else {
$fields->removeByName('QueryString');
}

if (self::config()->enable_page_anchor) {
$fields->dataFieldByName('AnchorText')->displayIf('LinkType')->isEqualTo('page');
$fields->dataFieldByName('AnchorText')->displayIf('LinkType')->isEqualTo(self::LINK_TYPE_PAGE);
$fields->addFieldToTab('Root.Main', $fields->dataFieldByName('AnchorText'));
} else {
$fields->removeByName('AnchorText');
Expand Down Expand Up @@ -144,27 +152,19 @@ private function getLinkTypes(): array

public function getLink(): string
{
$link = '';

switch ($this->LinkType) {
case 'url':
$link = $this->Url;
break;
case 'page':
$link = $this->LinkedPage()->Link();

if (self::config()->enable_query_string && $this->QueryString) {
$link = sprintf('%s?%s', $link, $this->QueryString);
}

if (self::config()->enable_page_anchor && $this->AnchorText) {
$link = sprintf('%s#%s', $link, $this->AnchorText);
}

break;
case 'file':
$link = $this->File()->Link();
break;
$link = match ($this->LinkType) {
'url' => $this->Url,
'page' => $this->LinkedPage()->Link(),
'file' => $link = $this->File()->Link(),
default => ''
};

if ($this->LinkType === self::LINK_TYPE_PAGE && self::config()->enable_query_string && $this->QueryString) {
$link = sprintf('%s?%s', $link, $this->QueryString);
}

if ($this->LinkType === self::LINK_TYPE_PAGE && self::config()->enable_page_anchor && $this->AnchorText) {
$link = sprintf('%s#%s', $link, $this->AnchorText);
}

$this->extend('updateLink', $link);
Expand All @@ -174,7 +174,7 @@ public function getLink(): string

public function LinkingMode(): string
{
if ($this->LinkType === 'page') {
if ($this->LinkType === self::LINK_TYPE_PAGE) {
return Controller::curr()->ID === $this->LinkedPageID ? 'current' : 'link';
}

Expand Down

0 comments on commit 67e983a

Please sign in to comment.