I have the following code in start.php
elgg_register_plugin_hook_handler('register', 'menu:title', 'title_menu_handler');
--------------------
function title_menu_handler($hook, $type, $items, $params) {
$owner = elgg_get_page_owner_entity();
if($owner->type == 'group') {
elgg_unregister_menu_item('title', 'groups:edit');
elgg_unregister_menu_item('title', 'groups:invite');
elgg_unregister_menu_item('title', 'edit:icon');
elgg_unregister_menu_item('title', 'edit:cover');
return $items;
}
}
And in the view I have the following code:
$title_menu = elgg()->menus->getMenu('title', $params);// Trigger the hook
$title_menu = elgg()->menus->getMenu('title', $params);// need to call twice to get the menu structure after triggering the hook
-------------------
-------------------
echo elgg_view_menu('title', array('sort_by' => 'priority'));
If I do not call elgg()->menus->getMenu('title', $params) twice all the unregistered menu items are still displayed.
I'd appreciate an explanation.
Thank you very much
info@elgg.org
Security issues should be reported to security@elgg.org!
©2014 the Elgg Foundation
Elgg is a registered trademark of Thematic Networks.
Cover image by RaĆ¼l Utrera is used under Creative Commons license.
Icons by Flaticon and FontAwesome.
- Reuven@reuven
Reuven - 0 likes
- Jerome Bakker@jeabakker
Jerome Bakker - 0 likes
- Reuven@reuven
Reuven - 0 likes
- Jerome Bakker@jeabakker
Jerome Bakker - 0 likes
- Reuven@reuven
Reuven - 0 likes
- Jerome Bakker@jeabakker
Jerome Bakker - 0 likes
- Reuven@reuven
Reuven - 0 likes
You must log in to post replies.Elgg 2.3.13
In your menu hook function you shouldn't call elgg_unregister_menu_item but instead loop through all the $items and check it $item->getName() is one you wish to remove and then unset it from the $items array
Thank you Jerome, works perfect.
However, for the getSections() (in the following code) to give the right results I still need to call $title_menu twice. If not, the results of count($sections,1) and count($sections,0) give the number of item before the removal.
If your code in the hook works correctly there is no reason why the results between the 2 calls would be different.
Thank you Jerome. Below is my code.
It behaves strangely. Hope you find my mistake.
//======== In the view ======== $title_menu = elgg()->menus->getMenu('title', $params);// Trigger the hook $title_menu = elgg()->menus->getMenu('title', $params);//== need to call twice to get the menu structure after triggering the hook $sections = $title_menu->getSections(); $diff = count($sections,1) - count($sections,0); if ($diff > 0) : ?> <a class="title-button-nav" rel="toggle" data-toggle-selector=".title-menu-panel" href="#"> <?= elgg_view_icon('cog'); ?> </a> <?php endif; ?> <nav id="ram-title" class="title-menu-panel" role="navigation"> <?php echo elgg_view_menu('title', array('sort_by' => 'priority')); ?> </nav>
Like i said in my earlier comment, you need to unset the item from $items like this:
Don't call elgg_unregister_menu_item in the hooks they don't have any effect.
Now everything works well and makes sense.
Thank you very much Jerome for your help..... and patience :)