1

The menu per role module does exactly like you want. It allows you to set a menu link to only be visible by a specific role, in this case anonymous users.


1

Set onlyEnabledLinks() on the menu tree parameters: $parameters = new MenuTreeParameters(); $parameters->onlyEnabledLinks(); $tree = \Drupal::menuTree()->load('main', $parameters);


1

@Weri, I would avoid doing that, having just implemeted your suggestion and spent the best part of a day trying to fix another issue. The query alter you suggest also affects entiry reference when linking paragraphs to nodes. A node I was woking on had 80+ paragraph item before I added the alter. Once added I was unable to save the node. Removing/Commenting ...


1

To expand on @skorzh answer and reply to the comment from @Matt, if you want to nest a menu item inside another you have to get the parent id and set it as 'parent'. As an example this code nests the item inside the top level using the variable $top_level below. $my_menu = \Drupal::entityTypeManager()->getStorage('menu_link_content') ->...


Only top voted, non community-wiki answers of a minimum length are eligible