I'm calling drupal_render to render a textarea element in another element's #prefix field in hook_form_customtype_node_form_alter(). When I load the form, most of the script tags in the header do not get printed. If I comment out the line with drupal_render they print out and the accordions and sliders load on the page.

Why would calling drupal_render prevent some of the javascript files from being included in the page header?


Here's my code:

$deltas = preg_grep('/^[0-9]*$/',     array_keys($form['field_recommendation_reference'][LANGUAGE_NONE]));
foreach ($deltas as $delta) {
  if ($form['field_recommendation_reference'][LANGUAGE_NONE][$delta]['field_key']['#default_value']['finding_wrapper'] == $wrapper_field_name) {
    $wrapper = $form[$wrapper_field_name];
    $wrapper['#printed'] = FALSE;
    $wrapper['#value'] = 'test';
    //$form[$field]['#suffix'] = drupal_render($wrapper);

When I output the element with dpm, the form loads all of the javascript. Also, when I include the markup without using drupal_render, the page loads correctly.

If I included the rendered markup manually the form doesn't load all of the javascript. Could it be a conflict with id's being used already? I have the same element stored in the $form array but with #printed set to TRUE.

Found the Issue

if I set #autocomplete_path = '', the form element's markup does not disrupt the rest of the page. Is it possible to get autocomplete to work with drupal render? I saw some comments related to this on the drupal_render api documentation

  • 1
    You shouldn't use drupal_render on a form element inside a form alter hook. The element will be rendered later on when the rest of the form is rendered, the form builder/alter functions are there to build the form. It's not that surprising that you're seeing strange behaviour when doing this
    – Clive
    Commented Apr 12, 2012 at 9:02
  • Is there a way to add the form element directly after the field I want. I was using a wrapper in the suffix to hold an empty div that I replace with ajax calls. When I load a form that has a value set, I want the element to appear in the prefix area.
    – Arosboro
    Commented Apr 12, 2012 at 13:17

1 Answer 1


Setting the #id to $wrapper['#id'] = drupal_html_id('edit-' . str_replace('_', '-', $wrapper_field_name)); resolved the issue. theme_textarea was unable to get a value for $element['#id'].


I still had trouble getting the #ajax elements to be processed even after adding a call to ajax_pre_render_element.

I restructured my alter hook so that it prints the textarea inside the form element that I'm trying to append to. that way it prints both fields one after the other. This way, I'm not touching drupal_render and everything works.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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