You are here

function ajax_comments_form_comment_form_alter in AJAX Comments 8

Same name and namespace in other branches
  1. 6 ajax_comments.module \ajax_comments_form_comment_form_alter()
  2. 7 ajax_comments.module \ajax_comments_form_comment_form_alter()

Implements hook_form_FORM_ID_alter().

File

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

Code

function ajax_comments_form_comment_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {

  /** @var \Drupal\ajax_comments\FieldSettingsHelper $field_settings_helper */
  $field_settings_helper = \Drupal::service('ajax_comments.field_settings_helper');

  /** @var \Drupal\Core\Routing\CurrentRouteMatch $current_route */
  $current_route = \Drupal::service('current_route_match');

  // Ajax replies to other comments should happen on the canonical entity page
  // (note this functionality has not been ported to D8, yet).
  // If the user is on the standalone comment reply page, it means JavaScript
  // is disabled or the ajax functionality is not working. Do not proceed with
  // the form alter.
  if (in_array($current_route
    ->getRouteName(), [
    'comment.reply',
    'entity.comment.edit_form',
  ])) {
    return;
  }

  /** @var \Drupal\comment\CommentInterface $comment */
  $comment = $form_state
    ->getFormObject()
    ->getEntity();

  /** @var \Drupal\Core\Entity\EntityInterface $commented_entity */
  $commented_entity = $comment
    ->getCommentedEntity();
  $field_name = $comment
    ->getFieldName();

  // Check to see if this node type uses ajax comments.
  $comment_formatter = $field_settings_helper
    ->getFieldFormatterFromComment($comment, 'full');
  if (empty($comment_formatter) || !$field_settings_helper
    ->isEnabled($comment_formatter)) {
    return;
  }
  $cid = $comment
    ->id() ? $comment
    ->id() : 0;
  $pid = $comment
    ->get('pid')->target_id ? $comment
    ->get('pid')->target_id : 0;
  $id = 'ajax-comments-reply-form-' . $commented_entity
    ->getEntityTypeId() . '-' . $commented_entity
    ->id() . '-' . $comment
    ->getFieldName() . '-' . $pid . '-' . $cid;

  // Get comment form state class.
  if (!empty($cid)) {

    // This is the Comment Edit Form.
    $form_state_class = 'ajax-comments-form-edit';
  }
  else {
    if (!empty($pid)) {

      // This is the Comment Reply Form.
      $form_state_class = 'ajax-comments-form-reply';
    }
    else {

      // This is the Add New Comment Form.
      $form_state_class = 'ajax-comments-form-add';
    }
  }
  $form['#attributes']['class'][] = $id;
  $form['#attributes']['class'][] = $form_state_class;

  // Set unique id (need for Views with enabled AJAX).
  if (empty($form['actions']['submit']['#id'])) {
    $form['actions']['submit']['#id'] = \Drupal\Component\Utility\Html::getUniqueId('edit-' . $id);
  }
  $form['actions']['preview']['#ajax'] = [
    'callback' => '_ajax_comments_preview_js',
    'wrapper' => $form['#attributes']['id'],
    'method' => 'replace',
    'effect' => 'fade',
  ];

  // Set unique id (need for Views with enabled AJAX).
  if (empty($form['actions']['preview']['#id'])) {
    $form['actions']['preview']['#id'] = \Drupal\Component\Utility\Html::getUniqueId('preview-' . $id);
  }
}