0

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.

1 Answer 1

0

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.

1
  • My comments are not listed from views, so here I get some directions link
    – Drale
    Commented Apr 4, 2023 at 11:11

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.