Need to call menu hook twice

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

 

  • 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.

    $title_menu = elgg()->menus->getMenu('title', $params);
    $title_menu = elgg()->menus->getMenu('title', $params);
    
    $sections = $title_menu->getSections();
    
    echo count($sections,1);
    
    echo count($sections,0);
  • 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 start.php ==========
    elgg_register_event_handler('init', 'system', 'title_menu_init');
    
    function title_menu_init() {
        elgg_register_plugin_hook_handler('register', 'menu:title', 'title_menu_handler');
    }
    
    function title_menu_handler($hook, $type, $items, $params) {
    
         $unregister = array("groups:edit", "groups:invite", "edit:icon", "edit:cover");
    
         $owner = elgg_get_page_owner_entity();
    
          if($owner->type == 'group') {
    
                foreach ($items as $item) {
    
                     $item_name = $item->getName();
    
                     if (in_array($item_name, $unregister)) {
    
                          elgg_unregister_menu_item('title', $item_name);
    
                      } 
    
                 }
    
                return $items;
    
           }
    
    }
    
    //======== 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:

    foreach ($items as $index => $item) {
    
                     $item_name = $item->getName();
    
                     if (in_array($item_name, $unregister)) {
    
                          unset($items[$index]);
    
                      } 
    
                 }

    Don't call elgg_unregister_menu_item in the hooks they don't have any effect.

  • All is well that ends well

    Now everything works well and makes sense.

    Thank you very much Jerome for your help..... and patience :)