The comment form builder adds an #after_build function when $op == t('Preview'). This is already obsolete, but when adding an AHAH element, #cache is set to TRUE and the form_builder is not invoked on submission. This breaks previews when AHAH elements are added.

CommentFileSizeAuthor
#3 comment_preview_fapi3.patch5.96 KBchx

Comments

heine’s picture

Here's a steps to reproduce; enable the test module below, visit the comment form, fill the comment body and click preview. There will be no preview.

If you comment out "'path' => 'test/js',", visit the comment form again (don't reload!), fill the comment body and click preview, you'll notice a preview will be shown.

In form.inc: 1760 the AHAH process handler sets $element['#cache'] = TRUE;
Then form_builder sets this for the entire form:

  // If some callback set #cache, we need to flip a static flag so later it
  // can be found.
  if (isset($form['#cache'])) {
    $cache = $form['#cache'];
  }
  // We are on the top form, we can copy back #cache if it's set.
  if (isset($form['#type']) && $form['#type'] == 'form' && isset($cache)) {
    $form['#cache'] = TRUE;
  }

Test module, the preview behaviour is what interest me.

function test_menu() {
  $items['test/js'] = array(
    'page callback' => 'test_js',
    'access arguments' => array('access content'),
    'type' => MENU_CALLBACK,
  );
  return $items;
}

function test_form_alter(&$form, $form_state, $form_id) {
  if ($form_id == 'comment_form') {
    $form['wrapper'] = array(
      '#prefix' => '<div id="attach-wrapper">',
      '#suffix' => '</div>',
    );
    $form['new']['attach'] = array(
      '#type' => 'submit',
      '#value' => t('Attach'),
      '#name' => 'attach',
      '#ahah' => array(
        'path' => 'test/js',
        'wrapper' => 'attach-wrapper',
        'progress' => array('type' => 'bar', 'message' => t('Please wait...')),
      ),
      '#submit' => array('test_button_submit'),
    );
  }
}

// Filler
function test_button_submit($form, $form_state) {  
  drupal_set_message('<pre>'. check_plain(print_r($form_state, TRUE)) .'</pre>');
}

// Filler
function test_js() {
  drupal_set_message('TEST JS');
  print drupal_to_js(array('status' => TRUE, 'data' => ''));
  exit;
}
chx’s picture

Status: Active » Needs review
StatusFileSize
new5.96 KB

I am afraid this is too big for D6 but here is an attempt to fix this.

birdmanx35’s picture

Bingo: Still works against HEAD.

mfb’s picture

Subscribing.

sun’s picture

Status: Needs review » Closed (works as designed)

This has been fixed for D7 already, but cannot be backported.