1

I'm trying to get an AJAX-submitted (AHAH) form working to display in a block on the sidebar. For testing purposes, I'm using an example module called "Poof" from the Pro Drupal Development book: http://books.google.com/books?id=VmZrdGuBZCMC&lpg=PA269&ots=cnHiYG6kXn&dq=pro%20drupal%20development%20poof&pg=PA269#v=onepage&q=pro%20drupal%20development%20poof&f=false

The only thing I've added to the example so far is an implementation of hook_block, which looks like this:

  function poof_block($op = 'list', $delta = 0, $edit = array()) {
  switch ($op) {
    case 'list':
      $blocks[0]['info'] = t('poof');
      return $blocks;
    case 'view':
       $block['content'] =  drupal_get_form('poof_form');
       return $block;
  }
}

The AJAX module works fine when displaying on its own page (mydrupalsite.com/poof) but when I call the form with module_invoke('poof', 'block' ...) in a template file, the form submits as normal (sans AJAX) and refreshes the page.

I can't find a definitive answer for why this happens, though a found something tangentially related that suggests that maybe AHAH doesn't work within blocks. If that's so, why? Or better yet, what's a work-around. Do I have to put the on its own blank page and bring it in with an iframe? That sounds unnecessarily messy.

UPDATED: Here's more code for reference (again, it's from the Pro Drupal book)

function poof_form() {
  $form['target'] = array(
    '#type' => 'markup',
    '#prefix' => '<div id="target">',
    '#value' => t('Click the button below.'),
    '#suffix' => '</div>',
  );
  $form['submit'] = array(
    '#type' => 'button',
    '#value' => t('Click Me'),
    '#submit'=>false,

    '#ahah' => array(
      'event' => 'click',
      'path' => 'poof/message_js',
      'wrapper' => 'target',
      'effect' => 'fade',
    ),
  );

  return $form;
}

function poof_message_js() {
  $output = t('POOF!');
  drupal_json(array('status' => TRUE, 'data' => $output));
}
fredrover
  • 2,997
  • 2
  • 17
  • 24
  • I use AJAX module enabled forms within blocks, and they work just fine - so there is probably something wrong with AJAX modules 'attach to form' detection logic in your case. How do you configure AJAX module to attach to your form? – Henrik Opel Jul 18 '10 at 15:18
  • Thanks for your reply, Henrik. I'm not using the AJAX module directly here. I'm using Drupal's "AHAH" functionality in forms. I'm adding another snippet to my original post for clarity. Maybe the answer is that I have to write Ajax directly with jQuery. Since the AHAH method seems to work in a page context, though, i was hoping to get around that and use the same code in a block. – fredrover Jul 18 '10 at 20:43
  • 1
    Does the misc/ahah.js get included when you get the form in the block. – Jeremy French Jul 19 '10 at 07:33

1 Answers1

1

Try adding

$blocks[0]['cache'] = BLOCK_NO_CACHE;

to your hook_block implementation.

Rendering a form with ahah causes a call to drupal_add_js to add the ahah javascript, but while the output of the block is cached, the javascript that gets added to the page doesn't.

zroger
  • 211
  • 2
  • 3