1

How do I render a form, which is part of a renderable array?



In .module

/**
 * Implements hook_block_view();
 */
function bibdk_vejviser_block_view($delta = '') {

  switch ($delta) {
    case 'bibdk_vejviser':
      $block['title'] = t('Find Library');
      $block['content'] = array(
        'link' => array(
          '#type' => 'link',
          '#title' => t("Find library"),
          '#href' => 'http://example.org',
        ),
        'form' => drupal_get_form('bibdk_vejviser_form'),
      );
      break;
  }
  return $block;
}



In custom block .tpl

// This will work (renders both elements)
print $content;

// This will also work (renders link)
print render($elements['link']);

// This will NOT work (renders nothing)
print render($elements['form']);



What am I doing wrong?



UPDATE: It works if I wrap drupal_get_form() in an array. Why is that??

...
'form' => array(drupal_get_form('bibdk_vejviser_form')),
...
0

1 Answer 1

1

It should work when you wrap drupal_get_form with drupal_render

Something like that:

...
'form' => array(
'#markup' => drupal_render(drupal_get_form('bibdk_vejviser_form'))),
...

Hope this works... Muhammad.

1
  • Hmm.. it's not working here. And besides - the solution would return a string, which would not be true to the render array philosophy. In my solution you would be able to alter the form with hook_block_view_alter too.
    – tolborg
    Oct 17, 2012 at 23:53

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.