tolborg
tolborg

Reputation: 612

Drupal: Render form in block tpl

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

Upvotes: 1

Views: 2059

Answers (1)

Muhammad Reda
Muhammad Reda

Reputation: 27043

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.

Upvotes: 1

Related Questions