If you render a comment you need the loaded node.
For just listing the comment of the node this is not really a problem but you might have a view which loads a lot of comments
from totally different nodes.

Comments

dawehner’s picture

Status: Active » Needs review
StatusFileSize
new2.46 KB
berdir’s picture

Hm...

- This conflicts with #1778178: Convert comments to the new Entity Field API, you're not supposed to add arbitrary stuff to $comments anmore
- Note that it only helps when actually calling comment_view_multiple(), which views is currently not.
- I thin kthe comment render controller would be a better place for this.

That said, we don't actually *have* to attach it. It's enough if we would just call node_load_multiple() to prepare the entity cache, then it will be loaded from the cache anyway. That could even happen within the view class and without having to use comment_view_multiple()...

dawehner’s picture

Status: Needs review » Needs work

I agree with you, the render controller should do that, as it also has the information which the nodes have.
It looked kind of ugly to actually load the nodes, and then node_load again later but doing it on the render controller helps.

- Note that it only helps when actually calling comment_view_multiple(), which views is currently not.

We fixed that in http://drupalcode.org/project/drupal.git/blob/refs/heads/8.x:/core/modul...
and http://drupalcode.org/project/drupal.git/blob/refs/heads/8.x:/core/modul...

dawehner’s picture

Status: Needs work » Needs review
StatusFileSize
new918 bytes

So here is a patch which does the loading on the comment render controller.

berdir’s picture

+++ b/core/modules/comment/lib/Drupal/comment/CommentRenderController.phpundefined
@@ -29,8 +29,15 @@ public function buildContent(array $entities = array(), $view_mode = 'full', $la
+    $nodes =node_load_multiple($nids);

Missing space :)

berdir’s picture

Status: Needs review » Needs work
dawehner’s picture

Status: Needs work » Needs review
StatusFileSize
new933 bytes

Wow, someone reviews patches! Thank you!

Status: Needs review » Needs work

The last submitted patch, drupal-1827582-7.patch, failed testing.

berdir’s picture

Status: Needs work » Needs review

#7: drupal-1827582-7.patch queued for re-testing.

berdir’s picture

+++ b/core/modules/comment/lib/Drupal/comment/CommentRenderController.phpundefined
@@ -32,8 +32,15 @@ public function buildContent(array $entities = array(), $view_mode = 'full', $la
+    // Collect all nodes and load them at once.
+    $nids = array();
     foreach ($entities as $entity) {
-      $node = node_load($entity->nid);
+      $nids[$entity->nid] = $entity->nid;
+    }
+    $nodes = node_load_multiple(array_unique($nids));

Sorry, another one.

The array_unique() is not necessary, you already use the nid as key.

Not sure if we can do better with the comment here, but I don't know how, so I guess it's fine.

dawehner’s picture

StatusFileSize
new918 bytes

Ups, for a short moment I thought this would be a good idea to use array_unique, what about the new comment?

berdir’s picture

+++ b/core/modules/comment/lib/Drupal/comment/CommentRenderController.phpundefined
@@ -32,8 +32,15 @@ public function buildContent(array $entities = array(), $view_mode = 'full', $la
+    foreach ($entities as $entity) {
+      $node = $nodes[$entity->nid];
       if (!$node) {
         throw new \InvalidArgumentException(t('Invalid node for comment.'));

Hm. If $node doesn't exist, then this results in a php notice. We should move the if (!$node) up and do a if (!isset($nodes[$entity->nid]) instead.

berdir’s picture

Status: Needs review » Needs work
dawehner’s picture

Status: Needs work » Needs review
StatusFileSize
new1 KB
new801 bytes

Good point!

berdir’s picture

Category: feature » task
Status: Needs review » Reviewed & tested by the community

Looks good now :)

I would also argue that this is a (performance-improving) task, not a feature, it shouldn't be blocked because we're currently over thresholds.

webchick’s picture

Status: Reviewed & tested by the community » Fixed

Agreed.

Committed and pushed to 8.x. Thanks!

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.