1

I'm working on a module in Drupal 7. I have three different forms set up with hook_form. I would like to display the three forms on one page along with some html to organize the forms and apply some css to change the look a little bit. What I have right now is:

  function floormap_setup_page() {
    $info[0] = '<div id="floormap_setup">';

    $info[0] .= '<div id="select_floormap">';
    $info[0] .= '<ul>';
    $floormap_options = get_floormap_options();
    $floormap_list = '';
    foreach ($floormap_options['name'] as $key => $value){ 
        $span_delete = '<span class="delete"><a href="#"> ' . t('Remove Floor Map') . ' </a></span>';
        $span_default = '<span class="default"><a href="#"> ' . t('Make Default') . ' </a></span>';

        $floormap_list .= '<li>' . $value . $span_default . $span_delete . '</li>';
        }
    $info[0] .= $floormap_list;
    $info[0] .= '</ul>';
    $info[0] .= '<div><!--select_floormap-->';

    $info[0] .= '<div id="label_cameras">';
    $info[1] = drupal_get_form('setup_cameras_form');
    $info[2] = '</div><!--label_cameras-->';

    $info[2] .= '<div id="set_expectations">';
    $info[3] = drupal_get_form('set_expectations_form');
    $info[4] = '</div><!--set_expectations-->';

    $info[4] .= '<div id="new_floormap">';
    $info[4] .= '</div><!--new_floormap-->';

    $info[4] .= '</div><!--floormap_setup-->';
  return $info;
}

Doing this displays the page how I want it, the only problem is that I get a bunch of errors along with it..

The errors look like this:

Warning: Illegal string offset '#children' in drupal_render() (line 5887 of /var/www/app.local.nucloudglobal.com/includes/common.inc).

Warning: Illegal string offset '#children' in drupal_render() (line 5897 of /var/www/app.local.nucloudglobal.com/includes/common.inc).

Warning: Illegal string offset '#children' in drupal_render() (line 5935 of /var/www/app.local.nucloudglobal.com/includes/common.inc).

Warning: Illegal string offset '#printed' in drupal_render() (line 5942 of /var/www/app.local.nucloudglobal.com/includes/common.inc).

The entire set of errors repeats for each item in the $info array.

  • 1
    Read Render Arrays in Drupal 7 – Felix Eve Apr 14 '14 at 15:45
  • @FelixEve Thank you. This does supply information about rendering a page using arrays, however it doesn't really help much with rendering a form along with markup. Which is the real issue here. From googling a little, I see that I may be able to use form as a #type, but can't find a definite list of #types that are acceptable, nor can find where I would put the name of the form to call. – slpcc63 Apr 14 '14 at 16:21
2

Render arrays look like this:

$build = array(
  '#prefix' => '<div id="floormap_setup">',
  '#suffix' => '</div>',
  'select_floormap' => array(
    '#markup' => '<p>Markup for the first bit</p>',
  ),
  // drupal_get_form() returns a render array.
  'setup_cameras_form' => drupal_get_form('setup_cameras_form') + array(
    '#prefix' => '<div id="label_cameras">',
    '#suffix' => '</div>',
  ),
  'expectation_form' => drupal_get_form('set_expectations_form') + array(
    '#prefix' => '<div id="set_expectations">',
    '#suffix' => '</div>',
  ),
  'new_floormap' => array(
    '#markup' => '<div id="new_floormap"></div>',
  ),
);

See the render array docs and What are "#" properties? for a bit more explanation.

0

You can directly render the form yourself before assigning it to the array

$info[3] = drupal_render( drupal_get_form('set_expectations_form'));

eventually you can just implode the array and return the resulting string, this way you can also avoid to deal with rendering arrays.

  • You shouldn't try to avoid render arrays - they're a fundamental part of core and contrib code, and it makes sense to use them as much as possible. It's also good practice for Drupal 8, which uses them even more (no more theme() function, for example) – Clive Apr 14 '14 at 18:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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