I have been reading up on trying to override the rendering of certain elements in my form but am making a mistake somewhere.

I have a generic node type created and inside the hook_form element have placed the following code...

 $form['#theme'] = '_theme_game_form';

Shouldn't this tell Drupal to send the form through this function before outputting to the screen?

Well it isn't so I must be making a mistake.

How can I tell drupal that I want to intercept the form and theme specific elements before outputting to a page?

Comments

gte451f’s picture

Anyone have any idea?

Thanks!

gte451f’s picture

I just don't get it guys.

I have looked in to
hook_form_alter but it looks like that is for adding additional field elements.

I have looked into the examples provided by upload.module and related links but in both cases, it is an outside module acting on a node based module.

I am creating a node based module and I want to override the default rendering logic of some particular form elements I outlined back in
hook_form.

Should I set the theme of the form in hook_form_alter?

Some supplement reading for anyone following along....
http://drupal.org/node/40947
http://drupal.org/node/37775

heine’s picture

Not much time, so a reference to a small Howto: Theme Drupal forms with PHPTemplate. The example is about story_node_form.

You should also be able to theme the form with a function theme_form_id($form) in your module eg theme_yournodetype_node_form (haven't tested).

--
When your problem is solved, please post a follow-up to the thread you started.

gte451f’s picture

Thanks for tip. This does help alittle but I must confess I have already started looking in this direction. Overall, it seems cleaner from a design perspective to confine theming to .tpl files but in this example, the level of granularity was the fieldset. Put another way, I can only move a field set around, how do I start controlling how field elements (check box, textfield) get placed within the fieldset?

heine’s picture

It's difficult to render children before a visible containing parent.

In the example, suppose you also wanted to swap the 'Authored by' and 'Authored on field' and wrap the Authored by field in a div one way to do this is:

  $form['author']['date']['#weight'] = -2;  
  $form['author']['name']['#prefix'] = '<div class="foo">';   
  $form['author']['name']['#suffix'] = '</div>';
  print form_render($form['author']);
  print form_render($form); 

--
When your problem is solved, please post a follow-up to the thread you started.

gte451f’s picture

Yes this is less then ideal but we're getting somewhere. I stumbled on another approach that also has some drawbacks.

<table width="600px" >
<tr>
<td colspan="4"><?php print_r(form_render($form[flash_details][flash_asset]));?></td>
</tr>
<tr valign="top">
<td><?php print_r(form_render($form[flash_details][flash_height]));?></td>
<td><?php print_r(form_render($form[flash_details][flash_width]));?></td>
<td><?php print_r(form_render($form[flash_details][flash_bg]));?> </td>
<td><?php print_r(form_render($form[flash_details][flash_version]));?></td>
</tr>
</table>
<?php  print form_render($form['flash_detials']); ?>

This method allows one to render individual form elements but if they are within a fieldset you could run into problems. In this case, I have extracted the field elements one by one to display horizontally in a table. Still what do do with that pesky field set? I suppose it still has to show up but it will appear as empty now.

Perhaps I can just recreate my module to forego fieldsets and "roll my own" but why should we have to settle!
I seem to remember there being an attribute that dictates if a form element has been displayed. Perhaps we could just disable the fieldset.

Another approach would be to use the prefix/suffix attributes to create some sort of table like effect.
I could go with inline divs as another alternative.

  $form['author']['name']['#prefix'] = '<div style="position:inline;">'; 
  $form['author']['name']['#suffix'] = '</div>';

*Forgive my poor CSS skills!

Still even the CSS approach means that I would either commit other users to my CSS or put the burden on them to create styles they liked.

heine’s picture

To render the children of a fieldset but not the fieldset itself use the following code. Example is from story form; here we're trying to render all the fields contained in the fieldset, but not the fieldset itself.

// Render all children of the fieldset author
foreach(element_children($form['author']) as $key) {
  print form_render($form['author'][$key]);    
}

// Now set the fieldset to 'value' to prevent display
$form['author']['#type'] = 'value';

// Render the rest of the form
print form_render($form);

--
The Manual | Troubleshooting FAQ | Tips for posting | Make Backups! | Consider creating a Test site.