2

What is difference in functionality between drupal_render() and drupal_render_children() ? When I used drupal_render for $form derived as follows from hook_theme

function hook_form_alter($form,&$form_state,$form_id) {
if ($form_id == 'content_type_a') {
  $form['#theme'][] = 'custom_content_type_a_node_form'; 
}


}


function hook_theme() {
  return array(
    'custom_content_type_a_node_form' => array(
       'render element' => 'form',
    );

}

Then inside a THEMENAME_custom_content_type_a_node_form

function THEMENAME_custom_content_type_a_node_form(&$variables) {
$output = '';
$form = $variables['form'];

$output = <div>Some HTML code</div>;

$output .= drupal_render($form);

return $output;
} 

While using drupal_render in the above function the page was going into loop and function was being called by endlessly
But when I replaced that with drupal_render_children($form) it did not go into an endless loop
What is the reason for such a behavior?

3

drupal_render() renders an item. Rendering it contains theming it. Look into function's code, there is this line:

// Call the element's #theme function if it is set

So placing render inside theming function is bound to make a loop. drupal_render_children() exist to break this loop - it renders element's children only, so you can print element itself in theming function and still have children themed by their own proper functions.

| improve this answer | |
  • So is there a way out for using drupal_render instead of drupal_render_children? – harshal Dec 27 '13 at 10:27
  • 1
    @harshal nope, by design drupal_render will always call theming function, so cannot be used in it. Of course you could hack around it in a dozen ways, but it would take more work than simply switching to drupal_render_children and printing element itself manually. – Mołot Dec 27 '13 at 10:29
0

Actually this is a bug that is still haunting us in Drupal 8:

And likely it could still be backported to Drupal 7.

However currently using drupal_render_children() is the only way.

| improve this answer | |

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.