0

Long story short when doing some basic theming on a drupal form the default / previously saved values refuse to display within the form fields (works without theming of course). In the code exerpt below we are simply taking some for fields and sticking them into a drupal table to better organize the overall page. This is within the hook_form_alter() framework.

$form['top'] = array(
  '#theme' => 'table',
  '#header' => array(array('data' => $form['#node']->title, 'style' => 'text-align: center; font-size: 1.6em; color:#333399; margin:1em 0; background: transparent; border: 1px solid #FFF; border-bottom: 3px solid #CCCCCC;', 'colspan' => 2,),),
  '#rows' => array(array(drupal_render($form['title']), drupal_render($form['field_order_id']),),),
  '#weight' => -8,
); 

The form correctly outputs the form items and submits correctly, all that is missing is the default / previously saved values. Nothing around the internet seems to have a workable solution either.

1

See examples for developers. You should not theme form in form alter hook. Do it in hook_theme(). In a module it looks like that:

function yourmodule_theme() {
  return array(
    'yourmodule_form' => array(
      'render element' => 'form',
      'file' => 'forms/yourmodule_form.inc',
    ),
  );
}

If you move elements around in form_alter(), they became different fields. If #tree is set to false (default), it does not interfere with submit, but it may interfere with defaults and validation.

| improve this answer | |
  • wouldn't you run into the same issue within the template? Where the rows include drupal_render() and the default values are lost? – DivineDestiny188 Jun 17 '13 at 8:00
  • drupal_render in form alter is pretty weird already. form_alter should only restructure form, never try to output anything by itself. – Mołot Jun 17 '13 at 8:01
  • Just a side note but nothing has been outputted in the form alter, its all just be restructured into the tables. No print or return statements. I can see your point though, just not sure how moving it over to a template file is going to change the behavior of drupal_render() nested within a drupal theme table – DivineDestiny188 Jun 17 '13 at 8:11
  • @DivineDestiny188 drupal_render is meant as an output function. If you put it into an form array earlier than in output, you are creating new markup elements. And markups can't have values set. Don't you ever use rendering functions in form alter unless you are damn sure you know what you are doing. – Mołot Jun 17 '13 at 8:16

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.