Mercurial > hg > isophonics-drupal-site
comparison core/modules/node/src/Controller/NodeViewController.php @ 0:4c8ae668cc8c
Initial import (non-working)
author | Chris Cannam |
---|---|
date | Wed, 29 Nov 2017 16:09:58 +0000 |
parents | |
children | af1871eacc83 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:4c8ae668cc8c |
---|---|
1 <?php | |
2 | |
3 namespace Drupal\node\Controller; | |
4 | |
5 use Drupal\Core\Entity\EntityInterface; | |
6 use Drupal\Core\Entity\Controller\EntityViewController; | |
7 use Drupal\Core\Entity\EntityManagerInterface; | |
8 use Drupal\Core\Render\RendererInterface; | |
9 use Drupal\Core\Session\AccountInterface; | |
10 use Symfony\Component\DependencyInjection\ContainerInterface; | |
11 | |
12 /** | |
13 * Defines a controller to render a single node. | |
14 */ | |
15 class NodeViewController extends EntityViewController { | |
16 | |
17 /** | |
18 * The current user. | |
19 * | |
20 * @var \Drupal\Core\Session\AccountInterface | |
21 */ | |
22 protected $currentUser; | |
23 | |
24 /** | |
25 * Creates an NodeViewController object. | |
26 * | |
27 * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager | |
28 * The entity manager. | |
29 * @param \Drupal\Core\Render\RendererInterface $renderer | |
30 * The renderer service. | |
31 * @param \Drupal\Core\Session\AccountInterface $current_user | |
32 * The current user. For backwards compatibility this is optional, however | |
33 * this will be removed before Drupal 9.0.0. | |
34 */ | |
35 public function __construct(EntityManagerInterface $entity_manager, RendererInterface $renderer, AccountInterface $current_user = NULL) { | |
36 parent::__construct($entity_manager, $renderer); | |
37 $this->currentUser = $current_user ?: \Drupal::currentUser(); | |
38 } | |
39 | |
40 /** | |
41 * {@inheritdoc} | |
42 */ | |
43 public static function create(ContainerInterface $container) { | |
44 return new static( | |
45 $container->get('entity.manager'), | |
46 $container->get('renderer'), | |
47 $container->get('current_user') | |
48 ); | |
49 } | |
50 | |
51 /** | |
52 * {@inheritdoc} | |
53 */ | |
54 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 $url = $node->toUrl($rel); | |
59 // Add link relationships if the user is authenticated or if the anonymous | |
60 // user has access. Access checking must be done for anonymous users to | |
61 // avoid traffic to inaccessible pages from web crawlers. For | |
62 // authenticated users, showing the links in HTML head does not impact | |
63 // user experience or security, since the routes are access checked when | |
64 // visited and only visible via view source. This prevents doing | |
65 // potentially expensive and hard to cache access checks on every request. | |
66 // This means that the page will vary by user.permissions. We also rely on | |
67 // the access checking fallback to ensure the correct cacheability | |
68 // metadata if we have to check access. | |
69 if ($this->currentUser->isAuthenticated() || $url->access($this->currentUser)) { | |
70 // Set the node path as the canonical URL to prevent duplicate content. | |
71 $build['#attached']['html_head_link'][] = [ | |
72 [ | |
73 'rel' => $rel, | |
74 'href' => $url->toString(), | |
75 ], | |
76 TRUE, | |
77 ]; | |
78 } | |
79 | |
80 if ($rel == 'canonical') { | |
81 // Set the non-aliased canonical path as a default shortlink. | |
82 $build['#attached']['html_head_link'][] = [ | |
83 [ | |
84 'rel' => 'shortlink', | |
85 'href' => $url->setOption('alias', TRUE)->toString(), | |
86 ], | |
87 TRUE, | |
88 ]; | |
89 } | |
90 } | |
91 | |
92 // Given this varies by $this->currentUser->isAuthenticated(), add a cache | |
93 // context based on the anonymous role. | |
94 $build['#cache']['contexts'][] = 'user.roles:anonymous'; | |
95 | |
96 return $build; | |
97 } | |
98 | |
99 /** | |
100 * The _title_callback for the page that renders a single node. | |
101 * | |
102 * @param \Drupal\Core\Entity\EntityInterface $node | |
103 * The current node. | |
104 * | |
105 * @return string | |
106 * The page title. | |
107 */ | |
108 public function title(EntityInterface $node) { | |
109 return $this->entityManager->getTranslationFromContext($node)->label(); | |
110 } | |
111 | |
112 } |