You are here

function ajax_comments_form_comment_form_alter in AJAX Comments 7

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

Implements hook_form_FORM_ID_alter().

File

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

Code

function ajax_comments_form_comment_form_alter(&$form, &$form_state, $form_id) {

  // Check to see if this node type uses ajax comments.
  if (!ajax_comments_node_type_active($form['#node']->type)) {
    return;
  }

  // Disable output original node/comment in preview
  if (isset($form['comment_preview'])) {
    $form['notify_text'] = array(
      '#markup' => variable_get('ajax_comments_notify', '') ? theme('ajax_comments_notify_text', array(
        'type' => 'preview',
        'comment' => $form_state['comment'],
      )) : '',
      '#weight' => "-100",
    );
    if (isset($form['comment_output_below'])) {
      unset($form['comment_output_below']);
    }
  }
  if (empty($form_state['storage']['ajax_comments_form_id'])) {
    $cid = $pid = empty($form_state['comment']->cid) ? 0 : $form_state['comment']->cid;
    $pid = empty($form_state['comment']->pid) ? 0 : $form_state['comment']->pid;
    $id = 'ajax-comments-reply-form-' . $form_state['comment']->nid . '-' . $pid . '-' . $cid;
    $form_state['storage']['ajax_comments_form_id'] = $id;

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

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

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

        // Add New Comment Form
        $form_state_class = 'ajax-comments-form-add';
      }
    }
    $form_state['storage']['form_state_class'] = $form_state_class;
  }
  else {
    $id = $form_state['storage']['ajax_comments_form_id'];
    $form_state_class = $form_state['storage']['form_state_class'];
  }
  $form['#attributes']['id'] = drupal_html_id($id);
  $form['#attributes']['class'][] = $id;
  $form['#attributes']['class'][] = $form_state_class;
  $form['actions']['submit']['#ajax'] = array(
    'callback' => 'ajax_comments_submit_js',
    'wrapper' => $form['#attributes']['id'],
    'method' => 'replace',
    'effect' => 'fade',
  );

  // Set unique id (need for Views with enabled AJAX)
  if (empty($form['actions']['submit']['#id'])) {
    $form['actions']['submit']['#id'] = drupal_html_id('edit-' . $id);
  }
  $form['actions']['preview']['#ajax'] = array(
    '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_html_id('preview-' . $id);
  }

  // If this a reply to comment offer a 'cancel' button
  if (isset($form_state['comment']->pid)) {
    $form['actions']['cancel'] = array(
      '#type' => 'button',
      '#value' => t('Cancel'),
      '#access' => true,
      '#weight' => 21,
      '#limit_validation_errors' => array(),
    );
    $form['actions']['cancel']['#ajax'] = array(
      'wrapper' => $form['#attributes']['id'],
      'method' => 'replace',
      'effect' => 'fade',
    );
    if (empty($form_state['comment']->cid)) {
      $form['actions']['cancel']['#ajax']['callback'] = 'ajax_comments_cancel_js';
    }
    else {
      $form['actions']['cancel']['#ajax']['callback'] = 'ajax_comments_edit_cancel_js';
    }

    // Set unique id (need for Views with enabled AJAX)
    if (empty($form['actions']['cancel']['#id'])) {
      $form['actions']['cancel']['#id'] = drupal_html_id('cancel-' . $id);
    }
  }
  $form['#attached'] = array(
    'js' => array(
      drupal_get_path('module', 'ajax_comments') . '/ajax_comments.js',
    ),
  );
  $form['actions'] = ajax_pre_render_element($form['actions']);

  // HACK, stop ctools from modifying us in node_comment_form.inc
  $form_state['ctools comment alter'] = FALSE;
}