comparison 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
comparison
equal deleted inserted replaced
4:a9cd425dd02b 5:12f9dff5fda9
6 use Drupal\comment\CommentManagerInterface; 6 use Drupal\comment\CommentManagerInterface;
7 use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface; 7 use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface;
8 use Drupal\Core\Access\AccessResult; 8 use Drupal\Core\Access\AccessResult;
9 use Drupal\Core\Cache\CacheableResponseInterface; 9 use Drupal\Core\Cache\CacheableResponseInterface;
10 use Drupal\Core\Controller\ControllerBase; 10 use Drupal\Core\Controller\ControllerBase;
11 use Drupal\Core\Entity\EntityFieldManagerInterface;
11 use Drupal\Core\Entity\EntityInterface; 12 use Drupal\Core\Entity\EntityInterface;
12 use Drupal\Core\Entity\EntityManagerInterface; 13 use Drupal\Core\Entity\EntityRepositoryInterface;
14 use Drupal\Core\Entity\EntityTypeManagerInterface;
13 use Symfony\Component\DependencyInjection\ContainerInterface; 15 use Symfony\Component\DependencyInjection\ContainerInterface;
14 use Symfony\Component\HttpFoundation\JsonResponse; 16 use Symfony\Component\HttpFoundation\JsonResponse;
15 use Symfony\Component\HttpFoundation\RedirectResponse; 17 use Symfony\Component\HttpFoundation\RedirectResponse;
16 use Symfony\Component\HttpFoundation\Request; 18 use Symfony\Component\HttpFoundation\Request;
17 use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException; 19 use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
38 * @var \Drupal\comment\CommentManagerInterface 40 * @var \Drupal\comment\CommentManagerInterface
39 */ 41 */
40 protected $commentManager; 42 protected $commentManager;
41 43
42 /** 44 /**
43 * The entity manager. 45 * The entity field manager.
44 * 46 *
45 * @var \Drupal\Core\Entity\EntityStorageInterface 47 * @var \Drupal\Core\Entity\EntityFieldManagerInterface
46 */ 48 */
47 protected $entityManager; 49 protected $entityFieldManager;
50
51 /**
52 * The entity repository.
53 *
54 * @var Drupal\Core\Entity\EntityRepositoryInterface
55 */
56 protected $entityRepository;
48 57
49 /** 58 /**
50 * Constructs a CommentController object. 59 * Constructs a CommentController object.
51 * 60 *
52 * @param \Symfony\Component\HttpKernel\HttpKernelInterface $http_kernel 61 * @param \Symfony\Component\HttpKernel\HttpKernelInterface $http_kernel
53 * HTTP kernel to handle requests. 62 * HTTP kernel to handle requests.
54 * @param \Drupal\comment\CommentManagerInterface $comment_manager 63 * @param \Drupal\comment\CommentManagerInterface $comment_manager
55 * The comment manager service. 64 * The comment manager service.
56 * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager 65 * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
57 * The entity manager service. 66 * The entity type manager service.
58 */ 67 * @param \Drupal\Core\Entity\EntityFieldManagerInterface $entity_field_manager
59 public function __construct(HttpKernelInterface $http_kernel, CommentManagerInterface $comment_manager, EntityManagerInterface $entity_manager) { 68 * The entity field manager service.
69 * @param \Drupal\Core\Entity\EntityRepositoryInterface $entity_repository
70 * The entity repository service.
71 */
72 public function __construct(HttpKernelInterface $http_kernel, CommentManagerInterface $comment_manager, EntityTypeManagerInterface $entity_type_manager, EntityFieldManagerInterface $entity_field_manager = NULL, EntityRepositoryInterface $entity_repository = NULL) {
60 $this->httpKernel = $http_kernel; 73 $this->httpKernel = $http_kernel;
61 $this->commentManager = $comment_manager; 74 $this->commentManager = $comment_manager;
62 $this->entityManager = $entity_manager; 75 $this->entityTypeManager = $entity_type_manager;
76 if (!$entity_field_manager) {
77 @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);
78 $entity_field_manager = \Drupal::service('entity_field.manager');
79 }
80 $this->entityFieldManager = $entity_field_manager;
81 if (!$entity_repository) {
82 @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);
83 $entity_repository = \Drupal::service('entity.repository');
84 }
85 $this->entityRepository = $entity_repository;
63 } 86 }
64 87
65 /** 88 /**
66 * {@inheritdoc} 89 * {@inheritdoc}
67 */ 90 */
68 public static function create(ContainerInterface $container) { 91 public static function create(ContainerInterface $container) {
69 return new static( 92 return new static(
70 $container->get('http_kernel'), 93 $container->get('http_kernel'),
71 $container->get('comment.manager'), 94 $container->get('comment.manager'),
72 $container->get('entity.manager') 95 $container->get('entity_type.manager'),
96 $container->get('entity_field.manager'),
97 $container->get('entity.repository')
73 ); 98 );
74 } 99 }
75 100
76 /** 101 /**
77 * Publishes the specified comment. 102 * Publishes the specified comment.
117 if ($entity = $comment->getCommentedEntity()) { 142 if ($entity = $comment->getCommentedEntity()) {
118 // Check access permissions for the entity. 143 // Check access permissions for the entity.
119 if (!$entity->access('view')) { 144 if (!$entity->access('view')) {
120 throw new AccessDeniedHttpException(); 145 throw new AccessDeniedHttpException();
121 } 146 }
122 $field_definition = $this->entityManager()->getFieldDefinitions($entity->getEntityTypeId(), $entity->bundle())[$comment->getFieldName()]; 147 $field_definition = $this->entityFieldManager->getFieldDefinitions($entity->getEntityTypeId(), $entity->bundle())[$comment->getFieldName()];
123 148
124 // Find the current display page for this comment. 149 // Find the current display page for this comment.
125 $page = $this->entityManager()->getStorage('comment')->getDisplayOrdinal($comment, $field_definition->getSetting('default_mode'), $field_definition->getSetting('per_page')); 150 $page = $this->entityTypeManager()->getStorage('comment')->getDisplayOrdinal($comment, $field_definition->getSetting('default_mode'), $field_definition->getSetting('per_page'));
126 // @todo: Cleaner sub request handling. 151 // @todo: Cleaner sub request handling.
127 $subrequest_url = $entity->urlInfo()->setOption('query', ['page' => $page])->toString(TRUE); 152 $subrequest_url = $entity->toUrl()->setOption('query', ['page' => $page])->toString(TRUE);
128 $redirect_request = Request::create($subrequest_url->getGeneratedUrl(), 'GET', $request->query->all(), $request->cookies->all(), [], $request->server->all()); 153 $redirect_request = Request::create($subrequest_url->getGeneratedUrl(), 'GET', $request->query->all(), $request->cookies->all(), [], $request->server->all());
129 // Carry over the session to the subrequest. 154 // Carry over the session to the subrequest.
130 if ($session = $request->getSession()) { 155 if ($request->hasSession()) {
131 $redirect_request->setSession($session); 156 $redirect_request->setSession($request->getSession());
132 } 157 }
133 $request->query->set('page', $page); 158 $request->query->set('page', $page);
134 $response = $this->httpKernel->handle($redirect_request, HttpKernelInterface::SUB_REQUEST); 159 $response = $this->httpKernel->handle($redirect_request, HttpKernelInterface::SUB_REQUEST);
135 if ($response instanceof CacheableResponseInterface) { 160 if ($response instanceof CacheableResponseInterface) {
136 // @todo Once path aliases have cache tags (see 161 // @todo Once path aliases have cache tags (see
153 * 178 *
154 * @return string 179 * @return string
155 * The translated comment subject. 180 * The translated comment subject.
156 */ 181 */
157 public function commentPermalinkTitle(CommentInterface $comment) { 182 public function commentPermalinkTitle(CommentInterface $comment) {
158 return $this->entityManager()->getTranslationFromContext($comment)->label(); 183 return $this->entityRepository->getTranslationFromContext($comment)->label();
159 } 184 }
160 185
161 /** 186 /**
162 * Redirects legacy node links to the new path. 187 * Redirects legacy node links to the new path.
163 * 188 *
217 if ($request->request->get('op') != $this->t('Preview')) { 242 if ($request->request->get('op') != $this->t('Preview')) {
218 243
219 // $pid indicates that this is a reply to a comment. 244 // $pid indicates that this is a reply to a comment.
220 if ($pid) { 245 if ($pid) {
221 // Load the parent comment. 246 // Load the parent comment.
222 $comment = $this->entityManager()->getStorage('comment')->load($pid); 247 $comment = $this->entityTypeManager()->getStorage('comment')->load($pid);
223 // Display the parent comment. 248 // Display the parent comment.
224 $build['comment_parent'] = $this->entityManager()->getViewBuilder('comment')->view($comment); 249 $build['comment_parent'] = $this->entityTypeManager()->getViewBuilder('comment')->view($comment);
225 } 250 }
226 251
227 // The comment is in response to a entity. 252 // The comment is in response to a entity.
228 elseif ($entity->access('view', $account)) { 253 elseif ($entity->access('view', $account)) {
229 // We make sure the field value isn't set so we don't end up with a 254 // We make sure the field value isn't set so we don't end up with a
230 // redirect loop. 255 // redirect loop.
231 $entity = clone $entity; 256 $entity = clone $entity;
232 $entity->{$field_name}->status = CommentItemInterface::HIDDEN; 257 $entity->{$field_name}->status = CommentItemInterface::HIDDEN;
233 // Render array of the entity full view mode. 258 // Render array of the entity full view mode.
234 $build['commented_entity'] = $this->entityManager()->getViewBuilder($entity->getEntityTypeId())->view($entity, 'full'); 259 $build['commented_entity'] = $this->entityTypeManager()->getViewBuilder($entity->getEntityTypeId())->view($entity, 'full');
235 unset($build['commented_entity']['#cache']); 260 unset($build['commented_entity']['#cache']);
236 } 261 }
237 } 262 }
238 else { 263 else {
239 $build['#title'] = $this->t('Preview comment'); 264 $build['#title'] = $this->t('Preview comment');
240 } 265 }
241 266
242 // Show the actual reply box. 267 // Show the actual reply box.
243 $comment = $this->entityManager()->getStorage('comment')->create([ 268 $comment = $this->entityTypeManager()->getStorage('comment')->create([
244 'entity_id' => $entity->id(), 269 'entity_id' => $entity->id(),
245 'pid' => $pid, 270 'pid' => $pid,
246 'entity_type' => $entity->getEntityTypeId(), 271 'entity_type' => $entity->getEntityTypeId(),
247 'field_name' => $field_name, 272 'field_name' => $field_name,
248 ]); 273 ]);
290 if ($pid) { 315 if ($pid) {
291 // Check if the user has the proper permissions. 316 // Check if the user has the proper permissions.
292 $access = $access->andIf(AccessResult::allowedIfHasPermission($account, 'access comments')); 317 $access = $access->andIf(AccessResult::allowedIfHasPermission($account, 'access comments'));
293 318
294 // Load the parent comment. 319 // Load the parent comment.
295 $comment = $this->entityManager()->getStorage('comment')->load($pid); 320 $comment = $this->entityTypeManager()->getStorage('comment')->load($pid);
296 // Check if the parent comment is published and belongs to the entity. 321 // Check if the parent comment is published and belongs to the entity.
297 $access = $access->andIf(AccessResult::allowedIf($comment && $comment->isPublished() && $comment->getCommentedEntityId() == $entity->id())); 322 $access = $access->andIf(AccessResult::allowedIf($comment && $comment->isPublished() && $comment->getCommentedEntityId() == $entity->id()));
298 if ($comment) { 323 if ($comment) {
299 $access->addCacheableDependency($comment); 324 $access->addCacheableDependency($comment);
300 } 325 }
327 // Only handle up to 100 nodes. 352 // Only handle up to 100 nodes.
328 $nids = array_slice($nids, 0, 100); 353 $nids = array_slice($nids, 0, 100);
329 354
330 $links = []; 355 $links = [];
331 foreach ($nids as $nid) { 356 foreach ($nids as $nid) {
332 $node = $this->entityManager->getStorage('node')->load($nid); 357 $node = $this->entityTypeManager()->getStorage('node')->load($nid);
333 $new = $this->commentManager->getCountNewComments($node); 358 $new = $this->commentManager->getCountNewComments($node);
334 $page_number = $this->entityManager()->getStorage('comment') 359 $page_number = $this->entityTypeManager()->getStorage('comment')
335 ->getNewCommentPageNumber($node->{$field_name}->comment_count, $new, $node, $field_name); 360 ->getNewCommentPageNumber($node->{$field_name}->comment_count, $new, $node, $field_name);
336 $query = $page_number ? ['page' => $page_number] : NULL; 361 $query = $page_number ? ['page' => $page_number] : NULL;
337 $links[$nid] = [ 362 $links[$nid] = [
338 'new_comment_count' => (int) $new, 363 'new_comment_count' => (int) $new,
339 'first_new_comment_link' => $this->getUrlGenerator()->generateFromRoute('entity.node.canonical', ['node' => $node->id()], ['query' => $query, 'fragment' => 'new']), 364 'first_new_comment_link' => $this->getUrlGenerator()->generateFromRoute('entity.node.canonical', ['node' => $node->id()], ['query' => $query, 'fragment' => 'new']),