Browse files

feat(menus): wrap menu items as collections

Menus service now wraps menus, menu section and menu items as collections,
to make it easer to alter them without having to loop every time an
item is needed.
  • Loading branch information...
hypeJunction committed Apr 5, 2018
1 parent efbdf71 commit 5e96d86426d5182ee9ac1e89972297347e968611
@@ -975,6 +975,8 @@ Menu changes
Default sorting of menu items has been changed from ``text`` to ``priority``.
Note that ``register`` and ``prepare`` hooks now use collections API. For the most part, all hooks should continue working, as long as they are not performing complex operations with arrays.
Support for ``icon`` and ``badge`` parameters was added. Plugins should start using these parameters and prefer them to a single ``text`` parameter. CSS should be used to control visibility of the label, icon and badge, instead of conditionals in preparing menu items.
All menus are now wrapped with ``nav.elgg-menu-container`` to ensure that multiple menu sections have a single parent element, and can be styled using flexbox or floats.
@@ -66,17 +66,37 @@ System hooks
**parameters, menu:<menu_name>**
Triggered by ``elgg_view_menu()``. Used to change menu variables (like sort order) before rendering.
The ``$params`` array will contain:
* ``name`` - name of the menu
* ``sort_by`` - preferring sorting parameter
* other parameters passed to ``elgg_view_menu()``
**register, menu:<menu_name>**
Filters the initial list of menu items pulled from configuration, before the menu has been split into
sections. Triggered by ``elgg_view_menu()`` and ``elgg()->menus->getMenu()``.
The ``$params`` array will contain parameters returned by ``parameters, menu:<menu_name>`` hook.
The return value is an instance of ``\Elgg\Collections\Collection`` containing ``\ElggMenuItem`` objects.
Hook handlers can add/remove items to the collection using the collection API, as well as array access operations.
**prepare, menu:<menu_name>**
Filters the array of menu sections before they're displayed. Each section is a string key mapping to
an area of menu items. This is a good hook to sort, add, remove, and modify menu items. Triggered by
``elgg_view_menu()`` and ``elgg()->menus->prepareMenu()``.
The ``$params`` array will contain:
* ``selected_item`` - ``ElggMenuItem`` selected in the menu, if any
The return value is an instance of ``\Elgg\Menu\PreparedMenu``. The prepared menu is a collection of ``\Elgg\Menu\MenuSection``,
which in turn are collections of ``\ElggMenuItem`` objects.
**register, menu:filter:<filter_id>**
Allows plugins to modify layout filter tabs on layouts that specify ``<filter_id>`` parameter.
Allows plugins to modify layout filter tabs on layouts that specify ``<filter_id>`` parameter. Parameters and return values
are same as in ``register, menu:<menu_name>`` hook.
**filter_tabs, <context>**
Filters the array of ``ElggMenuItem`` used to display the All/Mine/Friends tabs. The ``$params``
@@ -104,12 +104,8 @@ Examples
foreach ($items as $key => $item) {
if ($item->getName() == 'albums') {
// Set the new URL
if ($items->has('albums')) {
return $items;
@@ -142,17 +138,11 @@ Examples
return $menu;
foreach ($items as $key => $item) {
switch ($item->getName()) {
case 'likes':
// Remove the "likes" menu item
case 'edit':
// Change the "Edit" text into a custom icon
if ($items->has('edit')) {
$items->get('edit')->icon = 'pencil';
return $items;
Oops, something went wrong.

0 comments on commit 5e96d86

Please sign in to comment.