You are here

public function NodeViewController::view in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/node/src/Controller/NodeViewController.php \Drupal\node\Controller\NodeViewController::view()

Provides a page to render a single entity.

Parameters

\Drupal\Core\Entity\EntityInterface $_entity: The Entity to be rendered. Note this variable is named $_entity rather than $entity to prevent collisions with other named placeholders in the route.

string $view_mode: (optional) The view mode that should be used to display the entity. Defaults to 'full'.

Return value

array A render array as expected by drupal_render().

Overrides EntityViewController::view

File

core/modules/node/src/Controller/NodeViewController.php, line 21
Contains \Drupal\node\Controller\NodeViewController.

Class

NodeViewController
Defines a controller to render a single node.

Namespace

Drupal\node\Controller

Code

public function view(EntityInterface $node, $view_mode = 'full', $langcode = NULL) {
  $build = parent::view($node, $view_mode, $langcode);
  foreach ($node
    ->uriRelationships() as $rel) {

    // Set the node path as the canonical URL to prevent duplicate content.
    $build['#attached']['html_head_link'][] = array(
      array(
        'rel' => $rel,
        'href' => $node
          ->url($rel),
      ),
      TRUE,
    );
    if ($rel == 'canonical') {

      // Set the non-aliased canonical path as a default shortlink.
      $build['#attached']['html_head_link'][] = array(
        array(
          'rel' => 'shortlink',
          'href' => $node
            ->url($rel, array(
            'alias' => TRUE,
          )),
        ),
        TRUE,
      );
    }
  }
  return $build;
}