NodeViewController.php 3.66 KB
Newer Older
1 2 3 4 5 6
<?php

namespace Drupal\node\Controller;

use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\Controller\EntityViewController;
7 8 9 10
use Drupal\Core\Entity\EntityManagerInterface;
use Drupal\Core\Render\RendererInterface;
use Drupal\Core\Session\AccountInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
11 12 13 14 15 16

/**
 * Defines a controller to render a single node.
 */
class NodeViewController extends EntityViewController {

17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
  /**
   * The current user.
   *
   * @var \Drupal\Core\Session\AccountInterface
   */
  protected $currentUser;

  /**
   * Creates an NodeViewController object.
   *
   * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
   *   The entity manager.
   * @param \Drupal\Core\Render\RendererInterface $renderer
   *   The renderer service.
   * @param \Drupal\Core\Session\AccountInterface $current_user
   *   The current user. For backwards compatibility this is optional, however
   *   this will be removed before Drupal 9.0.0.
   */
  public function __construct(EntityManagerInterface $entity_manager, RendererInterface $renderer, AccountInterface $current_user = NULL) {
    parent::__construct($entity_manager, $renderer);
    $this->currentUser = $current_user ?: \Drupal::currentUser();
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static(
      $container->get('entity.manager'),
      $container->get('renderer'),
      $container->get('current_user')
    );
  }

51 52 53 54
  /**
   * {@inheritdoc}
   */
  public function view(EntityInterface $node, $view_mode = 'full', $langcode = NULL) {
55
    $build = parent::view($node, $view_mode, $langcode);
56 57

    foreach ($node->uriRelationships() as $rel) {
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
      $url = $node->toUrl($rel);
      // Add link relationships if the user is authenticated or if the anonymous
      // user has access. Access checking must be done for anonymous users to
      // avoid traffic to inaccessible pages from web crawlers. For
      // authenticated users, showing the links in HTML head does not impact
      // user experience or security, since the routes are access checked when
      // visited and only visible via view source. This prevents doing
      // potentially expensive and hard to cache access checks on every request.
      // This means that the page will vary by user.permissions. We also rely on
      // the access checking fallback to ensure the correct cacheability
      // metadata if we have to check access.
      if ($this->currentUser->isAuthenticated() || $url->access($this->currentUser)) {
        // Set the node path as the canonical URL to prevent duplicate content.
        $build['#attached']['html_head_link'][] = array(
          array(
            'rel' => $rel,
            'href' => $url->toString(),
          ),
          TRUE,
        );
      }
79 80 81

      if ($rel == 'canonical') {
        // Set the non-aliased canonical path as a default shortlink.
82
        $build['#attached']['html_head_link'][] = array(
83 84
          array(
            'rel' => 'shortlink',
85
            'href' => $url->setOption('alias', TRUE)->toString(),
86 87 88
          ),
          TRUE,
        );
89 90 91
      }
    }

92 93 94 95
    // Given this varies by $this->currentUser->isAuthenticated(), add a cache
    // context based on the anonymous role.
    $build['#cache']['contexts'][] = 'user.roles:anonymous';

96 97 98 99 100 101 102 103 104 105 106 107 108
    return $build;
  }

  /**
   * The _title_callback for the page that renders a single node.
   *
   * @param \Drupal\Core\Entity\EntityInterface $node
   *   The current node.
   *
   * @return string
   *   The page title.
   */
  public function title(EntityInterface $node) {
109
    return $this->entityManager->getTranslationFromContext($node)->label();
110 111 112
  }

}