Score:0

Stick comments on top

ls flag

I need to stick specific comment on top of others which will remain from oldest to newest. When it's stick on top it's replies must be with that comment.

So

  1. I made links in .module file which will take method from controller
    use Drupal\comment\CommentInterface;
    use Drupal\Core\Url;

    function hook_comment_links_alter(array &$links, CommentInterface $entity, array &$context) {

      $links['comment_on_top'] = [
    '#links' => [
      'comment-report' => [
        'title' => t('Stick on top'),
        'url' => Url::fromRoute('comment_on_top.add',
          ['comment' => $entity->id(),
        ]),
      ],
    ],
      ];

      $links['comment_on_top_remove'] = [
    '#links' => [
      'comment-report' => [
        'title' => t('Remove from top'),
        'url' => Url::fromRoute('comment_on_top.remove',
          ['comment' => $entity->id(),
        ]),
      ],
    ],
      ];
    }
  1. This is my route file
    comment_on_top.add:
      path: '/my_module/stick/{comment}'
      defaults:
        _controller: '\Drupal\my_module\Controller\MyModuleController::stickOnTop'
      requirements:
        _permission: 'access content'

    comment_on_top.remove:
      path: '/my_module/remove/{comment}'
      defaults:
        _controller: '\Drupal\my_module\Controller\MyModuleController::removeFromTop'
      requirements:
        _permission: 'access content'
  1. I created "sticked" field of the boolean type via drush gen field and added to comment type.

  2. I put this two methods in my controller

public function stickOnTop($comment_id)
  {
    $comment = \Drupal::entityTypeManager()->getStorage("comment")->load($comment_id);
    $comment->field_sticked_to_top->value_1 = '1';
    $comment->save();
  }

  public function removeFromTop($comment_id)
  {
    $comment = \Drupal::entityTypeManager()->getStorage("comment")->load($comment_id);
    $comment->field_sticked_to_top->value_1 = '0';
    $comment->save();
  }

And it works!

But now I'm stuck in hook_preprocess_HOOK() to stick that comment at top also in my .module file

function my_module_preprocess_comment(&$variables)
{
// what to put where
}

When I use kint($variables); I'm getting all comments in separated objects.

Is solution to change weight $variables['elements']['#weight'] ?

What now?

I saw this question How to place a specific comment on top of list and Best reply module and it helped but not for all problem.

Score:0
de flag

I would do the following:

  1. Add a new field to the comment with a field name of field_sticky.
  2. Create a view, showing comments, with a page display.
  3. Set the path of the display to whatever you want the path to be.
  4. Set the view style to be unformatted list, set to display the rendered entity in the full view mode.
  5. Add a sort on field_sticky, descending
  6. Add another sort on created, either ascending or descending depending on what you want
  7. Ensure the sort on created comes AFTER the sort on field_sticky, reorder if necessary.

Now you have a page, listing your comments, with sticky comments first, other comments after. If necessary, you can add a filter on a specific comment type to limit the results to that comment type.

Drale avatar
ls flag
My comments are not listed from views, so here I get some directions [link](https://www.drupal.org/forum/support/module-development-and-code-questions/2023-03-31/drupal-9-stick-comment-on-top#comment-14996654)
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.