1

I want to send notifications to users that have posted in a comment thread. I need to get the user IDs in an event subscriber that will be triggered when comments are created/edited, so performance might of importance.

After some investigation, I realized there is the loadThread() method in the CommentStorage class, and I came up with the following implementation.

$original_comment = $event->getEntity();
if ($original_comment->hasParentComment()) {
  $commented_entity = $original_comment->getCommentedEntity();
  $comment_field = $original_comment->getFieldName();
  $mode = $commented_entity->getFieldDefinition($comment_field)->getSetting('default_mode');
  $thread = $this->commentStorage->loadThread($commented_entity, $comment_field, $mode);
  $recipients = [];
  foreach ($thread as $comment) {
    // I keep them in a keyed array so that i won't need to reiterate for unique values.
    $recipients[$comment->getOwner()->id()] = $comment->getOwner()->id();
  }
}

As far as I can see, there is no way to avoid loading all the comments in the thread, but just get the comment IDs. At least, this isn't provided by the storage and manager classes implemented by the Comment module.

Is there any way to get the list of all users who posted a comment in a comment thread without loading all the comments?

Edit 2: Following up on Jaypan's comment, there is indeed no notable performance hit, since, as he explained, Drupal entities are cached upon generation and thus, loading them is not the end of the world.

6
  • Could you expand on what you mean by "clean"? For example can you edit details of your current unclean implementation into the question?
    – Clive
    Feb 25, 2021 at 14:15
  • Yes of cource, I am still working on it, i will edit it in a bit, when I have a complete draft. Feb 25, 2021 at 14:27
  • 3
    This will be performant. Drupal caches the entities after generating them (comments are entities), so as long as it is current content on your site that has already been viewed/cached, the above code won't be particularly harmful. You can do some benchmarking to confirm - clear your registry, load a bunch of comments, and time it. Then reload those comments, and re-time it.
    – Jaypan
    Feb 25, 2021 at 17:42
  • Thanks, indeed the second measure came up faster. Are there any good articles that you now of, that go in depth in explaining how drupal cache works? Also a second consideration (more generic and a bit irrelevant to the present question) is if the line $recipients[$comment->getOwner()->id()] = $comment->getOwner()->id(); would be more "performant" if the owner id was stored into a variable, instead of being fetched twice. Feb 26, 2021 at 10:13
  • This also expands to other things as well like storing the storage if you are going to reuse it multiple times. Does $comment_storage = $this->commentStorage; and then calling $comment_storage instead of $this->commentStorage; 7-8 times in the same function, make a difference performance wise? Feb 26, 2021 at 10:13

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.