You are here

function ajax_comments_comment_links_alter in AJAX Comments 8

Implements hook_comment_links_alter().

For entity bundles with ajax comments enabled, alter the comment link classes to allow ajax behaviors to be attached.

File

./ajax_comments.module, line 38
AJAX comments module file.

Code

function ajax_comments_comment_links_alter(array &$links, CommentInterface &$entity, array &$context) {
  $request = \Drupal::request();

  /** @var \Drupal\ajax_comments\FieldSettingsHelper $field_settings_helper */
  $field_settings_helper = \Drupal::service('ajax_comments.field_settings_helper');
  $comment_formatter = $field_settings_helper
    ->getFieldFormatterFromComment($entity, $context['view_mode']);
  if (!empty($comment_formatter) && $field_settings_helper
    ->isEnabled($comment_formatter)) {

    // A little HACK for do not mark as NEW own comments.
    if ($entity
      ->isNew() && $entity
      ->getOwnerId() === \Drupal::currentUser()
      ->id()) {
      $entity
        ->enforceIsNew(FALSE);
    }
    $field_name = $entity
      ->getFieldName();
    $wrapper_html_id = Utility::getWrapperIdFromEntity($context['commented_entity'], $field_name);

    // Attach classes to comment delete links to allow ajax_comments.js
    // to attach ajax behaviors to them.
    if (isset($links['comment']['#links']['comment-delete'])) {
      $classes = [
        'use-ajax',
        'js-use-ajax-comments',
        'js-ajax-comments-delete',
        'js-ajax-comments-delete-' . $entity
          ->id(),
      ];
      if (empty($links['comment']['#links']['comment-delete']['attributes']['class'])) {
        $links['comment']['#links']['comment-delete']['attributes']['class'] = $classes;
      }
      else {
        $links['comment']['#links']['comment-delete']['attributes']['class'] = array_unique(array_merge($links['comment']['#links']['comment-delete']['attributes']['class'], $classes));
      }

      // Set the delete confirmation form to appear in a modal dialog box,
      // if available.
      $links['comment']['#links']['comment-delete']['attributes']['data-dialog-type'] = 'modal';
      $links['comment']['#links']['comment-delete']['attributes']['data-dialog-options'] = Json::encode([
        'width' => 700,
      ]);
      $links['comment']['#links']['comment-delete']['attributes']['data-wrapper-html-id'] = $wrapper_html_id;
    }

    // Attach classes to comment edit links to allow ajax_comments.js
    // to attach ajax behaviors to them.
    if (isset($links['comment']['#links']['comment-edit'])) {
      $classes = [
        'use-ajax',
        'js-use-ajax-comments',
        'js-ajax-comments-edit',
        'js-ajax-comments-edit-' . $entity
          ->id(),
      ];
      if (empty($links['comment']['#links']['comment-edit']['attributes']['class'])) {
        $links['comment']['#links']['comment-edit']['attributes']['class'] = $classes;
      }
      else {
        $links['comment']['#links']['comment-edit']['attributes']['class'] = array_unique(array_merge($links['comment']['#links']['comment-edit']['attributes']['class'], $classes));
      }
      $links['comment']['#links']['comment-edit']['attributes']['data-wrapper-html-id'] = $wrapper_html_id;
      $links['comment']['#links']['comment-edit']['url'] = Url::fromRoute('ajax_comments.edit', [
        'comment' => $entity
          ->id(),
      ]);
    }

    // Attach classes to comment reply links to allow ajax_comments.js
    // to attach ajax behaviors to them.
    if (isset($links['comment']['#links']['comment-reply'])) {
      $classes = [
        'use-ajax',
        'js-use-ajax-comments',
        'js-ajax-comments-reply',
        'js-ajax-comments-reply-' . $entity
          ->getCommentedEntityId() . '-' . $entity
          ->getFieldName() . '-' . $entity
          ->id(),
      ];
      if (empty($links['comment']['#links']['comment-reply']['attributes']['class'])) {
        $links['comment']['#links']['comment-reply']['attributes']['class'] = $classes;
      }
      else {
        $links['comment']['#links']['comment-reply']['attributes']['class'] = array_unique(array_merge($links['comment']['#links']['comment-reply']['attributes']['class'], $classes));
      }
      $links['comment']['#links']['comment-reply']['attributes']['data-wrapper-html-id'] = $wrapper_html_id;
      $links['comment']['#links']['comment-reply']['url'] = Url::fromRoute('ajax_comments.reply', [
        'entity_type' => $entity
          ->getCommentedEntityTypeId(),
        'entity' => $entity
          ->getCommentedEntityId(),
        'field_name' => $entity
          ->getFieldName(),
        'pid' => $entity
          ->id(),
      ]);
    }
  }
}