Score:0

List all the nodes with the same menu parent ID

pt flag

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?

sonfd avatar
in flag
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)?
pt flag
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.
Stefanos Petrakis avatar
in flag
@sisko did you have a chance to try my suggested answer?
Score:0
in flag

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!

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.