NodePreviewController.php 1.71 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
<?php

/**
 * @file
 * Contains \Drupal\node\Controller\NodePreviewController.
 */

namespace Drupal\node\Controller;

use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\Controller\EntityViewController;

/**
 * Defines a controller to render a single node in preview.
 */
class NodePreviewController extends EntityViewController {

  /**
   * {@inheritdoc}
   */
  public function view(EntityInterface $node_preview, $view_mode_id = 'full', $langcode = NULL) {
    $node_preview->preview_view_mode = $view_mode_id;
23
    $build = parent::view($node_preview, $view_mode_id);
24 25 26 27

    $build['#attached']['library'][] = 'node/drupal.node.preview';

    // Don't render cache previews.
28
    unset($build['#cache']);
29 30 31

    foreach ($node_preview->uriRelationships() as $rel) {
      // Set the node path as the canonical URL to prevent duplicate content.
32
      $build['#attached']['html_head_link'][] = array(
33 34 35 36 37 38 39 40
        array(
        'rel' => $rel,
        'href' => $node_preview->url($rel),
        )
        , TRUE);

      if ($rel == 'canonical') {
        // Set the non-aliased canonical path as a default shortlink.
41
        $build['#attached']['html_head_link'][] = array(
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
          array(
            'rel' => 'shortlink',
            'href' => $node_preview->url($rel, array('alias' => TRUE)),
          )
        , TRUE);
      }
    }

    return $build;
  }

  /**
   * The _title_callback for the page that renders a single node in preview.
   *
   * @param \Drupal\Core\Entity\EntityInterface $node_preview
   *   The current node.
   *
   * @return string
   *   The page title.
   */
  public function title(EntityInterface $node_preview) {
63
    return $this->entityManager->getTranslationFromContext($node_preview)->label();
64 65 66
  }

}