You are here

function social_comment_comment_links_alter in Open Social 10.1.x

Same name and namespace in other branches
  1. 8.9 modules/social_features/social_comment/social_comment.module \social_comment_comment_links_alter()
  2. 8 modules/social_features/social_comment/social_comment.module \social_comment_comment_links_alter()
  3. 8.2 modules/social_features/social_comment/social_comment.module \social_comment_comment_links_alter()
  4. 8.3 modules/social_features/social_comment/social_comment.module \social_comment_comment_links_alter()
  5. 8.4 modules/social_features/social_comment/social_comment.module \social_comment_comment_links_alter()
  6. 8.5 modules/social_features/social_comment/social_comment.module \social_comment_comment_links_alter()
  7. 8.6 modules/social_features/social_comment/social_comment.module \social_comment_comment_links_alter()
  8. 8.7 modules/social_features/social_comment/social_comment.module \social_comment_comment_links_alter()
  9. 8.8 modules/social_features/social_comment/social_comment.module \social_comment_comment_links_alter()
  10. 10.3.x modules/social_features/social_comment/social_comment.module \social_comment_comment_links_alter()
  11. 10.0.x modules/social_features/social_comment/social_comment.module \social_comment_comment_links_alter()
  12. 10.2.x modules/social_features/social_comment/social_comment.module \social_comment_comment_links_alter()

Implements hook_comment_links_alter().

Alter the links of a comment.

File

modules/social_features/social_comment/social_comment.module, line 163
The Social comment module.

Code

function social_comment_comment_links_alter(array &$links, CommentInterface $entity, array &$context) {
  if (!$entity
    ->isPublished()) {
    if (\Drupal::moduleHandler()
      ->moduleExists('social_ajax_comments')) {
      unset($links['comment']['#links']['comment-reply']['attributes']['class']);
    }

    // If the comment is not published disable the reply link.
    $links['comment']['#links']['comment-reply']['url'] = Url::fromRoute('<nolink>');
  }
  $account = \Drupal::currentUser();
  if ($entity
    ->isPublished() && $account
    ->hasPermission('administer comments')) {
    $links['comment']['#links']['comment-unpublish'] = [
      'title' => t('Unpublish'),
      'url' => Url::fromRoute('social_comment.unpublish', [
        'comment' => $entity
          ->id(),
      ]),
    ];
  }
  if ($entity
    ->bundle() === 'post_comment') {

    // For post comments remove the reply button.
    unset($links['comment']['#links']['comment-reply']);
  }
  elseif ($entity
    ->getParentComment() !== NULL || \Drupal::routeMatch()
    ->getRouteName() === 'comment.reply') {

    // If this is already on the second level, remove the reply button.
    unset($links['comment']['#links']['comment-reply']);
  }
  else {

    /** @var \Drupal\group\Entity\Storage\GroupContentStorageInterface $storage */
    $storage = \Drupal::entityTypeManager()
      ->getStorage('group_content');

    /** @var \Drupal\Core\Entity\ContentEntityInterface $commented_entity */
    $commented_entity = $entity
      ->getCommentedEntity();
    $group_contents = $storage
      ->loadByEntity($commented_entity);

    // Only react if it is actually posted inside a group.
    foreach ($group_contents as $group_content) {
      $group = $group_content
        ->getGroup();

      // Remove comments from output if user don't have access.
      if (!$group
        ->hasPermission('post comments', $account)) {
        unset($links['comment']['#links']['comment-reply']);
        break;
      }
    }
  }
}