# @Date: 13-12-2017 # @Email: bachir@figureslibres.io # @Filename: edlp_productions.module # @Last modified by: bach # @Last modified time: 20-12-2017 # @License: GPL-V3 use Drupal\Core\Menu\MenuTreeParameters; use Drupal\menu_link_content\Entity\MenuLinkContent; /** * Implements hook_theme(). */ function edlp_productions_theme($existing, $type, $theme, $path) { // @see https://www.drupal.org/docs/8/theming/twig/create-custom-twig-templates-from-custom-module return array( 'edlp_productions' => array( 'file' => 'includes/edlp_productions.inc', 'variables' => array( 'nodes_entities' => NULL, ), ), ); } /** * hook_entity_extra_field_info() * */ function edlp_productions_entity_extra_field_info(){ $extra = []; $extra['node']['page']['display']['production_parent'] = [ 'label' => t('Production Parent'), 'description' => 'display production menu parent from current node', 'weight' => 99, 'visible' => FALSE, ]; $extra['node']['page']['display']['production_subtree'] = [ 'label' => t('Production Subtree'), 'description' => 'display production menu subtree from current node', 'weight' => 99, 'visible' => FALSE, ]; return $extra; } /** * Implements hook_ENTITY_TYPE_view(). * @see https://www.amazeelabs.com/en/render-menu-tree-custom-code-drupal-8 */ function edlp_productions_node_view(array &$build, \Drupal\Core\Entity\EntityInterface $entity, \Drupal\Core\Entity\Display\EntityViewDisplayInterface $display, $view_mode) { $parent_display_settings = $display->getComponent('production_parent'); $subtree_display_settings = $display->getComponent('production_subtree'); if (!empty($parent_display_settings) || !empty($subtree_display_settings)) { $menu_name = 'productions'; // Find the menu item corresponding to the entity (node). $menu_link_service = \Drupal::getContainer()->get('plugin.manager.menu.link'); $route_params = array('node' => $entity->id()); $menu_links = $menu_link_service->loadLinksByRoute('entity.node.canonical', $route_params, $menu_name); if (!empty($menu_links)) { // We found a menu link $root_menu_item = reset($menu_links); $menu_tree_service = \Drupal::service('menu.link_tree'); // parent if(!empty($parent_display_settings)){ $parents_ids = $menu_link_service->getParentIds($root_menu_item->getPluginId()); if(count($parents_ids) > 1){ // dpm($parents_ids); $root_parent_menu_item_id = array_pop($parents_ids); $menu_parameters = new \Drupal\Core\Menu\MenuTreeParameters(); $menu_parameters->setMaxDepth(0); $menu_parameters->setRoot($root_parent_menu_item_id); // Get the tree. $tree = $menu_tree_service->load($menu_name, $menu_parameters); // Apply some manipulators (checking the access, sorting). $manipulators = [ ['callable' => 'menu.default_tree_manipulators:checkNodeAccess'], ['callable' => 'menu.default_tree_manipulators:checkAccess'], ['callable' => 'menu.default_tree_manipulators:generateIndexAndSort'], ]; $tree = $menu_tree_service->transform($tree, $manipulators); // And the last step is to actually build the tree. $build['production_parent'] = array( '#type'=>"container", '#attributes'=>array( 'class'=>'productions-parent' ), 'menu'=>$menu_tree_service->build($tree) ); } } // subtree if(!empty($subtree_display_settings)){ // so we can continue with generating the tree. // First, setup the parameters: we need one level starting with the menu // link we found, but excluding it from the tree. $menu_parameters = new \Drupal\Core\Menu\MenuTreeParameters(); $menu_parameters->setMaxDepth(1); $menu_parameters->setRoot($root_menu_item->getPluginId()); $menu_parameters->excludeRoot(); // Get the tree. $tree = $menu_tree_service->load($menu_name, $menu_parameters); // Apply some manipulators (checking the access, sorting). $manipulators = [ ['callable' => 'menu.default_tree_manipulators:checkNodeAccess'], ['callable' => 'menu.default_tree_manipulators:checkAccess'], ['callable' => 'menu.default_tree_manipulators:generateIndexAndSort'], ]; $tree = $menu_tree_service->transform($tree, $manipulators); // And the last step is to actually build the tree. $build['production_subtree'] = array( '#type'=>"container", '#attributes'=>array( 'class'=>'productions-subtree' ), 'menu'=>$menu_tree_service->build($tree) ); } } } }