You are here

function entity_hierarchy_node_view in Entity Reference Hierarchy 8

Implements hook_ENTITY_TYPE_view().

File

./entity_hierarchy.module, line 273
A module to make nodes hierarchical.

Code

function entity_hierarchy_node_view(array &$build, EntityInterface $node, EntityViewDisplayInterface $display, $view_mode) {
  if ($view_mode == 'full') {
    $create_links = array();
    $current_user = \Drupal::currentUser();

    /** @var \Drupal\entity_hierarchy\HierarchyManager $hierarchy_manager */
    $hierarchy_manager = \Drupal::service('entity_hierarchy.manager');
    if ($current_user
      ->hasPermission('create child nodes') && $current_user
      ->hasPermission('create child of any parent' || $current_user
      ->hasPermission('update'))) {
      foreach ($hierarchy_manager
        ->hierarchyGetAllowedChildTypes($node
        ->getType()) as $key) {
        if ($node
          ->access()) {
          $destination = (array) drupal_get_destination() + array(
            'parent' => $node
              ->id(),
          );

          //d7: $key = str_replace('_', '-', $key);
          $url = \Drupal\Core\Url::fromRoute('node.add', array(
            'node_type' => $key,
          ), array(
            'query' => $destination,
          ));
          $link = \Drupal\Core\Link::fromTextAndUrl(t($key), $url);
          $create_links[] = render(@$link
            ->toRenderable());
        }
        if ($create_links) {
          $build['entity_hierarchy_new_child_links'] = array(
            // @todo: handle this with hook_theme and template_preprocess (see below) and use #theme over #markup
            // @see function template_preprocess_book_navigation
            // '#theme' => 'entity_hierarchy_new_child_links',
            // The Create new child link should probably come after the content, so make the weight high.
            '#weight' => 1000,
            '#markup' => '<div class="newchild">' . t('Create new child ') . implode(" | ", $create_links) . '</div>',
          );
        }
      }
    }
  }
}