Builds a menu tree.
This function may be used build the data for a menu tree only, for example to further massage the data manually before further processing happens. menu_tree_check_access() needs to be invoked afterwards.
function _menu_build_tree($menu_name, array $parameters = array()) {
// Static cache of already built menu trees.
$trees =& drupal_static(__FUNCTION__, array());
$language_interface = language(Language::TYPE_INTERFACE);
// Build the cache id; sort parents to prevent duplicate storage and remove
// default parameter values.
if (isset($parameters['expanded'])) {
sort($parameters['expanded']);
}
$tree_cid = 'links:' . $menu_name . ':tree-data:' . $language_interface->langcode . ':' . hash('sha256', serialize($parameters));
// If we do not have this tree in the static cache, check {cache_menu}.
if (!isset($trees[$tree_cid])) {
$cache = cache('menu')
->get($tree_cid);
if ($cache && isset($cache->data)) {
$trees[$tree_cid] = $cache->data;
}
}
if (!isset($trees[$tree_cid])) {
$query = Drupal::entityQuery('menu_link');
for ($i = 1; $i <= MENU_MAX_DEPTH; $i++) {
$query
->sort('p' . $i, 'ASC');
}
$query
->condition('menu_name', $menu_name);
if (!empty($parameters['expanded'])) {
$query
->condition('plid', $parameters['expanded'], 'IN');
}
elseif (!empty($parameters['only_active_trail'])) {
$query
->condition('mlid', $parameters['active_trail'], 'IN');
}
$min_depth = isset($parameters['min_depth']) ? $parameters['min_depth'] : 1;
if ($min_depth != 1) {
$query
->condition('depth', $min_depth, '>=');
}
if (isset($parameters['max_depth'])) {
$query
->condition('depth', $parameters['max_depth'], '<=');
}
// Add custom query conditions, if any were passed.
if (isset($parameters['conditions'])) {
foreach ($parameters['conditions'] as $column => $value) {
$query
->condition($column, $value);
}
}
// Build an ordered array of links using the query result object.
$links = array();
if ($result = $query
->execute()) {
$links = menu_link_load_multiple($result);
}
$active_trail = isset($parameters['active_trail']) ? $parameters['active_trail'] : array();
$data['tree'] = menu_tree_data($links, $active_trail, $min_depth);
$data['node_links'] = array();
menu_tree_collect_node_links($data['tree'], $data['node_links']);
// Cache the data, if it is not already in the cache.
cache('menu')
->set($tree_cid, $data, CacheBackendInterface::CACHE_PERMANENT, array(
'menu' => $menu_name,
));
$trees[$tree_cid] = $data;
}
return $trees[$tree_cid];
}