This a follow-up from #2155387: Multiple comment fields per entity are supported, but break horribly when they need pagers

Formatters and other container affected things should properly inject this function

Contrib modules should be able to override the implementation

CommentFileSizeAuthor
#57 2156089_56.patch13.93 KBslashrsm
#54 interdiff-2156089-50-54.txt3.42 KBroderik
#54 2156089-54.patch14 KBroderik
#50 2156089-50.patch14.22 KBandypost
#50 interdiff.txt1.88 KBandypost
#48 2156089_48.patch14.2 KBslashrsm
#46 interdiff-2156089-44-46.txt653 bytesroderik
#46 comment-get-thread-dep-2156089.46.patch14 KBroderik
#44 interdiff-2.txt4.24 KBroderik
#44 interdiff-1.txt1.63 KBroderik
#44 comment-get-thread-dep-2156089.44.patch13.95 KBroderik
#42 comment-get-thread-dep-2156089.42.patch14.46 KBlarowlan
#42 interdiff.txt813 byteslarowlan
#40 comment-get-thread-dep-2156089.40.patch14.45 KBlarowlan
#40 interdiff.txt2.64 KBlarowlan
#35 interdiff.txt5.42 KBslashrsm
#35 2156089_35.patch45.66 KBslashrsm
#32 2156089_32.patch15.27 KBslashrsm
#29 interdiff-2156089-27-79.txt2.09 KBroderik
#29 2156089-29-comment-get-thread.patch15.06 KBroderik
#27 2156089-27-comment-get-thread.patch14.74 KBroderik
#20 interdiff-2156089-17-20.txt4.57 KBroderik
#20 2156089-20-comment-get-thread.patch12.83 KBroderik
#19 interdiff-2156089-17-19.txt3.55 KBroderik
#19 2156089-19-comment-get-thread.patch12.58 KBroderik
#18 2156089-17-comment-get-thread.patch11.88 KBroderik
#15 interdiff-2156089-12-13.txt718 bytesroderik
#15 2156089-13-comment-get-thread.patch12.01 KBroderik
#12 interdiff-2156089-9-12.txt804 bytesroderik
#12 2156089-12-comment-get-thread.patch12.04 KBroderik
#9 interdiff-2156089-7-9.txt1.19 KBroderik
#9 2156089-9-comment-get-thread.patch12.04 KBroderik
#7 2156089-7-comment-get-thread.patch11.84 KBroderik
#5 2156089-comment-get-thread.patch11.97 KBroderik
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

larowlan’s picture

Should this go on the storage controller because its related to storage?

andypost’s picture

There are a lot of debates about it in issues about EntityQuery conversion

I think if the function could use "universal" QueryFactory it should live in manager because storage independent.
Once query involves sql-specific (db_query() for example) - should be places in swappable storage

larowlan’s picture

Yes, you are right

roderik’s picture

Status: Active » Closed (duplicate)

There's an ORDER BY a certain expression in comment_get_thread(), to make the speed bearable. So it goes into swappable storage afaict.

This is going to be part of CommentStorageInterface (which still needs work) in #2068331: Convert comment SQL queries to the Entity Query API - so I guess this one can be closed.

roderik’s picture

Status: Closed (duplicate) » Needs review
FileSize
11.97 KB

Actually - #2068331: Convert comment SQL queries to the Entity Query API grew way too large, so I'll reverse strategy, and split out comment_get_thread() into here.

For your review. I reshuffled the code/arguments a bit to allow for the possibility of not applying PagerSelectExtender.

One possibly outstanding issue is the call to \Drupal::currentUser() that's inside CommentStorage::loadThread() now.
Could we just make that into an argument 'has admin rights' to pass into the method?
Maybe even sneak that as a bitmask into the $mode argument?

roderik’s picture

Title: Deprecate comment_get_thread() in favour of method on CommentManager » Deprecate comment_get_thread() in favour of method on CommentStorage

retitling

roderik’s picture

FileSize
11.84 KB

Reroll after PSR-4

slashrsm’s picture

Status: Needs review » Needs work

Looks good. Just one minor comment.

+++ b/core/modules/comment/comment.module
@@ -974,8 +880,9 @@ function comment_node_update_index(EntityInterface $node, $langcode) {
+      if ($node->get($field_name)->status
+          && $comments = \Drupal::entityManager()->getStorage('comment')
+               ->loadThread($node, $field_name, $mode, $comments_per_page)) {
         comment_prepare_thread($comments);

Let's move $comments assignment out of condition.

roderik’s picture

Status: Needs work » Needs review
FileSize
12.04 KB
1.19 KB

Alright, no contest here.
(Spotted it but had no opinion so left as-is)

slashrsm’s picture

Status: Needs review » Reviewed & tested by the community

Looks good.

alexpott’s picture

Status: Reviewed & tested by the community » Needs work
+++ b/core/modules/comment/comment.module
@@ -611,107 +611,13 @@ function comment_add(EntityInterface $entity, $field_name = 'comment', $pid = NU
+  $nodes = \Drupal::entityManager()->getStorage('comment')->loadThread($entity, $field_name, $mode, $comments_per_page, $pager_id);
+  return array_keys($nodes);

These are not nodes.

roderik’s picture

Status: Needs work » Needs review
FileSize
12.04 KB
804 bytes

Hummm they're very much not.

larowlan’s picture

Status: Needs review » Reviewed & tested by the community

Back to rtbc

larowlan’s picture

roderik’s picture

FileSize
12.01 KB
718 bytes

"Wait, what was I thinking... This is not even true."

/me strips off part of an earlier addition to comments

roderik’s picture

Status: Reviewed & tested by the community » Needs review

crosspost, sorry larowlan discovered another minimal thing :)

roderik’s picture

Reroll for #2228763 (replaced field_id by field_name occurrences on 2 lines).

roderik’s picture

roderik’s picture

Still having the feeling that using a Drupal::currentUser() call inside CommentStorage will/should be shot down. See #5.

Here's a suggested patch: don't check user access inside loadThread() but require a extra flag to display unpublished comments. (This would open up the possibility for using 'non admin' displays for admin users too. And other flags like 'include to-be-moderated comments'.)

By the way, the patch doesn't look like it but I did check for other uses of COMMENT_MODE_FLAT / COMMENT_MODE_THREADED. I don't think there are any other places (yet) that should assume COMMENT_ACCESS_MODE_ADMIN could even be an input value.
(Except a possible future change to comment_new_page_count() - but not yet. I think there may be a bug in there that potentially returns the wrong page number for admin users, but getting into that is way out of scope for this issue.)

roderik’s picture

FileSize
12.83 KB
4.57 KB

Oops, forgot to check my own loadThread() calls. Interdiff from 17 included.

Side effect of this patch: comment_node_update_index can never read unpublished comments by mistake. I guess that's a good thing.

The last submitted patch, 19: 2156089-19-comment-get-thread.patch, failed testing.

slashrsm’s picture

Status: Needs review » Reviewed & tested by the community
slashrsm’s picture

alexpott’s picture

Status: Reviewed & tested by the community » Needs work
Issue tags: +Needs change record

The change to add COMMENT_ACCESS_MODE_ADMIN and not make comment_get_thread() determine if the current user has the administer comments permission needs a change notice.

slashrsm’s picture

Change record need to be created before or after this goes in?

larowlan’s picture

Before and saved as a draft

roderik’s picture

Status: Needs work » Needs review
FileSize
14.74 KB

Rerolled:
changed some comments for COMMENT_MODE_FLAT -> CommentManagerInterface::COMMENT_MODE_FLAT;
moved COMMENT_ACCESS_MODE_ADMIN into CommentManagerInterface because that's where the other constants are now.

This means that now, CommentStorage is dependent on CommentManagerInterface where it wasn't before... ('use' statement added during reroll).

Change notice will be added to https://www.drupal.org/node/2299799 soon. This means the change notice will document more than one not-yet-committed issue.

Status: Needs review » Needs work

The last submitted patch, 27: 2156089-27-comment-get-thread.patch, failed testing.

roderik’s picture

Status: Needs work » Needs review
FileSize
15.06 KB
2.09 KB

Fixing my own include...

slashrsm’s picture

Status: Needs review » Reviewed & tested by the community
alexpott’s picture

Status: Reviewed & tested by the community » Needs work
Issue tags: -Needs change record +Needs reroll
slashrsm’s picture

Status: Needs work » Needs review
FileSize
15.27 KB
slashrsm’s picture

Status: Needs review » Reviewed & tested by the community

Was very simple re-roll.

alexpott’s picture

Status: Reviewed & tested by the community » Needs work
+++ b/core/modules/comment/src/CommentManagerInterface.php
@@ -27,6 +27,13 @@
   /**
+   * All comments are displayed, not only published ones (which is the default).
+   * Some functions accept this mode in addition to (OR-ed with) COMMENT_MODE
+   * constants, but it is not a 'setting' like the other modes.
+   */
+  const COMMENT_ACCESS_MODE_ADMIN = 2;

I'm not sure about this. Using bitwise operator to join this together with the threaded and flat mode is kinda weird because you can't join them together... why are we doing this? The service already has the current user service. If we need to do this then lets add an argument to loadThread to include admin comments if the user has access.

slashrsm’s picture

Status: Needs work » Needs review
Issue tags: -Needs reroll
FileSize
45.66 KB
5.42 KB

Done.

Status: Needs review » Needs work

The last submitted patch, 35: 2156089_35.patch, failed testing.

slashrsm’s picture

Status: Needs work » Needs review
FileSize
14.49 KB

Meh... broken diff. Should be fine now. Interdiff from #35 is still valid.

andypost’s picture

getDisplayOrdinal() method does not pass any additional parameters, and both functions should be unified.

+++ b/core/modules/comment/src/CommentStorageInterface.php
@@ -103,11 +103,14 @@ public function getChildCids(array $comments);
+   * @param bool $load_unpublished_for_admins
+   *   Unpublished comments will be included in thread if set to TRUE and
+   *   current user has the 'administer comments' permission.

suppose "include_unpublished" better because it affects not only admins

The last submitted patch, 37: 2156089_35.patch, failed testing.

larowlan’s picture

Fixes 40

Status: Needs review » Needs work

The last submitted patch, 40: comment-get-thread-dep-2156089.40.patch, failed testing.

larowlan’s picture

Status: Needs work » Needs review
FileSize
813 bytes
14.46 KB

doh

marcingy’s picture

Status: Needs review » Reviewed & tested by the community

Looks good

roderik’s picture

Status: Reviewed & tested by the community » Needs review
FileSize
13.95 KB
1.63 KB
4.24 KB

Interdiff #1: backed out references to COMMENT_ACCESS_MODE_ADMIN in code comments.

interdiff #2: sorry for causing the confusion guys, but I'm backing out the whole part referring to this flag/function argument. I promise to post the reverse interdiff-2.txt in a followup patch with attribution.

Answers:
#34/alexpott

why are we doing this? The service already has the current user service.

We didn't have the current user service at the time I added this into the patch, and I (still newbie) mistakenly thought we could not have it. (From the combination of an old andypost comment and me agreeing that including $currentuser is weird.)

It seems that assumption is wrong**. So now that we have the current user service, this part is indeed not necessary. And shouldn't be included in this issue.

#38/andypost

getDisplayOrdinal() method does not pass any additional parameters, and both functions should be unified.

You're right.
On top of that, I think getNewCommentPageNumber() should be unified with those functions, even though it isn't now. (In other words: since it doesn't do the same permission check, I think that Drupal contains a bug where a link to the wrong page number for new comments could be output for admin users.)

I'll take care of that in the followup issue, and extend the tests to... test my suspicion.

--

** I can understand it if $currentuser doesn't actually contain a 'static' user but a 'proxy object' that always gets the active user. That's still unclear to me, but w/e.

andypost’s picture

@roderik Awesome! just a small nitpick

a 'proxy object' that always gets the active user

it is

+++ b/core/modules/comment/comment.module
@@ -807,11 +714,14 @@ function comment_node_update_index(EntityInterface $node, $langcode) {
+          comment_prepare_thread($comments);
+          $build = comment_view_multiple($comments);

I'd better use view builder here like formatter does $build = $this->viewBuilder->viewMultiple($comments);

Also needs follow-up to get rid of the function comment_view_multiple()

roderik’s picture

FileSize
14 KB
653 bytes

OK done.

Also needs follow-up to get rid of the function comment_view_multiple()

Doesn't ring a bell here. Is D8 deprecating comment_view_multiple() ( and comment_view() ) the same way we did with comment_load[_multiple]()? I'll open a follow-up when I understand.

Also: comment_prepare_thread(). Last month I didn't understand how to get rid of it but now it seems logical to me to move it into CommentViewBuilder::viewMultiple(). In the same follow-up. Or did you mean that?

roderik’s picture

Assigned: Unassigned » roderik
Status: Needs review » Needs work
slashrsm’s picture

Status: Needs work » Needs review
FileSize
14.2 KB

Status: Needs review » Needs work

The last submitted patch, 48: 2156089_48.patch, failed testing.

andypost’s picture

Status: Needs work » Needs review
FileSize
1.88 KB
14.22 KB

Proper fix for table name and $comments_per_page - why new implementation uses default argument with value 0?

Also fixed table alias after #1498662: Refactor comment entity properties to multilingual

slashrsm’s picture

Status: Needs review » Reviewed & tested by the community
roderik’s picture

Thanks for fixing. (I will keep falling away for some days because of work.)

why new implementation uses default argument with value 0?

Per #5: comment_get_thread() always applied paging (PagerSelectExtender). While we are nailing down interfaces here,

  • it seemed strange to me to make it impossible to not apply PagerSelectExtender ...even though I know of no current practical use for it...
  • in this case 'not applying PagerSelectExtender' (default argument = 0) feels to me like the most sensible default.

Opinions on that, welcome. I do appreciate these detailed reviews because it makes me feel safer to actually experiment with changes like this :)

alexpott’s picture

Status: Reviewed & tested by the community » Needs work
  1. +++ b/core/modules/comment/comment.module
    @@ -456,109 +457,13 @@ function comment_node_view_alter(array &$build, EntityInterface $node, EntityVie
    -  if ($mode == CommentManagerInterface::COMMENT_MODE_FLAT) {
    -    $query->orderBy('c.cid', 'ASC');
    -  }
    -  else {
    -    // See comment above. Analysis reveals that this doesn't cost too
    -    // much. It scales much much better than having the whole comment
    -    // structure.
    -    $query->addExpression('SUBSTRING(c.thread, 1, (LENGTH(c.thread) - 1))', 'torder');
    -    $query->orderBy('torder', 'ASC');
    -  }
    
    +++ b/core/modules/comment/src/CommentStorage.php
    @@ -216,6 +217,124 @@ public function getChildCids(array $comments) {
    +    if (($mode & CommentManagerInterface::COMMENT_MODE_THREADED)
    +        == CommentManagerInterface::COMMENT_MODE_THREADED) {
    +      // See comment above. Analysis reveals that this doesn't cost too
    +      // much. It scales much much better than having the whole comment
    +      // structure.
    +      $query->addExpression('SUBSTRING(c.thread, 1, (LENGTH(c.thread) - 1))', 'torder');
    +      $query->orderBy('torder', 'ASC');
    +    }
    +    else {
    +      $query->orderBy('c.cid', 'ASC');
    +    }
    

    Why have we swapped this around? Plus we don't need the bitwise operations anymore.

  2. +++ b/core/modules/comment/src/CommentStorageInterface.php
    @@ -85,6 +86,28 @@ public function getDisplayOrdinal(CommentInterface $comment, $comment_mode, $div
    +   *   Ordered array of comment objects.
    

    Lets add the fact that they are keyed by ID.

roderik’s picture

Status: Needs work » Needs review
FileSize
14 KB
3.42 KB
  1. True
  2. OK
  3. I didn't know of #2205673: [META] Remove all @deprecated functions marked "remove before 8.0" until now. So we're not marking functions as deprecated, but removing them. *remove* Will adjust wording in https://www.drupal.org/node/2299799.
slashrsm’s picture

Status: Needs review » Reviewed & tested by the community

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 54: 2156089-54.patch, failed testing.

slashrsm’s picture

Status: Needs work » Needs review
FileSize
13.93 KB

Re-roll.

marcingy’s picture

Status: Needs review » Reviewed & tested by the community

Looks good

alexpott’s picture

Title: Deprecate comment_get_thread() in favour of method on CommentStorage » Remove comment_get_thread() in favour of method on CommentStorage
Status: Reviewed & tested by the community » Fixed

re #54.3 Well, normally we deprecate and then remove in separate patches - since this gives a chance for module developers to convert but I can not imagine this is a widely used function.

Committed 6e6f2c6 and pushed to 8.x. Thanks!

+1 for the British spelling of favour in the issue title :)

  • alexpott committed 6e6f2c6 on 8.x
    Issue #2156089 by roderik, slashrsm, larowlan, andypost: Remove...

Status: Fixed » Closed (fixed)

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