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
- 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(),
]),
],
],
];
}
- 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'
I created "sticked" field of the boolean type via drush gen field and added to comment type.
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.