You are here

social_ajax_comments.module in Open Social 8.8

Social AJAX comments module file.

File

modules/custom/social_ajax_comments/social_ajax_comments.module
View source
<?php

/**
 * @file
 * Social AJAX comments module file.
 */
use Drupal\ajax_comments\Utility;
use Drupal\comment\CommentInterface;
use Drupal\Component\Utility\Html;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Drupal\node\Entity\Node;
use Drupal\social_post\Entity\Post;

/**
 * Implements hook_entity_display_build_alter().
 */
function social_ajax_comments_entity_display_build_alter(&$build, $context) {

  // Only for comments on nodes on the default display.
  // This is to override the defaults in ajax_comments who
  // don't take into account correct wrapper and html id's
  // for our post and nodes.
  if (!$context['entity'] instanceof Node && !$context['entity'] instanceof Post) {
    return;
  }
  foreach ($build as $field_name => $field) {
    if (!empty($field['#field_type']) && $field['#field_type'] === 'comment') {

      // Check that this comment field uses Ajax Comments.

      /** @var \Drupal\ajax_comments\FieldSettingsHelper $field_settings_helper */
      $field_settings_helper = \Drupal::service('ajax_comments.field_settings_helper');
      $field_config = $build[$field_name]['#items']
        ->getFieldDefinition();
      $field_formatter = $field_settings_helper
        ->getFieldFormatter($context['display'], $field['#field_name'], $field_config, $context['display']
        ->getMode());
      if (!empty($field_formatter) && $field_settings_helper
        ->isEnabled($field_formatter)) {

        // Check if this ID is being generated in response to an Ajax request.
        if (Utility::isAjaxRequest(\Drupal::request())) {

          // Note that setting ajax as TRUE on Html here also fixes issue
          // with non-unique IDs on textarea elements return through ajax,
          // which otherwise could result in WYYSIWYG editors being
          // incorrectly attached by Drupal.attachBehaviors().
          Html::setIsAjax(TRUE);
        }
        $html_id = $field['#entity_type'] . '_' . $field['#bundle'] . '_' . $field['#field_name'];
        if ($context['entity'] instanceof Post) {

          // It will also run on AJAX commands so cant do a .=
          // otherwise it will keep on adding the id.
          $html_id = $field['#entity_type'] . '_' . $field['#bundle'] . '_' . $field['#field_name'] . '_' . $context['entity']
            ->id();
        }
        if (isset($build[$field_name])) {
          $build['#cache']['contexts'][] = 'url';
          $build[$field_name]['#attributes']['id'] = Html::getId($html_id);
          Utility::setEntityRenderArray($build, $context['entity'], $context['display']
            ->getMode());
        }
      }
    }
  }
}

/**
 * Implements hook_form_FORM_ID_alter().
 *
 * Triggers on form for deleting a comment on a post.
 */
function social_ajax_comments_form_comment_post_comment_delete_form_alter(&$form, FormStateInterface $form_state, $form_id) {

  // We set a new class, so we can remove wrapper div.
  // Due to the form.inc and confirm-form twig setup.
  if (\Drupal::request()
    ->isXmlHttpRequest()) {
    $form['#attributes']['class'][] = 'is_modal';
  }
}

/**
 * Implements hook_form_FORM_ID_alter().
 *
 * Triggers on form for deleting a comment on a node.
 */
function social_ajax_comments_form_comment_comment_delete_form_alter(&$form, FormStateInterface $form_state, $form_id) {

  // We set a new class, so we can remove wrapper div.
  // Due to the form.inc and confirm-form twig setup.
  if (\Drupal::request()
    ->isXmlHttpRequest()) {
    $form['#attributes']['class'][] = 'is_modal';
  }
}

/**
 * Implements hook_comment_links_alter().
 *
 * Alter the links of a comment.
 */
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;
  }
}

/**
 * Implements hook_ajax_comments_wrapper_id_alter().
 */
function social_ajax_comments_ajax_comments_wrapper_id_alter(&$wrapper_html_id, ContentEntityInterface $commented_entity, $field_name) {
  if ($commented_entity instanceof Node && $field_name === 'field_topic_comments') {
    $wrapper_html_id = Html::getId($commented_entity
      ->getEntityTypeId() . '-' . $commented_entity
      ->bundle() . '-' . $field_name);
  }
  if ($commented_entity instanceof Post && $field_name === 'field_post_comments') {
    $wrapper_html_id = Html::getId($commented_entity
      ->getEntityTypeId() . '-' . $commented_entity
      ->bundle() . '-' . 'field_post_comments' . '-' . $commented_entity
      ->id());
  }
}