Hello
I'm trying to upgrade a module from D7 and I'm facing some concerns about getting the main menu links.
I read the change list where they said we want to use : MenuLinkTreeInterface::load()
so I tried this :
use Drupal\Core\Menu\MenuLinkTreeInterface;
...
function xxx() {
$mainmenu = MenuLinkTreeInterface::load('main');
But I've got an error :
Cannot call abstract method Drupal\Core\Menu\MenuLinkTreeInterface::load()
that I couldn't solve.
After that I tried :
$mainmenu = menu_load_links($menu_name);
But I've got another error (undefined function although it's not a deprecated function I think...)
How can I get my links list from the main-menu (now called 'main' if I'm correct...) ?
Any idea welcome...
Comments
$menu_tree = \Drupal::menuTree();
Is this it: https://api.drupal.org/api/drupal/core!lib!Drupal!Core!Menu!menu.api.php...
Particularly the code starting with...
$menu_tree = \Drupal::menuTree();
Thx for helping me. I tried
Thx for helping me. I tried this path and I get (with ksm($menu)) a huge output whose I cant find anything useful : how to eventually get label + link ?
I printed $menu_html as well and can see the menu links : this is the output
$args array(1)
→Called from /drupal8/modules/contrib/devel/kint/kint.module:55 [ksm()]
stdClass Object
(
[__CLASS__] => Drupal\Core\Render\Markup
[string] =>
Fancy (displayed as a link)
Home ...
)
But this doesn't help me to figure out the structure and how to browse into : where is burried the url, for instance ?
Artatum
www.ixxi-mobility.com
After diving in the ksm print
After diving in the ksm print I found the 'internalPath' and 'title' properties :
I tried to print them. I had an error because of protected status for internalPath :
Cannot access protected property Drupal\Core\Url::$internalPath
1 - I found ['menu_link_content:c4017fbc-d067-4df2-9fcd-f7e3e09ab822'] a little ... crazy !
2 - What can I do for this error and grab the url ?
3 - Is there any function to get some old good array ? Something like :
Artatum
www.ixxi-mobility.com
Nothing so far
I've spent a good bit of time trying to find a way, and have come up empty thus far. I can't seem to find an example anywhere in the code. I can only find examples where menus are rendered, edited, etc. But none of that code seems to work for accessing the link tree as was possible (and easy) in D7. No doubt it will be easy in D8 too, once we know how it's done. There might be some clues at... https://www.drupal.org/node/2302069 -- but I looked at some similar docs that ultimately did not help.
But I am finding that my browser crashes whenever I try to access the links in a "foreach ($tree as $element)" loop. Trying to var_dump a link crashes my browser, so it looks like I'll need some other way to debug in classes.
If you figure out how to access the links in the tree, please post your solution.
Hi
Hi
I wish you a Happy New Drupal Year Arnoldbird !
I followed up the code you showed me and edited it a little bit : It's yet not perfect (especially about front ) but it works : you get an indexed array as output:
$title => $link
NB in :
if ($item['url']->getRouteName() == 'front') {
you need inf and sup around front, as usual (the actual editor deletes them)
Artatum
www.ixxi-mobility.com
For debugging you need kint.
For debugging you need kint.
Artatum
www.ixxi-mobility.com
Nice! Pasted it into my
Nice! Pasted it into my module, and it works just like you have it. I might just return $menu and then have a few functions for common ways I'll need to loop through that array.
Worked for me
It would be better if it works for sub navigation items also.
I am trying on that.
Nice job.
Just noting another way to
Just noting another way to get the menu links:
For example:
This is worked for me.
This seems to work for me -- modified version
Is there any way it could get multiple level of the menu
n8tron is there any way i can get all the levels of the menu using this code or using recursive function. this code only works for 1 level
https://drupal.stackexchange
https://drupal.stackexchange.com/questions/252503/can-menutree-load-a-menus-items-including-children has info about getting children items, too, by changing the $parameters.