comparison core/modules/node/src/Controller/NodeViewController.php @ 18:af1871eacc83

Update to Drupal core 8.7.1
author Chris Cannam
date Thu, 09 May 2019 15:33:08 +0100
parents 4c8ae668cc8c
children
comparison
equal deleted inserted replaced
17:129ea1e6d783 18:af1871eacc83
2 2
3 namespace Drupal\node\Controller; 3 namespace Drupal\node\Controller;
4 4
5 use Drupal\Core\Entity\EntityInterface; 5 use Drupal\Core\Entity\EntityInterface;
6 use Drupal\Core\Entity\Controller\EntityViewController; 6 use Drupal\Core\Entity\Controller\EntityViewController;
7 use Drupal\Core\Entity\EntityManagerInterface; 7 use Drupal\Core\Entity\EntityRepositoryInterface;
8 use Drupal\Core\Entity\EntityTypeManagerInterface;
8 use Drupal\Core\Render\RendererInterface; 9 use Drupal\Core\Render\RendererInterface;
9 use Drupal\Core\Session\AccountInterface; 10 use Drupal\Core\Session\AccountInterface;
10 use Symfony\Component\DependencyInjection\ContainerInterface; 11 use Symfony\Component\DependencyInjection\ContainerInterface;
11 12
12 /** 13 /**
20 * @var \Drupal\Core\Session\AccountInterface 21 * @var \Drupal\Core\Session\AccountInterface
21 */ 22 */
22 protected $currentUser; 23 protected $currentUser;
23 24
24 /** 25 /**
26 * The entity repository service.
27 *
28 * @var \Drupal\Core\Entity\EntityRepositoryInterface
29 */
30 protected $entityRepository;
31
32 /**
25 * Creates an NodeViewController object. 33 * Creates an NodeViewController object.
26 * 34 *
27 * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager 35 * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
28 * The entity manager. 36 * The entity type manager.
29 * @param \Drupal\Core\Render\RendererInterface $renderer 37 * @param \Drupal\Core\Render\RendererInterface $renderer
30 * The renderer service. 38 * The renderer service.
31 * @param \Drupal\Core\Session\AccountInterface $current_user 39 * @param \Drupal\Core\Session\AccountInterface $current_user
32 * The current user. For backwards compatibility this is optional, however 40 * The current user. For backwards compatibility this is optional, however
33 * this will be removed before Drupal 9.0.0. 41 * this will be removed before Drupal 9.0.0.
42 * @param \Drupal\Core\Entity\EntityRepositoryInterface $entity_repository
43 * The entity repository.
34 */ 44 */
35 public function __construct(EntityManagerInterface $entity_manager, RendererInterface $renderer, AccountInterface $current_user = NULL) { 45 public function __construct(EntityTypeManagerInterface $entity_type_manager, RendererInterface $renderer, AccountInterface $current_user = NULL, EntityRepositoryInterface $entity_repository = NULL) {
36 parent::__construct($entity_manager, $renderer); 46 parent::__construct($entity_type_manager, $renderer);
37 $this->currentUser = $current_user ?: \Drupal::currentUser(); 47 $this->currentUser = $current_user ?: \Drupal::currentUser();
48 if (!$entity_repository) {
49 @trigger_error('The entity.repository service must be passed to NodeViewController::__construct(), it is required before Drupal 9.0.0. See https://www.drupal.org/node/2549139.', E_USER_DEPRECATED);
50 $entity_repository = \Drupal::service('entity.repository');
51 }
52 $this->entityRepository = $entity_repository;
38 } 53 }
39 54
40 /** 55 /**
41 * {@inheritdoc} 56 * {@inheritdoc}
42 */ 57 */
43 public static function create(ContainerInterface $container) { 58 public static function create(ContainerInterface $container) {
44 return new static( 59 return new static(
45 $container->get('entity.manager'), 60 $container->get('entity_type.manager'),
46 $container->get('renderer'), 61 $container->get('renderer'),
47 $container->get('current_user') 62 $container->get('current_user'),
63 $container->get('entity.repository')
48 ); 64 );
49 } 65 }
50 66
51 /** 67 /**
52 * {@inheritdoc} 68 * {@inheritdoc}
53 */ 69 */
54 public function view(EntityInterface $node, $view_mode = 'full', $langcode = NULL) { 70 public function view(EntityInterface $node, $view_mode = 'full', $langcode = NULL) {
55 $build = parent::view($node, $view_mode, $langcode); 71 $build = parent::view($node, $view_mode, $langcode);
56 72
57 foreach ($node->uriRelationships() as $rel) { 73 foreach ($node->uriRelationships() as $rel) {
58 $url = $node->toUrl($rel); 74 $url = $node->toUrl($rel)->setAbsolute(TRUE);
59 // Add link relationships if the user is authenticated or if the anonymous 75 // 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 76 // user has access. Access checking must be done for anonymous users to
61 // avoid traffic to inaccessible pages from web crawlers. For 77 // avoid traffic to inaccessible pages from web crawlers. For
62 // authenticated users, showing the links in HTML head does not impact 78 // authenticated users, showing the links in HTML head does not impact
63 // user experience or security, since the routes are access checked when 79 // user experience or security, since the routes are access checked when
87 TRUE, 103 TRUE,
88 ]; 104 ];
89 } 105 }
90 } 106 }
91 107
108 // Since this generates absolute URLs, it can only be cached "per site".
109 $build['#cache']['contexts'][] = 'url.site';
110
92 // Given this varies by $this->currentUser->isAuthenticated(), add a cache 111 // Given this varies by $this->currentUser->isAuthenticated(), add a cache
93 // context based on the anonymous role. 112 // context based on the anonymous role.
94 $build['#cache']['contexts'][] = 'user.roles:anonymous'; 113 $build['#cache']['contexts'][] = 'user.roles:anonymous';
95 114
96 return $build; 115 return $build;
104 * 123 *
105 * @return string 124 * @return string
106 * The page title. 125 * The page title.
107 */ 126 */
108 public function title(EntityInterface $node) { 127 public function title(EntityInterface $node) {
109 return $this->entityManager->getTranslationFromContext($node)->label(); 128 return $this->entityRepository->getTranslationFromContext($node)->label();
110 } 129 }
111 130
112 } 131 }