0

I have the following menu in my system.

screenshot

The parent menu-link connects to a node whose ID is 58. All the sub menu-links have a parent ID equal to 58.

I need to find the main and the sub-menus using 58, but I have been unsuccessful at locating the a table that contains that information.

How can I list the nodes that share a particular menu ID?

3
  • Are you actually trying to find just the nodes that are children of a particular parent menu item and do something with them? or are you just trying to display all of a particular menu item's children (whether the child menu items point to nodes or other)?
    – sonfd
    Oct 8 at 12:40
  • Yes. I'm trying to get the nodes to do something with them The parent and offspring node IDs will go into some logic else where on the site.
    – sisko
    Oct 8 at 15:47
  • @sisko did you have a chance to try my suggested answer? Oct 14 at 16:30
0

The menu_link_content entity definition contains information related to your question; namely, the database table that contain the parent-child relation would be menu_link_content_data.

Below is a code sample that would deliver an array of node ids given a parent node, provided that there is a menu link attached to the node. This code uses Drupal's APIs instead of SQL queries.

<?php

// Get the root based on a given node, e.g. node 58.
$menu_link_root_entities = \Drupal::entityTypeManager()
  ->getStorage('menu_link_content')
  ->loadByProperties([
      'link'  => [
        'uri' => 'entity:node/58'
      ],
    ],
);
$menu_link_root = reset($menu_link_root_entities);

// Need to call this in order to instantiate the tree_storage service.
$menu_tree = \Drupal::menuTree();
// Get menu link content entities based on their parent property.
$menu_links = \Drupal::service('menu.tree_storage')->loadByProperties([
    // Need a filter value like menu_link_content:8031d182-7a0b-4798-839a-6c66bdd1f27b
    'parent' => 'menu_link_content:' . $menu_link_root->uuid(),
]) ?: [];

$node_ids = array_map(function($v){
    return $v['route_parameters']['node'];
}, $menu_links);

Hope this helps, good luck!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.