added parent menu link to production pages

This commit is contained in:
Bachir Soussi Chiadmi
2018-03-04 20:50:39 +01:00
parent a648598b0a
commit 42f6b8eb40
2 changed files with 67 additions and 27 deletions
@@ -32,9 +32,15 @@ function edlp_productions_theme($existing, $type, $theme, $path) {
*/
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('Subtree'),
'description' => 'display prodction menu subtree from current node',
'label' => t('Production Subtree'),
'description' => 'display production menu subtree from current node',
'weight' => 99,
'visible' => FALSE,
];
@@ -47,34 +53,63 @@ function edlp_productions_entity_extra_field_info(){
* @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) {
$display_settings = $display->getComponent('production_subtree');
if (!empty($display_settings)) {
$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 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.
// We found a menu link
$root_menu_item = reset($menu_links);
$menu_parameters = new \Drupal\Core\Menu\MenuTreeParameters();
$menu_parameters->setMaxDepth(1);
$menu_parameters->setRoot($root_menu_item->getPluginId());
$menu_parameters->excludeRoot();
// Get the tree.
$menu_tree_service = \Drupal::service('menu.link_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'] = $menu_tree_service->build($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'] = $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'] = $menu_tree_service->build($tree);
}
}
}
}
@@ -23,19 +23,19 @@ content:
body:
label: hidden
type: text_default
weight: 2
weight: 3
settings: { }
third_party_settings: { }
region: content
field_son:
weight: 1
weight: 2
label: above
settings: { }
third_party_settings: { }
type: file_default
region: content
field_visuel:
weight: 0
weight: 1
label: above
settings:
image_style: ''
@@ -44,12 +44,17 @@ content:
type: image
region: content
links:
weight: 4
weight: 5
region: content
settings: { }
third_party_settings: { }
production_parent:
weight: 0
region: content
settings: { }
third_party_settings: { }
production_subtree:
weight: 3
weight: 4
region: content
settings: { }
third_party_settings: { }