Mercurial > hg > cmmr2012-drupal-site
diff core/modules/comment/src/Controller/CommentController.php @ 5:12f9dff5fda9 tip
Update to Drupal core 8.7.1
author | Chris Cannam |
---|---|
date | Thu, 09 May 2019 15:34:47 +0100 |
parents | a9cd425dd02b |
children |
line wrap: on
line diff
--- a/core/modules/comment/src/Controller/CommentController.php Thu Feb 28 13:11:55 2019 +0000 +++ b/core/modules/comment/src/Controller/CommentController.php Thu May 09 15:34:47 2019 +0100 @@ -8,8 +8,10 @@ use Drupal\Core\Access\AccessResult; use Drupal\Core\Cache\CacheableResponseInterface; use Drupal\Core\Controller\ControllerBase; +use Drupal\Core\Entity\EntityFieldManagerInterface; use Drupal\Core\Entity\EntityInterface; -use Drupal\Core\Entity\EntityManagerInterface; +use Drupal\Core\Entity\EntityRepositoryInterface; +use Drupal\Core\Entity\EntityTypeManagerInterface; use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\RedirectResponse; @@ -40,11 +42,18 @@ protected $commentManager; /** - * The entity manager. + * The entity field manager. * - * @var \Drupal\Core\Entity\EntityStorageInterface + * @var \Drupal\Core\Entity\EntityFieldManagerInterface */ - protected $entityManager; + protected $entityFieldManager; + + /** + * The entity repository. + * + * @var Drupal\Core\Entity\EntityRepositoryInterface + */ + protected $entityRepository; /** * Constructs a CommentController object. @@ -53,13 +62,27 @@ * HTTP kernel to handle requests. * @param \Drupal\comment\CommentManagerInterface $comment_manager * The comment manager service. - * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager - * The entity manager service. + * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager + * The entity type manager service. + * @param \Drupal\Core\Entity\EntityFieldManagerInterface $entity_field_manager + * The entity field manager service. + * @param \Drupal\Core\Entity\EntityRepositoryInterface $entity_repository + * The entity repository service. */ - public function __construct(HttpKernelInterface $http_kernel, CommentManagerInterface $comment_manager, EntityManagerInterface $entity_manager) { + public function __construct(HttpKernelInterface $http_kernel, CommentManagerInterface $comment_manager, EntityTypeManagerInterface $entity_type_manager, EntityFieldManagerInterface $entity_field_manager = NULL, EntityRepositoryInterface $entity_repository = NULL) { $this->httpKernel = $http_kernel; $this->commentManager = $comment_manager; - $this->entityManager = $entity_manager; + $this->entityTypeManager = $entity_type_manager; + if (!$entity_field_manager) { + @trigger_error('The entity_field.manager service must be passed to CommentController::__construct(), it is required before Drupal 9.0.0. See https://www.drupal.org/node/2549139.', E_USER_DEPRECATED); + $entity_field_manager = \Drupal::service('entity_field.manager'); + } + $this->entityFieldManager = $entity_field_manager; + if (!$entity_repository) { + @trigger_error('The entity.repository service must be passed to CommentController::__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; } /** @@ -69,7 +92,9 @@ return new static( $container->get('http_kernel'), $container->get('comment.manager'), - $container->get('entity.manager') + $container->get('entity_type.manager'), + $container->get('entity_field.manager'), + $container->get('entity.repository') ); } @@ -119,16 +144,16 @@ if (!$entity->access('view')) { throw new AccessDeniedHttpException(); } - $field_definition = $this->entityManager()->getFieldDefinitions($entity->getEntityTypeId(), $entity->bundle())[$comment->getFieldName()]; + $field_definition = $this->entityFieldManager->getFieldDefinitions($entity->getEntityTypeId(), $entity->bundle())[$comment->getFieldName()]; // Find the current display page for this comment. - $page = $this->entityManager()->getStorage('comment')->getDisplayOrdinal($comment, $field_definition->getSetting('default_mode'), $field_definition->getSetting('per_page')); + $page = $this->entityTypeManager()->getStorage('comment')->getDisplayOrdinal($comment, $field_definition->getSetting('default_mode'), $field_definition->getSetting('per_page')); // @todo: Cleaner sub request handling. - $subrequest_url = $entity->urlInfo()->setOption('query', ['page' => $page])->toString(TRUE); + $subrequest_url = $entity->toUrl()->setOption('query', ['page' => $page])->toString(TRUE); $redirect_request = Request::create($subrequest_url->getGeneratedUrl(), 'GET', $request->query->all(), $request->cookies->all(), [], $request->server->all()); // Carry over the session to the subrequest. - if ($session = $request->getSession()) { - $redirect_request->setSession($session); + if ($request->hasSession()) { + $redirect_request->setSession($request->getSession()); } $request->query->set('page', $page); $response = $this->httpKernel->handle($redirect_request, HttpKernelInterface::SUB_REQUEST); @@ -155,7 +180,7 @@ * The translated comment subject. */ public function commentPermalinkTitle(CommentInterface $comment) { - return $this->entityManager()->getTranslationFromContext($comment)->label(); + return $this->entityRepository->getTranslationFromContext($comment)->label(); } /** @@ -219,9 +244,9 @@ // $pid indicates that this is a reply to a comment. if ($pid) { // Load the parent comment. - $comment = $this->entityManager()->getStorage('comment')->load($pid); + $comment = $this->entityTypeManager()->getStorage('comment')->load($pid); // Display the parent comment. - $build['comment_parent'] = $this->entityManager()->getViewBuilder('comment')->view($comment); + $build['comment_parent'] = $this->entityTypeManager()->getViewBuilder('comment')->view($comment); } // The comment is in response to a entity. @@ -231,7 +256,7 @@ $entity = clone $entity; $entity->{$field_name}->status = CommentItemInterface::HIDDEN; // Render array of the entity full view mode. - $build['commented_entity'] = $this->entityManager()->getViewBuilder($entity->getEntityTypeId())->view($entity, 'full'); + $build['commented_entity'] = $this->entityTypeManager()->getViewBuilder($entity->getEntityTypeId())->view($entity, 'full'); unset($build['commented_entity']['#cache']); } } @@ -240,7 +265,7 @@ } // Show the actual reply box. - $comment = $this->entityManager()->getStorage('comment')->create([ + $comment = $this->entityTypeManager()->getStorage('comment')->create([ 'entity_id' => $entity->id(), 'pid' => $pid, 'entity_type' => $entity->getEntityTypeId(), @@ -292,7 +317,7 @@ $access = $access->andIf(AccessResult::allowedIfHasPermission($account, 'access comments')); // Load the parent comment. - $comment = $this->entityManager()->getStorage('comment')->load($pid); + $comment = $this->entityTypeManager()->getStorage('comment')->load($pid); // Check if the parent comment is published and belongs to the entity. $access = $access->andIf(AccessResult::allowedIf($comment && $comment->isPublished() && $comment->getCommentedEntityId() == $entity->id())); if ($comment) { @@ -329,9 +354,9 @@ $links = []; foreach ($nids as $nid) { - $node = $this->entityManager->getStorage('node')->load($nid); + $node = $this->entityTypeManager()->getStorage('node')->load($nid); $new = $this->commentManager->getCountNewComments($node); - $page_number = $this->entityManager()->getStorage('comment') + $page_number = $this->entityTypeManager()->getStorage('comment') ->getNewCommentPageNumber($node->{$field_name}->comment_count, $new, $node, $field_name); $query = $page_number ? ['page' => $page_number] : NULL; $links[$nid] = [