I have some elements in a fieldset. Now I want to use theme_my_form to do the layout work. But I cannot separate the elements from their fieldset...

If I do not render the fieldset, everything is ok. If I render the fieldset, all elements in it will be follow the default layout... all elements rendered with the fieldset together I think...

How to deal with it?

Comments

traxer’s picture

Rendering the children first allows you to theme them. You can then put the markup for the fieldset around your children manually and call form_render() on the fieldset (disregarding the output) to mark it rendered.

Greetings
--
~/.singatrue: file not found

zhangx1a0’s picture

thanks for your reply. But I am not very clear about your words. Did you mean that I should render children first then render fieldset after children? I tried this way. But in the output page, children elements are not in the fieldset but under the fieldset...

traxer’s picture

Say you want to render the fieldset $form['my_fieldset']. Do it like this:

  $my_fieldset = '';
  $children = element_children($form['my_fieldset']);
  foreach ($children as $child) {
    $my_fieldset .= form_render($form['my_fieldset'][$child]); // renders a single child
  }
  form_render($form['my_fieldset']); // marks the fieldset as rendered
  $output .= '<fieldset class="collapsible"><legend>' . t($form['my_fieldset']['#title']) . '</legend>'
    . '<div class="description">' . t($form['my_fieldset']['#description']) . '</div>'
    .  $my_fieldset . '</fieldset>'; // creates the output
  $output .= form_render($form); // renders all remaining elements.
  return $output;

This should give you about the same output as the standard rendering. Now if you want to render the children differently you can change the foreach-loop that iterates over the children and add to $my_fieldset. Remember to call form_render on the children as well, just like I did form_render($form['my_fieldset']) without remembering the result.

BTW: This seems to be a hack, I've got the feeling there exists a better way.

Greetings,
--
~/.singatrue: file not found