CoolCMS API

Menu extends BaseComponent
in package

Admin menu: a tree navigation of links to admin pages.

Counterpart of Menu, but works with AdminMenuItem and AdminPage. Items are loaded through the API bridge (AdminMenuItemManager -> api/system/admin-menu-item) with the page reference and translations.

Links are warmed up once in initializeRender(): the target AdminPage::getPresenter() + getAction() of each item is resolved through presenter->link() into a pretty admin URL (see AdminRouteFactory).

The active item comes from the page-to-menu-item relation (cms_admin_system_page_menu_item_relations, loaded in PageInitTrait). A menu item points to presenter:show, while the user may be in add/edit/del; those have their own page row but belong under the same menu item.

Table of Contents

Properties

$container  : Container
$routerDispatcher  : RouterDispatcher
$translator  : ITranslator
$appContext  : AppContext
$componentParameters  : array<string, mixed>
$currencyContext  : CurrencyContext|null
$deviceDetector  : DeviceDetector|null
$languageContext  : LanguageContext|null
$localizationContext  : LocalizationContext|null
$settingContext  : SettingContext
$themeContext  : ThemeContext|null
$translatorDomain  : string|null
$adminMenuManager  : AdminMenuItemManager

Methods

__call()  : mixed
Dispatches `renderXyz()` to `render('Xyz', ...$args)`.
__construct()  : mixed
createComponent()  : IComponent|null
getAppContext()  : AppContext
getComponentParameter()  : mixed
Returns a component parameter, or null when it is not set.
getContainer()  : Container
getCurrencyContext()  : CurrencyContext|null
getLanguageContext()  : LanguageContext|null
getLocalizationContext()  : LocalizationContext|null
getRouterDispatcher()  : RouterDispatcher
getSettingContext()  : SettingContext
getSettings()  : array<int, array<string, mixed>>
getThemeContext()  : ThemeContext|null
hasServiceByType()  : bool
Returns whether the container has a service of the given type.
initializeAllContexts()  : void
injectDeviceDetector()  : void
onCreate()  : void
render()  : void
Vertical tree menu (default look of the admin sidebar).
setAppContext()  : $this
setComponentParameter()  : void
Sets a component parameter.
setCurrencyContext()  : $this
setLanguageContext()  : $this
setLocalizationContext()  : $this
setSettingContext()  : $this
setThemeContext()  : $this
setup()  : void
setupAppContext()  : void
typedComponent()  : T
Returns a subcomponent with a verified type.
frontPresenter()  : BasePresenter|null
Returns the front presenter the component is attached to (null outside the front end or when detached).
getTranslator()  : ITranslator
setTemplateFile()  : void
setTranslator()  : self
buildTree()  : array<int, AdminMenuItem, children: array}>
Builds a nested tree by `parentId` (adjacency), independent of lft/rgt, which the admin seed may not have computed. Siblings are ordered by lft, then id.
createMultiplierComponent()  : Multiplier<string|int, IComponent>
Creates a multiplier for the component.
createNewComponentInstance()  : IComponent|null
Creates a new component instance without adding it to the presenter; used by the multiplier.
createStandardComponent()  : IComponent|null
Creates a standard component (not a multiplier).
dropEmptySections()  : array<string|int, AdminMenuItem>
A section heading exists only for the items below it. When privilege pruning empties a section (a role without Billing and Users empties the whole "People" section), the heading must go too, otherwise an empty box stays in the menu.
filterByPrivileges()  : array<string|int, AdminMenuItem>
Drops menu items whose target the user has no privilege for.
initializeRender()  : void
normalizeParentId()  : int|null
presenterClass()  : string
Converts a Nette presenter target (`:Admin:System:Page`) into the full class name (`App\UI\Admin\System\Presenters\PagePresenter`).
resolveLink()  : string|null
Resolves the page target (presenter + action) into a pretty admin URL. Returns null for an item without a page or for a target whose presenter does not exist yet (rendered as text without a link).

Properties

$componentParameters

protected array<string, mixed> $componentParameters = []

component parameters set by setComponentParameter()

$translatorDomain

protected string|null $translatorDomain = 'admin.system.components.menu'

Methods

__call()

Dispatches `renderXyz()` to `render('Xyz', ...$args)`.

public __call(string $name, array<string|int, mixed> $args) : mixed

Called only when the component has no renderXyz() method of its own.

Parameters
$name : string
$args : array<string|int, mixed>

createComponent()

public createComponent(string $name) : IComponent|null
Parameters
$name : string
Return values
IComponent|null

getComponentParameter()

Returns a component parameter, or null when it is not set.

public getComponentParameter(string $name) : mixed
Parameters
$name : string

getContainer()

public getContainer() : Container
Return values
Container

getSettings()

public getSettings() : array<int, array<string, mixed>>
Return values
array<int, array<string, mixed>>

hasServiceByType()

Returns whether the container has a service of the given type.

public hasServiceByType(string $className) : bool

The name is composed dynamically (module plus component), so class_exists() runs first, and a true result guarantees a valid type.

Parameters
$className : string
Tags
phpstan-assert-if-true

class-string $className

Return values
bool

render()

Vertical tree menu (default look of the admin sidebar).

public render([int $menuId = 1 ][, string|null $htmlId = null ]) : void
Parameters
$menuId : int = 1
$htmlId : string|null = null

setComponentParameter()

Sets a component parameter.

public setComponentParameter(string $name, mixed $value) : void
Parameters
$name : string
$value : mixed

typedComponent()

Returns a subcomponent with a verified type.

public typedComponent(string $name, T> $type) : T

Use it instead of an inline @var above $this['x'] (which does not narrow the type) and instead of the generic IComponent that $this['x'] returns. See Form::typedComponent().

Parameters
$name : string
$type : T>
Tags
template
Return values
T

frontPresenter()

Returns the front presenter the component is attached to (null outside the front end or when detached).

protected frontPresenter() : BasePresenter|null
Return values
BasePresenter|null

getTranslator()

protected getTranslator() : ITranslator
Return values
ITranslator

setTemplateFile()

protected setTemplateFile(string $dir, string $filename) : void
Parameters
$dir : string
$filename : string

setTranslator()

protected setTranslator(ITranslator $translator) : self
Parameters
$translator : ITranslator
Return values
self

buildTree()

Builds a nested tree by `parentId` (adjacency), independent of lft/rgt, which the admin seed may not have computed. Siblings are ordered by lft, then id.

private buildTree(array<string|int, AdminMenuItem> $items) : array<int, AdminMenuItem, children: array}>
Parameters
$items : array<string|int, AdminMenuItem>
Return values
array<int, AdminMenuItem, children: array}>

createMultiplierComponent()

Creates a multiplier for the component.

private createMultiplierComponent(string $name) : Multiplier<string|int, IComponent>
Parameters
$name : string
Return values
Multiplier<string|int, IComponent>

createNewComponentInstance()

Creates a new component instance without adding it to the presenter; used by the multiplier.

private createNewComponentInstance(string $baseName[, int|string|null $uniqueKey = null ]) : IComponent|null
Parameters
$baseName : string
$uniqueKey : int|string|null = null
Return values
IComponent|null

createStandardComponent()

Creates a standard component (not a multiplier).

private createStandardComponent(string $name) : IComponent|null
Parameters
$name : string
Return values
IComponent|null

dropEmptySections()

A section heading exists only for the items below it. When privilege pruning empties a section (a role without Billing and Users empties the whole "People" section), the heading must go too, otherwise an empty box stays in the menu.

private dropEmptySections(array<string|int, AdminMenuItem> $items) : array<string|int, AdminMenuItem>

A section is the contiguous run of root items after a separator up to the next separator (or the end of the menu). Sibling order follows lft, same as buildTree().

Parameters
$items : array<string|int, AdminMenuItem>
Return values
array<string|int, AdminMenuItem>

filterByPrivileges()

Drops menu items whose target the user has no privilege for.

private filterByPrivileges(array<string|int, AdminMenuItem> $items) : array<string|int, AdminMenuItem>

IMPORTANT: this only hides links; the server-side gate still decides. Without it the user would see doors that stay locked and a click would end in an access-denied message.

The key is built from the item's target page: <presenter>:<action>, e.g. :Admin:System:UserGroup:show. It is the same convention as PrivilegeCheckTrait::checkCurrentPrivilege() and the read gate in BasePresenter::authorizeListing(), so the menu and the screen cannot disagree.

An item without a target page (heading, folder) is not judged: it has nothing to allow. A folder disappears when every one of its children goes, because an empty folder is worse than none; pruning repeats since a folder can contain a folder. A section heading (separator) never has children, so it follows its own rule in dropEmptySections(): it disappears once the section below it is empty.

IMPORTANT: a key that does not exist in the privilege table hides the item from everyone. The test AdminPrivilegeSurfaceTest (menu targets) guards this, so it is not discovered only when an item vanishes from someone's menu.

Parameters
$items : array<string|int, AdminMenuItem>
Return values
array<string|int, AdminMenuItem>

initializeRender()

private initializeRender(int $menuId, string|null $htmlId) : void
Parameters
$menuId : int
$htmlId : string|null

normalizeParentId()

private normalizeParentId(int|null $parentId) : int|null
Parameters
$parentId : int|null
Return values
int|null

presenterClass()

Converts a Nette presenter target (`:Admin:System:Page`) into the full class name (`App\UI\Admin\System\Presenters\PagePresenter`).

private presenterClass(string $presenter) : string
Parameters
$presenter : string
Return values
string

Resolves the page target (presenter + action) into a pretty admin URL. Returns null for an item without a page or for a target whose presenter does not exist yet (rendered as text without a link).

private resolveLink(AdminMenuItem $item) : string|null

The presenter is checked with class_exists() first: not all admin presenters exist yet and link() would throw "Cannot load presenter" on a missing one.

An item may carry parameters (column params, JSON), so two items can point to a single presenter and differ only in a preset instead of subclassing the presenter per module. The keys must match the persistent parameters of the target, otherwise Nette does not put them into the URL.

Parameters
$item : AdminMenuItem
Return values
string|null
On this page

Search results