15

I'm using drupal_get_form('event_signup_form') to embed my custom form inside a block. The form is being rendered as intended, but submitting the form triggers nothing.

The form function itself is called event_signup_form($form, &$form_state) and the submit function is called event_signup_form_submit($form, &$form_state) but it's not being called.

I am using hook_block_view to show the form:

function event_block_view($delta = '') {
$blocks = array();
if ($delta ...) {
$blocks['content'] = drupal_render(drupal_get_form('event_signup_form'));
}
}

I managed to get this working - here is how (stub):

function hook_block_view($delta = '') {
$blocks['content'] = drupal_render(drupal_get_form('event_signup_form'));
}

function event_signup_form() {
// Register form submit handler, same goes for validation handler
$form['#submit'][] = 'event_signup_form_submit';

return $form;
}

function event_signup_form_submit($form, &$form_state) {
// This is now triggered
}

2 Answers 2

15

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).

5
  • But from within hook_block_view($delta = '') api.drupal.org/api/drupal/modules--block--block.api.php/… I am returning an array, I'm not printing anything. Commented Nov 10, 2011 at 7:34
  • 4
    I made a tool that creates block code that you can place in your module: browse-tutorials.com/tools/drupal/create-block
    – ram4nd
    Commented Nov 10, 2011 at 9:29
  • @Kristian ok, does it work with the code you edited into your question?
    – Laxman13
    Commented Nov 10, 2011 at 13:58
  • Yes I did manage to get it working, I'll add an update to the question Commented Nov 11, 2011 at 11:11
  • While this may work (sort of) for the edited question, it is not the right way to put a working form within a block. You want a render array, not a string. You also want to have the arguments (as prescribed by the Form API) for hook_form(). See answer by Francisco Luz for correct answer. Commented Jan 26, 2019 at 13:01
12

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);
}
3
  • 1
    An excellent example!
    – leymannx
    Commented Nov 9, 2015 at 13:39
  • 1
    Great example. I didn't need the custom form submit, but the first two functions put my custom form in a block, which is what I was looking for when I found this page. Thanks. Commented Jul 23, 2016 at 20:00
  • I think this should be the accepted answer. Commented Jan 26, 2019 at 12:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.