User login

Theming node creation forms to split up taxonomy input fields by vocabulary

The otherwise excellent documentation Using Theme Override Functions For Forms doesn't work too well for core forms like the node form. What that documentation is actually about is creating your own overridable theme functions for your own custom forms.

It's a somewhat different approach to overriding core forms. And even though you may have a custom content type "custom" you cannot override its form with theme_node_custom_form, sadly.

Instead, you must override the core theme function theme_node_form be renaming it phptemplate_node_form or somesuch.

Why is it whenever I find myself trying to do something a little bit crazy the only person who's done it before is Nick Lewis?

Here we are again, this time theming node forms.

His tutorial, Overriding Themeable Functions: The Where's, the Why's, and the How's is from Drupal 4.7, so we'll be following the same investigative procedures but for Drupal 5 (OK, Agaric admits it: we'll really be multiplying everything he does by .. um... ok, we'll add .3 to everything he does).

We will be throwing a bit of a twist into the approach for overriding the node form: we'll dynamically call for a node_whatever_form template if it exists, otherwise falling back on rendering the form the usual way. (This should be checked for performance impact, but it shouldn't be much.) This should be more straightforward in Drupal 6 because the goal is to have all output in template form, so you can copy the template rather than theme functions.

And our goal with all this is to be able to split up where taxonomy vocabularies show up on the form, because as far as Agaric's experimentation has uncovered you can't

By the way, Drupal Pro Development recommends for forms tips and tricks: http://drupal.org/node/37775 We'll report back there.

And that is not the place to look for theming: this page (link may break, api.drupal.org seems to be shifting sands) has the basics of everything forms, including theming: http://api.drupal.org/api/file/developer/topics/forms_api.html

Also to check: if contemplate messes with node creation forms, or only node output.

Proof of concept:

<pre>
function phptemplate_node_form($form) {
  $output = "Hollow Wourld";
  $output .= drupal_render($form); // Process any other fields and display them
  return $output;
}
</pre>

Note that in Drupal 5 that's drupal_render() not form_render()

OK, let's see what we're dealing with:

<pre>
function phptemplate_node_form($form) {
global $user; if ($user->uid==1) drupal_set_message('&lt;pre&gt;' . print_r($form, TRUE) . '&lt;/pre&gt;');
  $output .= drupal_render($form); // Process any other fields and display them
  return $output;
}
</pre>

Key parts of that output were what the title was called (title), what the description was called (body_filter), and what the vocabulary we were interested in was (looking at 'taxonomy' and then looking past all the 'tags' code for our two free tagging vocabularies, '6,' which is what we'd already confirmed from admin/content/taxonomy). Putting it all together, some very basic theming of the form that successfully splits up our taxonomy, to put the one that we're requiring right at the top.

Resolution

Commit message: "put the action status (vocabulary 6) right after the description, and let everything else fall where it usually does."

<pre>
function phptemplate_node_form($form) {
  $output = '';
  $output .= drupal_render($form['title']);
  $output .= drupal_render($form['body_filter']);
  $output .= drupal_render($form['taxonomy'][6]);
  $output .= drupal_render($form); // Process any other fields and display them
  return $output;
}
</pre>

Searched words: 
theme form taxonomy drupal theme_my_test_form($form) Drupal theme node form node add form edit

Comments

Post new comment

The content of this field is kept private and will not be shown publicly.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.
  • You can use Markdown syntax to format and style the text. Also see Markdown Extra for tables, footnotes, and more.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <img> <blockquote> <small> <h2> <h3> <h4> <h5> <h6> <sub> <sup> <p> <br> <strike> <table> <tr> <td> <thead> <th> <tbody> <tt> <output>
  • Lines and paragraphs break automatically.

More information about formatting options

By submitting this form, you accept the Mollom privacy policy.