How to copy Elgg Menu Items to a new menu?

I am trying to copy only a few items from menu:entity into another menu so I can use the new menu when a criteria is met. As I'm looping through each existing menu item in the hook handler function I am trying to create a new menu item but it does not seem to work when I call the new menu using echo elgg_view_menu. 

 

   foreach($value as $index => $item){

                $name = $item->getName();

                $href = $item->getHref();

                $text = $item->getText();

                $menu_item = new ElggMenuItem($name,$text,$href);

                elgg_register_menu_item('no_reply', $menu_item);

}

 

  • // This does not go into a menu hook
    // Put it somewhere in the views or global handlers
    
    $old_menu = elgg()->menus->getMenu('old_menu', $params);
    $sections = $old_menu->getSections();
    foreach ($sections as $section => $items) {
      foreach ($items as $item) {
        elgg_register_menu_item('new_menu', $item);
       
        // or in 2.3 you can inject items into elgg_view_menu()
        $new_menu_items[] = $item;
      }
    }
    
    echo elgg_view_menu('new_menu', [
       'items' => $new_menu_items,
    ]);
    
    // If you insist on using a hook
    // Give your hook registration high priority, so it's called after all other hooks
    elgg_register_plugin_hook_handler('register', 'menu:old_menu', function($hook, $type, $return, $params) {
    
       foreach ($return as $item) {
          elgg_register_menu_item('new_menu', $item);
       }
    }, 999);
    
  • This comment was removed by a moderator because it contained advertising.

  • Sorry, pressed the wrong button and removed your comment by accident. The menu API may have been added in the later Elgg version. So, use the hook approach

  • Ismayil,

    hahaha, no problem. I updated to 2.3.0. I will test again and let you know how did it go. Like always, thanks for your replies. 

  • Ok, I finally had the opportunity to try it. I tried the first part of the code but I am getting the following error. 

    Call to a member function canEdit() on null in /usr/local/elgg/2.3.0/vendor/elgg/elgg/engine/lib/navigation.php on line 484

    This is how I did it.

    $p = array( 
    'entity' => $posts, 
    'handler' => 'thewire', 
    'sort_by' => 'priority', 
    'class' => 'elgg-menu-hz', ); 
    
    $old_menu = elgg()->menus->getMenu('entity', $p); 
    $sections = $old_menu->getSections(); 
    
    foreach ($sections as $section => $items) { 
      foreach ($items as $item) { 
         elgg_register_menu_item('no_reply', $item); 
    
         // or in 2.3 you can inject items into elgg_view_menu() 
         $new_menu_items[] = $item; 
      } 
    } 
    
    echo elgg_view_menu('no_reply',[ 'items' => $new_menu_items, ]);

    The hook method worked though.

     

  • I just remembered that the there is an option to combine menus somewhere in the menus API. Just dig through the code. 

Beginning Developers

Beginning Developers

This space is for newcomers, who wish to build a new plugin or to customize an existing one to their liking