Drupal - Form within a block

As of Drupal 7, drupal_get_form() returns a render array instead of a string, so try using:

drupal_render(drupal_get_form('event_signup_form'));

to embed the form in the block. For information, see the section drupal_get_form() returns a render array instead of a string in the article Converting 6.x Modules to 7.x

Also, I believe your function event_signup_form($form, &$form_state) should just be event_signup_form() (without arguments).


My bet is that $form['#submit'][] = 'event_signup_form_submit'; is the culprit. You set that only if you want an extra submission handler.

This is a working sample code.

<?php
/**
 * Implements hook_block_info().
 */
function MYMODULE_block_info() {
  $blocks = array();

  $blocks['MYBLOCK'] = array(
    'info' => t('My block'), 
    'cache' => DRUPAL_NO_CACHE,
  );
  return $blocks;
}

/**
 * Implements hook_block_view().
 */
function MYMODULE_block_view($delta = '') {
  $block = array();

  switch ($delta) {
    case 'MYBLOCK':
      $block['subject'] = t('My block title');
      $block['content'] = drupal_get_form('MYFORM_form');
    break;
  }
  return $block;
}

/**
 * Custom form.
 */
function MYFORM_form($form, &$form_state) {
  $form['MYFIELD'] = array(
    '#type' => 'textfield',
    '#title' => t('Some Field'),
  );

  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Submit Button'),
  );

  return $form;
}

/**
 * Custom form submit function.
 */
function MYFORM_form_submit($form, &$form_state) {
  // You need the have Devel module enabled for dpm() to work.
  dpm($form_state);
}

Tags:

Forms

7

Blocks