1
votes

This is a my code. I show menu in page and children pages. The question is, how to show this menu in children's all children pages?

/**
* Implements hook_preprocess_page().
*/
function agnian_material_admin_preprocess_page(&$variables) {
// @todo: add an if statement to add below data only for "page__admin__content" page.
$variables['node_types'] = node_type_get_names();

/** * Render "admin/structure" page's menu in it's all subpages. */

// get current page path
$path = \Drupal::request()->getPathInfo();
if(strpos($path, '/admin/structure') !== false) {
$tree = \Drupal::menuTree();
$params = $tree->getCurrentRouteMenuTreeParameters('admin');
$admin_menu = $tree->load('admin', $params);
$admin_structure_menu = array_key_exists('system.admin_structure', $admin_menu['system.admin']->subtree) ? $admin_menu['system.admin']->subtree['system.admin_structure'] : null;
if($admin_structure_menu) {
$subtree = $tree->build($admin_structure_menu->subtree);

/* this part is only for syncing menu items positions between "admin/structure" page and it's child pages */

$old_items = $subtree['#items'];
$tmp = $subtree['#items'] = array();
foreach($old_items as $key => $value) $tmp[$key] = $value['title'];
asort($tmp);
foreach($tmp as $key => $value) $subtree['#items'][$key] = $old_items[$key];

/* end of sync */

$variables['admin_menu'] = $subtree;
}
}

/** End Of Render "admin/structure" page's menu in it's all subpages. */ }

1
i dont get it , if u associate the pages and subpages to a menu , using the back office , the menu will then be displayed in all theses pagesMatoeil
other option create an menu in the back office, display the menu block and on visiblity parameters choose the page you want it to appearMatoeil
Thank you, for your replay. I do not understand what is back office? With this cod menu is shown only in first child, for example my page is x, menu shown in x and its child x1 , and not shown in x1s child x2 ( x > x1> x2 ). MY problem is to show is menu in x2 and x3 pages.Evi
do u need complex programatic solution ? have u tried using the possiblity offered by the back office ( /admin)Matoeil
I can not use back office , I need only code solutoinEvi

1 Answers

0
votes

try replacing

 if(strpos($path, '/admin/structure') !== false) {

with

 if(preg_match("/admin/structure/[^/]+/[^/]+", $path)!== false) {

[^/]+ meaning "One or more characters except slashes".

or whatever after structure should be

     if(  preg_match("/admin/structure(.*)", $path)!== false) {