Skip to content
This repository has been archived by the owner on Mar 15, 2024. It is now read-only.

Commit

Permalink
Keeping gridfield to manage items beyond max depth
Browse files Browse the repository at this point in the history
  • Loading branch information
Brandon32 committed Aug 29, 2018
1 parent 0245ce7 commit d87369b
Showing 1 changed file with 39 additions and 30 deletions.
69 changes: 39 additions & 30 deletions code/MenuItemSquared.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
/**
* Class MenuItemSquared
*
* @method HasManyList ChildItems
* @method MenuItem ParentItem
* @see MenuItem
*/
class MenuItemSquared extends DataExtension
Expand Down Expand Up @@ -32,71 +34,77 @@ class MenuItemSquared extends DataExtension
*/
public function updateCMSFields(FieldList $fields)
{
if (!$this->owner->config()->disable_image) {
/** @var MenuItem|MenuItemSquared $owner */
$owner = $this->owner;
if (!$owner->config()->disable_image) {
$fields->push(new UploadField('Image', 'Image'));
}

if (!$this->owner->config()->disable_hierarchy) {
if ($this->owner->ID != null) {
$AllParentItems = $this->owner->getAllParentItems();
$topMenuSet = $this->owner->TopMenuSet();
if (!$owner->config()->disable_hierarchy) {
if ($owner->ID != null) {
$ascendants = $owner->getAllParentItems();
$topMenuSet = $owner->TopMenuSet();
$topMenuName = $topMenuSet->Name;

$config = MenuSet::config();
$depth = 1;
$maxDepth = 1;
if (is_array($config->$topMenuName) && isset($config->{$topMenuName}['depth'])) {
$depth = $config->{$topMenuName}['depth'];
$maxDepth = $config->{$topMenuName}['depth'];
}

if (!is_numeric($depth) || $depth < 0) {
$depth = 1;
if (!is_numeric($maxDepth) || $maxDepth < 0) {
$maxDepth = 1;
}

if (!empty($AllParentItems) && count($AllParentItems) >= $depth) {
$fields->push(new LabelField('MenuItems', 'Max Sub Menu Depth Limit'));
} else {
$fields->push(
new GridField(
'MenuItems',
'Sub Menu Items',
$this->owner->ChildItems(),
new MenuItemSquaredGridFieldConfig()
)
);
$gridFieldConfig = new MenuItemSquaredGridFieldConfig();
if (count($ascendants) >= $maxDepth) {
$fields->push(new LabelField('MenuItems', 'Max Depth Limit Reached, Update Config to Add Sub Menu Items'));
$gridFieldConfig->removeComponentsByType(GridFieldAddNewMultiClass::class);
}
// Keep GridField in case of import or max depth changed.
$fields->push(new GridField(
'MenuItems',
'Sub Menu Items',
$owner->ChildItems(),
$gridFieldConfig
));
} else {
$fields->push(new LabelField('MenuItems', 'Save This Menu Item Before Adding Sub Menu Items'));
}
}
}

/**
* @return mixed
* @return MenuSet
*/
public function TopMenuSet()
{
$AllParentItems = $this->owner->getAllParentItems();
if (!empty($AllParentItems)) {
return end($AllParentItems)->MenuSet();
$ascendants = $this->owner->getAllParentItems();
if (!empty($ascendants)) {
return end($ascendants)->MenuSet();
}

return $this->owner->MenuSet();
}

/**
* Create a key value pair of ChildID => Parent relationships.
* Starts with itself, stops at circular relationships.
*
* @return array
*/
public function getAllParentItems()
{
$WorkingItem = $this->owner;
$ParentItems = [];
/** @var MenuItem|MenuItemSquared $current */
$current = $this->owner;
$parents = [];

while ($WorkingItem->ParentItemID && $WorkingItem->ParentItem() && $WorkingItem->ParentItem()->ID && !isset($ParentItems[$WorkingItem->ParentItem()->ID])) {
$ParentItems[$WorkingItem->ID] = $WorkingItem->ParentItem();
$WorkingItem = $ParentItems[$WorkingItem->ID];
while ($current->ParentItemID && $current->ParentItem() && $current->ParentItem()->ID && !isset($parents[$current->ParentItem()->ID])) {
$parents[$current->ID] = $current->ParentItem();
$current = $parents[$current->ID];
}

return $ParentItems;
return $parents;
}

public function onBeforeWrite()
Expand All @@ -112,6 +120,7 @@ public function onBeforeWrite()

public function onBeforeDelete()
{
/** @var MenuItem $childItem */
foreach ($this->owner->ChildItems() as $childItem) {
$childItem->delete();
}
Expand Down

0 comments on commit d87369b

Please sign in to comment.