Updated: Comment #0

Problem/Motivation

The function used only in theme layer and could be removed in #1938062: Convert the recent_comments block to a view
Also theme function should be changed in twig conversion in #1987396: Refactor & Clean-up comment.module theme functions
Ordering needs fix #1059608: Fix comment_get_recent ordering

Proposed resolution

Move query to current implementation of block to allow contrib inherit the block and change the query.

Remaining tasks

Approve API cleanup

User interface changes

No

API changes

Change theme function arguments
Remove function

#1938062: Convert the recent_comments block to a view
#1987396: Refactor & Clean-up comment.module theme functions
#1059608: Fix comment_get_recent ordering

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

andypost’s picture

Status: Active » Needs review
FileSize
7.36 KB
4.7 KB

No visial changes
rc.png

tim.plunkett’s picture

I think it could be a method on a CommentManager service, honestly. But, if we continue down this path:

+++ b/core/modules/comment/lib/Drupal/comment/Plugin/Block/RecentCommentsBlock.phpundefined
@@ -62,9 +99,27 @@ public function blockSubmit($form, &$form_state) {
+      '#entities' => entity_load_multiple('comment', $comments),

Inject the storage controller, please

andypost’s picture

I think it useless to have this function so better encapsulate in block where it used only

andypost’s picture

Proper patch and interdiff

larowlan’s picture

Not fussy either way
On the manager would make it more useful to contrib/easier to reuse.
For forum I've tried to get all of the SQL queries (other than hooks) on the manager to make it easier for other backends to override in one place so that would fit here too.
But either way is better than what we have now.

andypost’s picture

Title: Remove comment_get_recent() in favour of RecentCommentsBlock » Remove comment_get_recent() in favour of reusable RecentCommentsBlock

I suppose that it much better UX and DX could be found with reusable block because it's much easy to inherit block then manager

tim.plunkett’s picture

If I wanted to get 10 comment object for the most recent, what does that have to do with a block?

BlockManager::getRecentComments(AccountInterface $account);

andypost’s picture

@tim.plunkett suppose once you need to query recent comments then use can use views or copy/paste code from block or just inherit block into own module and change query for your needs.
But in case this function placed into commentManager service you could only to swap service but this approach is limited because 2 modules can not swap service same time.
For example site uses contrib that replaces manager service with own implementation and developer needs new block of recent comments but for products only (the most often task) in this case only view or custom query could be used!

That's why I think that easy to copy/paste block code then service

tim.plunkett’s picture

I think its easier to call a method than copy paste the contents of a class, but I'll probably never need this, I don't care.
But I still think it has nothing to do with blocks.

dawehner’s picture

+++ b/core/modules/comment/lib/Drupal/comment/Plugin/Block/RecentCommentsBlock.phpundefined
@@ -62,9 +111,27 @@ public function blockSubmit($form, &$form_state) {
+    $query = $this->database->select('comment', 'c');
+    $query->innerJoin('node_field_data', 'n', 'n.nid = c.nid');
+    $query->addTag('node_access');
+    $query->addMetaData('base_table', 'comment');
+    $comments = $query
+      ->fields('c', array('cid'))
+      ->condition('c.status', COMMENT_PUBLISHED)
+      ->condition('n.status', NODE_PUBLISHED)
+      // @todo This should be actually filtering on the desired node status field
+      //   language and just fall back to the default language.
+      ->condition('n.default_langcode', 1)
+      // Additionally order by cid to ensure that comments returned in the exact
+      // order posted.
+      ->orderBy('c.cid', 'DESC')
+      ->range(0, $this->configuration['block_count'])
+      ->execute()
+      ->fetchAllKeyed(0, 0);

Is there a reason to not use EQ or just skip all that and just do the views bit?

dawehner’s picture

I interpretate #1938062-82: Convert the recent_comments block to a view as a won't fix for this issue.

xjm’s picture

@dawehner, I don't think my comment has anything to do with this issue?

It makes sense to me to not remove that function in that issue, and discuss it specifically here instead, as to whether we think it's a worthwhile procedural helper or not. I agree that the function is generic and needn't have anything to do with the block.

My two cents is that this is not serious enough to merit a BC break after API freeze. Instead, I think we should:

  • Stop using comment_get_recent() inside OO code. This dovetails nicely with #1938062: Convert the recent_comments block to a view, but neither issue is dependent on the other.
  • Gut the current comment_get_recent() and replace its innards with an EFQ.
  • Decide whether or not to mark comment_get_recent(), which is then merely a BC procedural wrapper, as @deprecated.
xjm’s picture

Title: Remove comment_get_recent() in favour of reusable RecentCommentsBlock » Refactor and possibly deprecate comment_get_recent()
xjm’s picture

tstoeckler’s picture

Status: Needs review » Needs work

This needs to additionally oder by comment.updated and only if they are the same by comment.cid. Otherwise editing comments will not make them jump to the top of the list.

andypost’s picture

Status: Needs work » Needs review
FileSize
5.19 KB

@xjm Thanx for point!

Patch introduces CommentManager with getRecent($count) method and deprecates comment_get_recent() in favour of it.
Uses EQ but I leave 'node_access' tag here as it was

comment.updated

changed could be addressed in #1059608: Fix comment_get_recent ordering but I use it here too

dawehner’s picture

Shameless advertisement for a different issue: #1938062: Convert the recent_comments block to a view

  1. +++ b/core/modules/comment/comment.module
    @@ -390,28 +390,11 @@ function comment_permission() {
     /**
    @@ -491,7 +474,7 @@ function theme_comment_block($variables) {
    
    @@ -491,7 +474,7 @@ function theme_comment_block($variables) {
       $items = array();
       $number = $variables['number'];
       foreach (comment_get_recent($number) as $comment) {
    -    $items[] = l($comment->subject, 'comment/' . $comment->cid, array('fragment' => 'comment-' . $comment->cid)) . '&nbsp;<span>' . t('@time ago', array('@time' => format_interval(REQUEST_TIME - $comment->changed))) . '</span>';
    +    $items[] = l($comment->subject->value, 'comment/' . $comment->id(), array('fragment' => 'comment-' . $comment->id())) . '&nbsp;<span>' . t('@time ago', array('@time' => format_interval(REQUEST_TIME - $comment->changed->value))) . '</span>';
       }
     
    

    This feels kind of out of scope ? I would rather suggest to use the service directly

  2. +++ b/core/modules/comment/lib/Drupal/comment/CommentManager.php
    @@ -0,0 +1,64 @@
    +    $query = $this->queryFactory->get('comment');
    ...
    +      ->addTag('node_access')
    

    Wait, we do add node_access on a comment entity query?

  3. +++ b/core/modules/comment/lib/Drupal/comment/CommentManager.php
    @@ -0,0 +1,64 @@
    +    $ids =$query
    

    Missing space before $query

  4. +++ b/core/modules/comment/lib/Drupal/comment/CommentManagerInterface.php
    @@ -0,0 +1,27 @@
    +   * @param integer $count
    

    ... this should be int not integer.

  5. +++ b/core/modules/comment/lib/Drupal/comment/CommentManagerInterface.php
    @@ -0,0 +1,27 @@
    +   * @return array
    

    What about using \Drupal\Core\CommentInterface[] to typehint it?

andypost’s picture

All addressed

we do add node_access on a comment entity query?

As pointed in #1938062-102: Convert the recent_comments block to a view and discussed in IRC RelationshipPluginBase.php automatically adds access tags for all entities involved in relation. But EQ does not do that (debugged already to make sure)

And while node_access tag is not applied automatically we should do this because user should not see comments from nodes that he is not allowed to access

dawehner’s picture

+++ b/core/modules/comment/lib/Drupal/comment/CommentManager.php
@@ -0,0 +1,64 @@
+      ->addTag('node_access')

There is no reason to not add multiple tags. ... nevertheless I think it is wrong to add one, given that this is supposed to be an api function.

andypost’s picture

So let's do it!

jibran’s picture

Status: Needs review » Needs work
Issue tags: +Needs issue summary update, +DX (Developer Experience), +API clean-up

The last submitted patch, drupal8.comment-module.2054993-20.patch, failed testing.

andypost’s picture

Status: Needs work » Needs review
FileSize
5.46 KB

re-roll

andypost’s picture

proper patch

andypost’s picture

jibran’s picture

Status: Needs review » Needs work

The last submitted patch, 24: drupal8.comment-module.2054993-24.patch, failed testing.

andypost’s picture

Status: Needs review » Needs work

The last submitted patch, 27: drupal8.comment-module.2054993-27.patch, failed testing.

andypost’s picture

Status: Needs work » Needs review
FileSize
5.24 KB

Status: Needs review » Needs work

The last submitted patch, 29: 2054993-recent-comments-29.patch, failed testing.

andypost’s picture

Status: Needs work » Needs review
tstoeckler’s picture

Now that this function is completely unused after #1938062: Convert the recent_comments block to a view, do we really still need it? If we do, I think we should introduce some test coverage for it as well.

andypost’s picture

Assigned: Unassigned » catch
Issue tags: +Needs committer feedback
FileSize
1.72 KB

Following #2158871-13: [Policy, no patch] Clearly denote when @deprecated code is slated to be removed there's still no conclusion about deprecated and removed so better to get commiter feedback on unused functions without test coverage.
Also this function is broken now

+++ b/core/modules/comment/comment.module
@@ -312,32 +312,11 @@ function comment_permission() {
-    $query->addTag('node_access');
...
-  $query->addMetaData('base_table', 'comment');

As the current implementation is broken and useless it make sense to get rid of it.

dawehner’s picture

Status: Needs review » Reviewed & tested by the community

I have to agree, this function is really unrealistic to be used at all and not part of some fundamental API.

catch’s picture

Assigned: catch » Unassigned

If I'm reading correctly, this should have been removed with #1938062: Convert the recent_comments block to a view but just got missed. If so I'm fine with just nuking it. Not committing right now though, but unassigning.

dawehner’s picture

@catch
Sadly it was decided to not do it: #1938062-85: Convert the recent_comments block to a view

xjm’s picture

Issue tags: -Needs committer feedback +Approved API change, +API change

That sounds like API change approval from @catch.

For my part, +1, kill it. We just split this off to avoid blocking the conversion of the block on whether or not we could kill this. Conversion landed, so kill. :)

xjm’s picture

Title: Refactor and possibly deprecate comment_get_recent() » Remove comment_get_recent()

Oh. Accurate title.

xjm’s picture

Issue tags: +Quick fix
xjm’s picture

Title: Remove comment_get_recent() » Remove (untested, unused, broken) comment_get_recent()

Sorry for all the noise.

webchick’s picture

Status: Reviewed & tested by the community » Fixed

Committed and pushed to 8.x.

Status: Fixed » Closed (fixed)

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