function entity_hierarchy_views_node_view in Entity Reference Hierarchy 8
Implements hook_ENTITY_TYPE_view().
Add the embedded children view to the node body if appropriate.
Todo: hide view if empty and user adds title. See https://www.drupal.org/node/1343430
File
- entity_hierarchy_views/
entity_hierarchy_views.module, line 267 - Contains entity_hierarchy_views.module..
Code
function entity_hierarchy_views_node_view(array &$build, NodeInterface $node, EntityViewDisplayInterface $display, $view_mode) {
if ($view_mode == 'full' && \Drupal::currentUser()
->hasPermission('access embedded child view')) {
static $depth = 0;
// Make sure we don't go through more than 3 iterations to prevent memory or call stack errors.
if ($depth++ < 3) {
// Get the arguments to send to the view. This should allow other view arguments to work.
$current_path = \Drupal::service('path.current')
->getPath();
$arguments = explode('/', $current_path);
// First arg will be 'node', the second will be the node id, so remove them.
array_shift($arguments);
array_shift($arguments);
if ($view = Views::getView($node->nh_children_view)) {
// If we want a title, add the following:
// $title = $view->getTitle();
// $view->setTitle('$title'); might work
// $build['entity_hierarchy_views_embed']['#prefix'] = '<h2 id="hierarchy-title" class ="views-title">'.$title.'</h2>';
$display = @$node->nh_children_view_display ? @$node->nh_children_view_display : 'default';
$view
->setDisplay($display);
$view
->setArguments($arguments);
$build['entity_hierarchy_views_embed'] = $view
->render();
$build['entity_hierarchy_views_embed']['#weight'] = 1001;
}
}
}
}