diff 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
line wrap: on
line diff
--- a/core/modules/node/src/Controller/NodeViewController.php	Thu Feb 28 13:21:36 2019 +0000
+++ b/core/modules/node/src/Controller/NodeViewController.php	Thu May 09 15:33:08 2019 +0100
@@ -4,7 +4,8 @@
 
 use Drupal\Core\Entity\EntityInterface;
 use Drupal\Core\Entity\Controller\EntityViewController;
-use Drupal\Core\Entity\EntityManagerInterface;
+use Drupal\Core\Entity\EntityRepositoryInterface;
+use Drupal\Core\Entity\EntityTypeManagerInterface;
 use Drupal\Core\Render\RendererInterface;
 use Drupal\Core\Session\AccountInterface;
 use Symfony\Component\DependencyInjection\ContainerInterface;
@@ -22,19 +23,33 @@
   protected $currentUser;
 
   /**
+   * The entity repository service.
+   *
+   * @var \Drupal\Core\Entity\EntityRepositoryInterface
+   */
+  protected $entityRepository;
+
+  /**
    * Creates an NodeViewController object.
    *
-   * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
-   *   The entity manager.
+   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
+   *   The entity type 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.
+   * @param \Drupal\Core\Entity\EntityRepositoryInterface $entity_repository
+   *   The entity repository.
    */
-  public function __construct(EntityManagerInterface $entity_manager, RendererInterface $renderer, AccountInterface $current_user = NULL) {
-    parent::__construct($entity_manager, $renderer);
+  public function __construct(EntityTypeManagerInterface $entity_type_manager, RendererInterface $renderer, AccountInterface $current_user = NULL, EntityRepositoryInterface $entity_repository = NULL) {
+    parent::__construct($entity_type_manager, $renderer);
     $this->currentUser = $current_user ?: \Drupal::currentUser();
+    if (!$entity_repository) {
+      @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);
+      $entity_repository = \Drupal::service('entity.repository');
+    }
+    $this->entityRepository = $entity_repository;
   }
 
   /**
@@ -42,9 +57,10 @@
    */
   public static function create(ContainerInterface $container) {
     return new static(
-      $container->get('entity.manager'),
+      $container->get('entity_type.manager'),
       $container->get('renderer'),
-      $container->get('current_user')
+      $container->get('current_user'),
+      $container->get('entity.repository')
     );
   }
 
@@ -55,7 +71,7 @@
     $build = parent::view($node, $view_mode, $langcode);
 
     foreach ($node->uriRelationships() as $rel) {
-      $url = $node->toUrl($rel);
+      $url = $node->toUrl($rel)->setAbsolute(TRUE);
       // 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
@@ -89,6 +105,9 @@
       }
     }
 
+    // Since this generates absolute URLs, it can only be cached "per site".
+    $build['#cache']['contexts'][] = 'url.site';
+
     // Given this varies by $this->currentUser->isAuthenticated(), add a cache
     // context based on the anonymous role.
     $build['#cache']['contexts'][] = 'user.roles:anonymous';
@@ -106,7 +125,7 @@
    *   The page title.
    */
   public function title(EntityInterface $node) {
-    return $this->entityManager->getTranslationFromContext($node)->label();
+    return $this->entityRepository->getTranslationFromContext($node)->label();
   }
 
 }