Backend\Classes\NavigationManager

Overview

NavigationManager manages the backend navigation.


Protected Properties

protected [[MainMenuItem[]|null]] $items

items contains a list of registered items.

protected array|null $menuDisplayTree

protected string $contextOwner

protected string $contextMainMenuItemCode

protected string $contextSideMenuItemCode

protected array $contextSidenavPartials


Public Methods

public addMainMenuItem()

public addMainMenuItem(
    string $owner,
    string $code,
    array $definition
): void

addMainMenuItem dynamically adds a single main menu item.

public addMainMenuItems()

public addMainMenuItems(string $owner, array $definitions): void

addMainMenuItems dynamically adds an array of main menu items.

public addSideMenuItem()

public addSideMenuItem(
    string $owner,
    string $code,
    string $sideCode,
    array $definition
): bool 

addSideMenuItem dynamically add a single side menu item

public addSideMenuItems()

public addSideMenuItems(
    string $owner,
    string $code,
    array $definitions
): void

addSideMenuItems dynamically adds an array of side menu items

public getActiveMainMenuItem()

public getActiveMainMenuItem(): null|MainMenuItem 

getActiveMainMenuItem returns the currently active main menu item

public getContext()

public getContext(): mixed 

getContext returns information about the current navigation context.

public getContextSidenavPartial()

public getContextSidenavPartial(
    string $owner,
    string $mainMenuItemCode
): mixed 

getContextSidenavPartial returns the side navigation partial for a specific main menu previously registered with the registerContextSidenavPartial() method.

public getMainMenuItem()

public getMainMenuItem($owner, $code): Backend\Classes\MainMenuItem|null 

getMainMenuItem returns a main menu item

public getSideMenuItem()

public getSideMenuItem(
    $owner,
    $code,
    $sideCode
): Backend\Classes\SideMenuItem|null 

getSideMenuItem returns a side menu item

public init()

public init(): void

init this class items

public static instance()

public static instance(): static 

instance creates a new instance of this singleton

public isDashboardItemActive()

public isDashboardItemActive(): bool 

isDashboardItemActive determines if the dashboard is active.

public isMainMenuItemActive()

public isMainMenuItemActive(MainMenuItem $item): boolean 

isMainMenuItemActive determines if a main menu item is active.

public isSideMenuItemActive()

public isSideMenuItemActive(SideMenuItem $item): boolean 

isSideMenuItemActive determines if a side menu item is active.

public listMainMenuItems()

public listMainMenuItems(): array 

listMainMenuItems returns a list of the main menu items.

public listMainMenuItemsWithSubitems()

public listMainMenuItemsWithSubitems(): void

listMainMenuItemsWithSubitems prepares data for displaying the top menu and side (collapsable) menu. Uses caching to avoid running counter functions twice.

public listMainMenuSubItems()

public listMainMenuSubItems(): void

listMainMenuSubItems uses cached result of listMainMenuItemsWithSubitems to return submenu items and avoid duplicate counter calls.

public listSideMenuItems()

public listSideMenuItems(
    null $owner = null,
    null $code = null
): SideMenuItem[] 

listSideMenuItems returns a list of side menu items for the currently active main menu item. The currently active main menu item is set with the setContext methods.

public registerCallback()

public registerCallback(callable $callback): void

registerCallback function that defines menu items. The callback function should register menu items by calling the manager's registerMenuItems method. The manager instance is passed to the callback function as an argument. Usage:

BackendMenu::registerCallback(function ($manager) {
    $manager->registerMenuItems([...]);
});

public registerContextSidenavPartial()

public registerContextSidenavPartial(
    string $owner,
    string $mainMenuItemCode,
    string $partial
): void

registerContextSidenavPartial registers a special side navigation partial for a specific main menu. The sidenav partial replaces the standard side navigation.

public registerMenuItems()

public registerMenuItems(string $owner, array $definitions): void

registerMenuItems for the back-end menu items. The argument is an array of the main menu items. The array keys represent the menu item codes, specific for the plugin/module. Each element in the array should be an associative array with the following keys:

  • label - specifies the menu label localization string key, required.
  • icon - an icon name from the Font Awesome icon collection, required.
  • url - the back-end relative URL the menu item should point to, required.
  • permissions - an array of permissions the back-end user should have, optional. The item will be displayed if the user has any of the specified permissions.
  • order - a position of the item in the menu, optional.
  • counter - an optional numeric value to output near the menu icon. The value should be a number or a callable returning a number.
  • counterLabel - an optional string value to describe the numeric reference in counter.
  • sideMenu - an array of side menu items, optional. If provided, the array items should represent the side menu item code, and each value should be an associative array with the following keys:
    • label - specifies the menu label localization string key, required.
    • icon - an icon name from the Font Awesome icon collection, required.
    • url - the back-end relative URL the menu item should point to, required.
    • attributes - an array of attributes and values to apply to the menu item, optional.
    • permissions - an array of permissions the back-end user should have, optional.
    • counter - an optional numeric value to output near the menu icon. The value should be a number or a callable returning a number.
    • counterLabel - an optional string value to describe the numeric reference in counter.

public removeMainMenuItem()

public removeMainMenuItem($owner $owner, $code $code): void

removeMainMenuItem removes a single main menu item

public removeSideMenuItem()

public removeSideMenuItem(
    string $owner,
    string $code,
    string $sideCode
): bool 

removeSideMenuItem removes a single main menu item

public removeSideMenuItems()

public removeSideMenuItems(
    string $owner,
    string $code,
    array $sideCodes
): void 

removeSideMenuItems with multiple codes

public resetCache()

public resetCache(): void

resetCache resets any memory or cache involved with the sites

public setContext()

public setContext(
    string $owner,
    string $mainMenuItemCode,
    string $sideMenuItemCode = null
): void

setContext sets the navigation context. The function sets the navigation owner, main menu item code and the side menu item code.

public setContextMainMenu()

public setContextMainMenu(string $mainMenuItemCode): void

setContextMainMenu specifies a code of the main menu item in the current navigation context.

public setContextOwner()

public setContextOwner(string $owner): void

setContextOwner sets the navigation context owner. The function sets the navigation owner.

public setContextSideMenu()

public setContextSideMenu(string $sideMenuItemCode): void

setContextSideMenu specifies a code of the side menu item in the current navigation context. If the code is set to TRUE, the first item will be flagged as active.

Protected Methods

protected defineMainMenuItem()

protected defineMainMenuItem($config): Backend\Classes\MainMenuItem 

protected defineSideMenuItem()

protected defineSideMenuItem($config): Backend\Classes\SideMenuItem 

protected filterItemPermissions()

protected filterItemPermissions(
    Backend\Models\User $user,
    array $items
): array 

filterItemPermissions removes menu items from an array if the supplied user lacks permission.

protected getCallableCounterValue()

protected getCallableCounterValue($item): void

getCallableCounterValue returns the counter value for a menu item

protected loadItems()

protected loadItems(): void

loadItems from modules and plugins

protected makeItemKey()

protected makeItemKey(string $owner, string $code): string 

makeItemKey is an internal method to make a unique key for an item.