You are here

public function NodePreviewController::view in Zircon Profile 8.0

Same name and namespace in other branches
  1. 8 core/modules/node/src/Controller/NodePreviewController.php \Drupal\node\Controller\NodePreviewController::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

1 string reference to 'NodePreviewController::view'
node.routing.yml in core/modules/node/node.routing.yml
core/modules/node/node.routing.yml

File

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

Class

NodePreviewController
Defines a controller to render a single node in preview.

Namespace

Drupal\node\Controller

Code

public function view(EntityInterface $node_preview, $view_mode_id = 'full', $langcode = NULL) {
  $node_preview->preview_view_mode = $view_mode_id;
  $build = parent::view($node_preview, $view_mode_id);
  $build['#attached']['library'][] = 'node/drupal.node.preview';

  // Don't render cache previews.
  unset($build['#cache']);
  foreach ($node_preview
    ->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_preview
          ->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_preview
            ->url($rel, array(
            'alias' => TRUE,
          )),
        ),
        TRUE,
      );
    }
  }
  return $build;
}