You are here

function comment_upload_alter_comment_form in Comment Upload 6

Alter the comment form to support AHAH uploads.

Note; not a hook implementation.

1 call to comment_upload_alter_comment_form()
comment_upload_form_alter in ./comment_upload.module
Add an Attachments for comments setting to the content type settings forms.

File

./comment_upload.module, line 100
Provides file attachment functionality for comments.

Code

function comment_upload_alter_comment_form(&$form, $form_state) {
  if (!user_access('upload files to comments')) {
    return;
  }

  // Check whether attachments are enabled for comments on this content type.
  $node = node_load($form['nid']['#value']);
  if (!variable_get('comment_upload_' . $node->type, 0)) {
    return;
  }
  $files = array();
  $cid = $form['cid']['#value'];

  // Rebuild indicates whether we have a pristine form (FALSE) or one that has been trough validation -> submission once.
  if (empty($form_state['rebuild'])) {
    if ($cid) {

      // Attempt to load files for an existing comment.
      $files = comment_upload_load_files($cid);
    }
  }
  else {

    // Using the Attach button without JS does not generate a preview.
    // Rape the comment form.
    // Comment_form was not build to rebuild a form and fetch values from $form_state.
    // To preserve changes made by the user, we build the part of the form we just received,
    // mapping $_POST values to #default_values.
    // Because rebuild is TRUE, $_POST has been validated. Nevertheless this is insane.
    $copy_form = $form;
    $copy_form['#post'] = $_POST;
    unset($copy_form['#post']['op']);
    unset($copy_form['#after_build']);
    $build_form = form_builder('comment_form', $copy_form, $state);
    comment_upload_value_to_default($build_form, $form);
    form_clean_id(NULL, TRUE);
    if (isset($form_state['storage']['comment_upload_storage'])) {
      $files = $form_state['storage']['comment_upload_storage'];
      if (count($files) > 1) {
        uasort($files, 'comment_upload_sort_files');
      }
    }
  }
  $form['#comment_upload_storage'] = $files;
  $form['attachments'] = array(
    '#type' => 'fieldset',
    '#title' => t('File attachments'),
    '#collapsible' => TRUE,
    '#collapsed' => count($files) == 0,
    '#description' => t('Changes made to the attachments are not permanent until you save this post.'),
    '#prefix' => '<div class="attachments">',
    '#suffix' => '</div>',
  );

  // Wrapper for fieldset contents (used by ahah.js).
  $form['attachments']['wrapper'] = array(
    '#prefix' => '<div id="attach-wrapper">',
    '#suffix' => '</div>',
  );
  $form['attachments']['wrapper'] += comment_upload_upload_form($files);
  $form['#attributes']['enctype'] = 'multipart/form-data';

  // Comment_form dynamically adds buttons such as preview / save in the
  // formbuilder. As the attachment fieldset contains an AHAH element, the
  // #cache property of the form will be set to TRUE and the formbuilder will
  // no longer be called. Therefore, comment_upload needs to implement preview
  // functionality as well.
  $form['#validate'][] = 'comment_upload_comment_form_validate';
}