edlp_productions.module 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php
  2. # @Author: Bachir Soussi Chiadmi <bach>
  3. # @Date: 13-12-2017
  4. # @Email: bachir@figureslibres.io
  5. # @Filename: edlp_productions.module
  6. # @Last modified by: bach
  7. # @Last modified time: 20-12-2017
  8. # @License: GPL-V3
  9. use Drupal\Core\Menu\MenuTreeParameters;
  10. use Drupal\menu_link_content\Entity\MenuLinkContent;
  11. /**
  12. * Implements hook_theme().
  13. */
  14. function edlp_productions_theme($existing, $type, $theme, $path) {
  15. // @see https://www.drupal.org/docs/8/theming/twig/create-custom-twig-templates-from-custom-module
  16. return array(
  17. 'edlp_productions' => array(
  18. 'file' => 'includes/edlp_productions.inc',
  19. 'variables' => array(
  20. 'nodes_entities' => NULL,
  21. ),
  22. ),
  23. );
  24. }
  25. /**
  26. * hook_entity_extra_field_info()
  27. *
  28. */
  29. function edlp_productions_entity_extra_field_info(){
  30. $extra = [];
  31. $extra['node']['page']['display']['production_parent'] = [
  32. 'label' => t('Production Parent'),
  33. 'description' => 'display production menu parent from current node',
  34. 'weight' => 99,
  35. 'visible' => FALSE,
  36. ];
  37. $extra['node']['page']['display']['production_subtree'] = [
  38. 'label' => t('Production Subtree'),
  39. 'description' => 'display production menu subtree from current node',
  40. 'weight' => 99,
  41. 'visible' => FALSE,
  42. ];
  43. return $extra;
  44. }
  45. /**
  46. * Implements hook_ENTITY_TYPE_view().
  47. * @see https://www.amazeelabs.com/en/render-menu-tree-custom-code-drupal-8
  48. */
  49. function edlp_productions_node_view(array &$build, \Drupal\Core\Entity\EntityInterface $entity, \Drupal\Core\Entity\Display\EntityViewDisplayInterface $display, $view_mode) {
  50. $parent_display_settings = $display->getComponent('production_parent');
  51. $subtree_display_settings = $display->getComponent('production_subtree');
  52. if (!empty($parent_display_settings) || !empty($subtree_display_settings)) {
  53. $menu_name = 'productions';
  54. // Find the menu item corresponding to the entity (node).
  55. $menu_link_service = \Drupal::getContainer()->get('plugin.manager.menu.link');
  56. $route_params = array('node' => $entity->id());
  57. $menu_links = $menu_link_service->loadLinksByRoute('entity.node.canonical', $route_params, $menu_name);
  58. if (!empty($menu_links)) {
  59. // We found a menu link
  60. $root_menu_item = reset($menu_links);
  61. $menu_tree_service = \Drupal::service('menu.link_tree');
  62. // parent
  63. if(!empty($parent_display_settings)){
  64. $parents_ids = $menu_link_service->getParentIds($root_menu_item->getPluginId());
  65. if(count($parents_ids) > 1){
  66. // dpm($parents_ids);
  67. $root_parent_menu_item_id = array_pop($parents_ids);
  68. $menu_parameters = new \Drupal\Core\Menu\MenuTreeParameters();
  69. $menu_parameters->setMaxDepth(0);
  70. $menu_parameters->setRoot($root_parent_menu_item_id);
  71. // Get the tree.
  72. $tree = $menu_tree_service->load($menu_name, $menu_parameters);
  73. // Apply some manipulators (checking the access, sorting).
  74. $manipulators = [
  75. ['callable' => 'menu.default_tree_manipulators:checkNodeAccess'],
  76. ['callable' => 'menu.default_tree_manipulators:checkAccess'],
  77. ['callable' => 'menu.default_tree_manipulators:generateIndexAndSort'],
  78. ];
  79. $tree = $menu_tree_service->transform($tree, $manipulators);
  80. // And the last step is to actually build the tree.
  81. $build['production_parent'] = array(
  82. '#type'=>"container",
  83. '#attributes'=>array(
  84. 'class'=>'productions-parent'
  85. ),
  86. 'menu'=>$menu_tree_service->build($tree)
  87. );
  88. }
  89. }
  90. // subtree
  91. if(!empty($subtree_display_settings)){
  92. // so we can continue with generating the tree.
  93. // First, setup the parameters: we need one level starting with the menu
  94. // link we found, but excluding it from the tree.
  95. $menu_parameters = new \Drupal\Core\Menu\MenuTreeParameters();
  96. $menu_parameters->setMaxDepth(1);
  97. $menu_parameters->setRoot($root_menu_item->getPluginId());
  98. $menu_parameters->excludeRoot();
  99. // Get the tree.
  100. $tree = $menu_tree_service->load($menu_name, $menu_parameters);
  101. // Apply some manipulators (checking the access, sorting).
  102. $manipulators = [
  103. ['callable' => 'menu.default_tree_manipulators:checkNodeAccess'],
  104. ['callable' => 'menu.default_tree_manipulators:checkAccess'],
  105. ['callable' => 'menu.default_tree_manipulators:generateIndexAndSort'],
  106. ];
  107. $tree = $menu_tree_service->transform($tree, $manipulators);
  108. // And the last step is to actually build the tree.
  109. $build['production_subtree'] = array(
  110. '#type'=>"container",
  111. '#attributes'=>array(
  112. 'class'=>'productions-subtree'
  113. ),
  114. 'menu'=>$menu_tree_service->build($tree)
  115. );
  116. }
  117. }
  118. }
  119. }