Finding hook_ENTITY_TYPE_access
/hook_comment_access
not working when trying to return AccessResult::forbidden()->cachePerUser()
after comparing the comment owner ID and current user ID (probably related: #2879087), can you please try the following preprocess logic? As I currently have no project to properly check this out myself I'm not sure if it works that way and if #plain_text
can simply be (mis-)used as a cache busting switch in a template at all.
/**
* Implements hook_preprocess_HOOK().
*/
function MYMODULE_preprocess_comment(&$variables) {
$variables['own_comment'] = [
'#plain_text' => 'no',
'#cache' => [
'contexts' => ['user'],
],
];
/** @var \Drupal\comment\Entity\Comment $comment */
$comment = $variables['elements']['#comment'];
if ($comment->getOwner()->id() === \Drupal::currentUser()->id()) {
$variables['own_comment']['#plain_text'] = 'yes';
}
}
{% if own_comment['#plain_text'] == 'yes' %}
{{ content }}
{% else %}
{{ 'Not your comment'|t }}
{% endif %}
Maybe I'm actually doing the same you did and in the end the only way is to implement your own comments field formatter, similar to the one in the Manage comments on own content module.