Dear community,
I am creating a module using D7, and I want to display a form in a block with some custom theming. Here is what I have for the functions in foo_module.module (skipped the parts that I think do not matter for my issue).

function foo_module_theme($existing, $type, $theme, $path) {
  return array(
    'foo_login' => array(
      'variables' => array('form' => NULL),
    ),
  );
}

function foo_module_block_view($delta = '') {
  $block = array();
  switch ($delta) {
  case "login":
    $form = drupal_get_form('foo_login_form');
    $block["subject"] = "Login";
    $block["content"] = $form;
    break;
  }
  return $block;
}

function theme_foo_login_form($variables) {
  $form = $variables['form'];
  // Some custom theming
  $output = drupal_render_children($form);
  return $output; 
}

function foo_login_form($form, &$form_state) {
  $form['foo_username'] = array(
    '#type' => 'textfield',
    '#size' => '15',
    '#id' => 'foo_username'
  );
  $form['foo_password'] = array(
    '#type' => 'password',
    '#size' => '15',
    '#id' => 'foo_password'
  );
  $form['#theme'] = 'foo_login_form';
  return $form;
}

I am getting "Invalid argument supplied for foreach() in element_children() (line .... in .../common.inc)", And the form is not displayed at all.

After debugging with eclipse brings to the fact, that the "theme_" function does not receive the $varialbles['form'] the way it is supposed to, and the $form variable is empty. Later on, when I put a breakpoint in theme_form(), I see that it's invoked only after theme_foo_login_form() and already with the correct $variables['element'], which is the $form array I create in foo_login_form(). I have also tried using 'render element' => 'form' in hook_theme(), but that doesn't help..

Any suggestions would be deeply appreciated!
Vahe

Comments

Anonymous’s picture

I have exactly the same problem (except I'm using a template file rather than a theme function), does nobody know how to fix this bug? This is the only Drupal page I can find relating to it.

Any help anyone?

supertrader6’s picture

Install the Examples for Developers module and look at render_example / display blocks.

David.D’s picture

It is usefull for me ,thanks for your nice share

liquidcms’s picture

yea, i don't see much in the form_example that explains theming forms and there is no display blocks example.

supertrader6’s picture

I find the render_example useful for understanding Drupal 7.

dfletcher’s picture

I was hitting this problem when updating a module from 6, and the various full form theming examples here were not 100% appropriate. My use case needs #theme on a sub element (well, several really) that's not the main form itself. So the #theme parameter is required and must specify a theme function. I also seeing the very odd behavior of $variables['form'] looking incorrect - an empty array like the default I was giving in hook_theme.

After reading the liquidcms example above (thanks!) I realized my theming function was incorrect:

function mymod_theme($existing, $type, $theme, $path) {
  return array(
    'field_edit_config' => array(
      'variables' => array('form' => array()),
    ),
  );
}

That worked fine in 6, but in 7 this is required:

function mymod_theme($existing, $type, $theme, $path) {
  return array(
    'field_edit_config' => array(
      'render element' => 'form'
    ),
  );
}

After that my old theme functions worked great. Since they are sub-element theming, didn't even hit the issue with drupal_render()/drupal_render_children() and the old theme implementations work great without very many changes.