Fixes #418 - Added the ability to extend backend navigation
This commit is contained in:
parent
04bbad320f
commit
2617921ba3
|
|
@ -1,5 +1,6 @@
|
|||
<?php namespace Backend\Classes;
|
||||
|
||||
use Event;
|
||||
use BackendAuth;
|
||||
use System\Classes\PluginManager;
|
||||
|
||||
|
|
@ -86,6 +87,11 @@ class NavigationManager
|
|||
$this->registerMenuItems($id, $items);
|
||||
}
|
||||
|
||||
/*
|
||||
* Extensibility
|
||||
*/
|
||||
Event::fire('backend.menu.extendItems', [$this]);
|
||||
|
||||
/*
|
||||
* Sort menu items
|
||||
*/
|
||||
|
|
@ -157,25 +163,95 @@ class NavigationManager
|
|||
$this->items = [];
|
||||
|
||||
foreach ($definitions as $code => $definition) {
|
||||
$item = (object)array_merge(self::$mainItemDefaults, array_merge($definition, [
|
||||
'code' => $code,
|
||||
$item = (object) array_merge(self::$mainItemDefaults, array_merge($definition, [
|
||||
'code' => $code,
|
||||
'owner' => $owner
|
||||
]));
|
||||
|
||||
foreach ($item->sideMenu as $sideMenuItemCode => $sideMenuDefinition) {
|
||||
$item->sideMenu[$sideMenuItemCode] = (object)array_merge(
|
||||
$item->sideMenu[$sideMenuItemCode] = (object) array_merge(
|
||||
self::$sideItemDefaults,
|
||||
array_merge($sideMenuDefinition, [
|
||||
'code' => $sideMenuItemCode,
|
||||
'code' => $sideMenuItemCode,
|
||||
'owner' => $owner
|
||||
])
|
||||
);
|
||||
}
|
||||
|
||||
$this->items[] = $item;
|
||||
$itemKey = $this->makeItemKey($owner, $code);
|
||||
$this->items[$itemKey] = $item;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Dynamically add an array of main menu items
|
||||
* @param string $owner
|
||||
* @param array $definitions
|
||||
*/
|
||||
public function addMainMenuItems($owner, array $definitions)
|
||||
{
|
||||
foreach ($definitions as $code => $definition)
|
||||
$this->addMainMenuItem($owner, $code, $definition);
|
||||
}
|
||||
|
||||
/**
|
||||
* Dynamically add a single main menu item
|
||||
* @param string $owner
|
||||
* @param string $code
|
||||
* @param array $definitions
|
||||
*/
|
||||
public function addMainMenuItem($owner, $code, array $definition)
|
||||
{
|
||||
$sideMenu = isset($definition['sideMenu']) ? $definition['sideMenu'] : null;
|
||||
|
||||
$itemKey = $this->makeItemKey($owner, $code);
|
||||
if (isset($this->items[$itemKey]))
|
||||
$definition = array_merge((array) $this->items[$itemKey], $definition);
|
||||
|
||||
$item = (object) array_merge(self::$mainItemDefaults, array_merge($definition, [
|
||||
'code' => $code,
|
||||
'owner' => $owner
|
||||
]));
|
||||
|
||||
$this->items[$itemKey] = $item;
|
||||
|
||||
if ($sideMenu !== null)
|
||||
$this->addSideMenuItems($sideMenu);
|
||||
}
|
||||
|
||||
/**
|
||||
* Dynamically add an array of side menu items
|
||||
* @param string $owner
|
||||
* @param string $code
|
||||
* @param array $definitions
|
||||
*/
|
||||
public function addSideMenuItems($owner, $code, array $definitions)
|
||||
{
|
||||
foreach ($definitions as $sideCode => $definition)
|
||||
$this->addSideMenuItem($owner, $code, $sideCode, $definition);
|
||||
}
|
||||
|
||||
/**
|
||||
* Dynamically add a single side menu item
|
||||
* @param string $owner
|
||||
* @param string $code
|
||||
* @param string $sideCode
|
||||
* @param array $definitions
|
||||
*/
|
||||
public function addSideMenuItem($owner, $code, $sideCode, array $definition)
|
||||
{
|
||||
$itemKey = $this->makeItemKey($owner, $code);
|
||||
if (!isset($this->items[$itemKey]))
|
||||
return false;
|
||||
|
||||
$mainItem = $this->items[$itemKey];
|
||||
if (isset($mainItem->sideMenu[$sideCode]))
|
||||
$definition = array_merge((array) $mainItem->sideMenu[$sideCode], $definition);
|
||||
|
||||
$item = (object) array_merge(self::$sideItemDefaults, $definition);
|
||||
$this->items[$itemKey]->sideMenu[$sideCode] = $item;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list of the main menu items.
|
||||
* @return array
|
||||
|
|
@ -267,7 +343,7 @@ class NavigationManager
|
|||
* Specifies a code of the side menu item in the current navigation context.
|
||||
* @param string @sideMenuItemCode Specifies the side menu item code
|
||||
*/
|
||||
public function setContextSideMenu($sideMenuItemCode)
|
||||
public function setContextSideMenu($sideMenuItemCode)
|
||||
{
|
||||
$this->contextSideMenuItemCode = $sideMenuItemCode;
|
||||
}
|
||||
|
|
@ -309,4 +385,14 @@ class NavigationManager
|
|||
|
||||
return $items;
|
||||
}
|
||||
|
||||
/**
|
||||
* Internal method to make a unique key for an item.
|
||||
* @param object $item
|
||||
* @return string
|
||||
*/
|
||||
private function makeItemKey($owner, $code)
|
||||
{
|
||||
return strtoupper($owner).'.'.strtoupper($code);
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue