2

I'm trying to render a block in my module that contains both a form and a list of links. I can display one or the other correctly, but apparently do not understand the render array format well enough to get them both rendered at the same time (one above the other) in the same block. Using Drupal 7.4

Example for setting block content to show a list:

$block['subject']='Title';
$items= // code that generates a list of links into an array
$theme_args=array('items'=>$items,'type'=>'ul');
$block['content']=theme('item_list',$theme_args);
return $block;

Example for setting block content to show a form:

$block['subject']='Title';
$block['content']=drupal_get_form('mymodule_myform_function'); 
// call function that returns the usual form array  
return $block;

Each case works fine individually for me. How can I combine the form and the list into one block['content'] array so it can be rendered in one block? Thanks in advance.

peetucket
  • 23
  • 1
  • 4
  • there's an error, a $ preceding your second 'return $block'. I can't edit it, apparently an edit must change at least 6 characters... – charliefortune Mar 26 '13 at 15:49

1 Answers1

8

I think this should work, I haven't tested though:

$block = array(
  'items' = array(
    '#markup' => theme('item_list', $theme_args);
  ),
  'form' = drupal_get_form('mymodule_myform_function');
);
$block['content'] = $block;

It is a little counter-intuitive, drupal_get_form returns a form render array, however theme() returns markup.

You could always do this (terrible solution) but it isn't advised as its highly inefficient and goes against everything that Drupal intends for you to do:

$block['content'] = theme('item_list', $theme_args) . render(drupal_get_form('myform'));
Boaz
  • 19,892
  • 8
  • 62
  • 70
mattacular
  • 1,849
  • 13
  • 17
  • Thanks, the first solution works fine and makes sense after I read more about render arrays. What through me is that the fact that you can return a render array OR markup (or just a string) and it all works, but when you try and combine them into a render array, you need to use the '#markup' type for stuff that is already rendered (like theme output). You have one small typo in your code sample, which is "'form' = drupal_get_form" should be "'form'=>drupal_get_form". Cheers – peetucket Jul 23 '11 at 20:15
  • It's worth noting that in your render array example, the order in which you specify `items` and `form` determines the order in which they appear, top to bottom in the rendered HTML. – Lester Peabody Dec 10 '12 at 21:18