You are here

function social_ajax_comments_comment_links_alter in Open Social 10.0.x

Same name and namespace in other branches
  1. 8.9 modules/custom/social_ajax_comments/social_ajax_comments.module \social_ajax_comments_comment_links_alter()
  2. 8.8 modules/custom/social_ajax_comments/social_ajax_comments.module \social_ajax_comments_comment_links_alter()
  3. 10.3.x modules/custom/social_ajax_comments/social_ajax_comments.module \social_ajax_comments_comment_links_alter()
  4. 10.1.x modules/custom/social_ajax_comments/social_ajax_comments.module \social_ajax_comments_comment_links_alter()
  5. 10.2.x modules/custom/social_ajax_comments/social_ajax_comments.module \social_ajax_comments_comment_links_alter()

Implements hook_comment_links_alter().

Alter the links of a comment.

File

modules/custom/social_ajax_comments/social_ajax_comments.module, line 96
The Social AJAX comments module.

Code

function social_ajax_comments_comment_links_alter(array &$links, CommentInterface $entity, array &$context) {
  $bundle = $entity
    ->bundle();
  $commented_entity = $entity
    ->getCommentedEntity();
  $account = \Drupal::currentUser();

  // Url::fromRoute()->toRenderable doesn't fully take care of access checks.
  // We need the custom checks to override what Ajax comments does for us.
  // Mainly because of UX flaws in the edit form that opens up.
  // For now we don't support edit ajax actions nor unpublishing.
  if (!empty($links['comment']['#links']['comment-edit'])) {
    $links['comment']['#links']['comment-edit']['attributes']['class'] = [];
    $links['comment']['#links']['comment-edit']['url'] = Url::fromRoute('entity.comment.edit_form', [
      'comment' => $entity
        ->id(),
    ]);
  }

  // For post comments we need to render better data wrapper's so
  // Ajax replace works on the right ID.
  if ($entity
    ->isPublished() && $account
    ->hasPermission('administer comments') && $bundle === 'post_comment') {
    $wrapper_id = Html::getId($commented_entity
      ->getEntityTypeId() . '-' . $commented_entity
      ->bundle() . '-' . 'field_post_comments' . '-' . $commented_entity
      ->id());
    $links['comment']['#links']['comment-edit']['attributes']['data-wrapper-html-id'] = $wrapper_id;
    $links['comment']['#links']['comment-unpublish']['attributes']['data-wrapper-html-id'] = $wrapper_id;
    $links['comment']['#links']['comment-delete']['attributes']['data-wrapper-html-id'] = $wrapper_id;
  }
}