2

Loading a menu tree and parsing the menu_tree_elements loading the menu_link_content entities is not straight forward. I have to perform it like this.

    $menu_id = 'menu';
    $menu_tree = \Drupal::menuTree();
    $menu_parameters = $menu_tree->getCurrentRouteMenuTreeParameters($menu_id);
    $menu_tree_elements = $menu_tree->load($menu_id, $menu_parameters);

    foreach($menu_tree_elements as $menu_tree_element){
        $id = $menu_tree_element->link->getMetaData()['entity_id'];
        $menulink = \Drupal::entityTypeManager()->getStorage('menu_link_content')->load($id);
    var_dump('Title: '. $menulink->label());
    }

I notice that getEntity() is a method that does almost exactly what I'm already doing in order to get the menu_link_content, but it's a protected function. Why would they make that function protected? Is it because I'm doing things in a really round about way when I should be using a different method?

1
  • 2
    Menu link plugins provide an interface to the outside which is independent from the source, so that it can be used in the same way no matter whether derived from an entity, yaml file or dynamically in code. BTW you can also use the UUID to get the entity, see drupal.stackexchange.com/questions/235754/…
    – 4uk4
    Commented Apr 12, 2018 at 16:43

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.