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(){ function edlp_productions_entity_extra_field_info(){
$extra = []; $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'] = [ $extra['node']['page']['display']['production_subtree'] = [
'label' => t('Subtree'), 'label' => t('Production Subtree'),
'description' => 'display prodction menu subtree from current node', 'description' => 'display production menu subtree from current node',
'weight' => 99, 'weight' => 99,
'visible' => FALSE, '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 * @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) { 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'); $parent_display_settings = $display->getComponent('production_parent');
if (!empty($display_settings)) { $subtree_display_settings = $display->getComponent('production_subtree');
if (!empty($parent_display_settings) || !empty($subtree_display_settings)) {
$menu_name = 'productions'; $menu_name = 'productions';
// Find the menu item corresponding to the entity (node). // Find the menu item corresponding to the entity (node).
$menu_link_service = \Drupal::getContainer()->get('plugin.manager.menu.link'); $menu_link_service = \Drupal::getContainer()->get('plugin.manager.menu.link');
$route_params = array('node' => $entity->id()); $route_params = array('node' => $entity->id());
$menu_links = $menu_link_service->loadLinksByRoute('entity.node.canonical', $route_params, $menu_name); $menu_links = $menu_link_service->loadLinksByRoute('entity.node.canonical', $route_params, $menu_name);
if (!empty($menu_links)) { if (!empty($menu_links)) {
// We found a menu link so we can continue with generating the tree. // We found a menu link
// First, setup the parameters: we need one level starting with the menu
// link we found, but excluding it from the tree.
$root_menu_item = reset($menu_links); $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'); $menu_tree_service = \Drupal::service('menu.link_tree');
$tree = $menu_tree_service->load($menu_name, $menu_parameters);
// Apply some manipulators (checking the access, sorting). // parent
$manipulators = [ if(!empty($parent_display_settings)){
['callable' => 'menu.default_tree_manipulators:checkNodeAccess'], $parents_ids = $menu_link_service->getParentIds($root_menu_item->getPluginId());
['callable' => 'menu.default_tree_manipulators:checkAccess'], if(count($parents_ids) > 1){
['callable' => 'menu.default_tree_manipulators:generateIndexAndSort'], // dpm($parents_ids);
]; $root_parent_menu_item_id = array_pop($parents_ids);
$tree = $menu_tree_service->transform($tree, $manipulators); $menu_parameters = new \Drupal\Core\Menu\MenuTreeParameters();
// And the last step is to actually build the tree. $menu_parameters->setMaxDepth(0);
$build['production_subtree'] = $menu_tree_service->build($tree); $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: body:
label: hidden label: hidden
type: text_default type: text_default
weight: 2 weight: 3
settings: { } settings: { }
third_party_settings: { } third_party_settings: { }
region: content region: content
field_son: field_son:
weight: 1 weight: 2
label: above label: above
settings: { } settings: { }
third_party_settings: { } third_party_settings: { }
type: file_default type: file_default
region: content region: content
field_visuel: field_visuel:
weight: 0 weight: 1
label: above label: above
settings: settings:
image_style: '' image_style: ''
@@ -44,12 +44,17 @@ content:
type: image type: image
region: content region: content
links: links:
weight: 4 weight: 5
region: content
settings: { }
third_party_settings: { }
production_parent:
weight: 0
region: content region: content
settings: { } settings: { }
third_party_settings: { } third_party_settings: { }
production_subtree: production_subtree:
weight: 3 weight: 4
region: content region: content
settings: { } settings: { }
third_party_settings: { } third_party_settings: { }