Agaric Design Collective

How to theme a Drupal form

Average: 4.7 (21 votes)
By Benjamin Melançon
on 13 Mar
13 comments

Key words and phrases

Howto theme Drupal forms themeing input forms user_register with bio module

Tags

Description

Update: This tutorial has been revised significantly to provide a robust solution consistently works.

Define a function named your_theme_name_ or phptemplate_ plus name_of_form($form).

  • Tip: You can find out what the names of forms are by implementing hook_form_alter in a module and adding the code drupal_set_message("Form ID: " . $form_id);, as in the first commented out line in this example.

<?php
function phptemplate_user_register($form) {
   
$variables = array('user' => $user, 'form' => $form);
    return
_phptemplate_callback('user_register', $variables);
}
?>

Then create a .tpl.php file with that name.

In this case, user_register.tpl.php

We can use this PHP snippet:

<?php
drupal_set_message
('< pre >'. var_export($variables,TRUE) .'< /pre >');
?>

This provides us with the variable names (inside the $form variable) that are available to "drupal render" at your own chosen place.

  • Tip: To preserve the functionality of Drupal's catch-all-remaining form elements, you must pass the the form values in from template.php in a broader variables array (as described above). This allows you to use a form variable with form element names as keys, rather than using form names directly. Form names directly will build the form, but without the ability to drupal_render all remaining form elements, and several hidden ones are essential to a functioning Drupal form.

Example of a form theming .tpl.php file:

<div id="regform1">
<fieldset class="group-account-basics collapsible"><legend>Account Basics</legend>

<?php print drupal_render($form['name']); ?>

<?php print drupal_render($form['mail']); ?>

<?php print drupal_render($form['pass']); ?>

<?php print drupal_render($form['field_user_picture']); ?>

<?php print drupal_render($form['field_city']); ?>

</fieldset>
</div>


<div id="regform2">
<fieldset class="group-story collapsible"><legend>My Story</legend>

<?php print drupal_render($form['field_storypic']); ?>

<?php print drupal_render($form['field_user_story_field']); ?>

</fieldset>
</div>

<?php print drupal_render($form['captcha']); ?>

<?php print drupal_render($form['submit']); ?>

<?php
unset($form['field_mostimportantlesson']);
print
drupal_render($form);
?>

Note the last step: we unset any fields we don't want to show, and then print the remainder of the form. If you drupal_render($form) outputs something you don't want or in a place you don't want it, simply unset that field or drupal_render that field specifically where you do want it.

Modifying individual fields

That's great, we can put fields in any order, surround them with any markup we want, and skip ones we don't want. What about changing the output of a given field?

We will only discuss here the simple way, very similar to form_alter, yet powerful enough to almost certainly meet all your needs with the addition of CSS.

In this case, we want to do something special with the age at diagnosis field.

To see what we're doing and make an initial attempt which proved incorrect, we added this to our user_register.tpl.php file in our theme's directory:

<?php
drupal_set_message
('<pre>'.print_r($form['field_ageatdiagnosis'],TRUE).'</pre>');
$form['field_ageatdiagnosis']['#suffix'] = ' '. t('years');
print
drupal_render($form['field_ageatdiagnosis']);
?>

That printed "years" at the end of the whole form, not what we had hoped. We will be able to refine our attempt based on the output of drupal_set_message print_r on the field, below:

Array
(
    [#tree] => 1
    [0] => Array
        (
            [value] => Array
                (
                    [#type] => textfield
                    [#title] => Age at Diagnosis
                    [#default_value] => 
                    [#required] => 0
                    [#description] => 
                    [#size] => 20
                    [#maxlength] => 11
                    [#attributes] => Array
                        (
                            [class] => number
                        )

                    [#field_prefix] => 
                    [#field_suffix] => 
                    [#post] => Array
                        (
                        )

                    [#programmed] => 
                    [#tree] => 1
                    [#parents] => Array
                        (
                            [0] => field_ageatdiagnosis
                            [1] => 0
                            [2] => value
                        )

                    [#weight] => 0
                    [#processed] => 
                    [#input] => 1
                    [#autocomplete_path] => 
                    [#name] => field_ageatdiagnosis[0][value]
                    [#id] => edit-field-ageatdiagnosis-0-value
                    [#value] => 
                    [#sorted] => 1
                )

            [#post] => Array
                (
                )

            [#programmed] => 
            [#tree] => 1
            [#parents] => Array
                (
                    [0] => field_ageatdiagnosis
                    [1] => 0
                )

            [#weight] => 0
            [#processed] => 
            [#sorted] => 1
        )

    [#post] => Array
        (
        )

    [#programmed] => 
    [#parents] => Array
        (
            [0] => field_ageatdiagnosis
        )

    [#weight] => 0.011
    [#processed] => 
    [#sorted] => 1
)

Based on this information, we revised our PHP to set #field_suffix:

<?php
$form
['field_ageatdiagnosis'][0]['value']['#field_suffix'] = t('years');
$form['field_ageatdiagnosis'][0]['value']['#attributes'] = array('class' => 'number inline');
print
drupal_render($form['field_ageatdiagnosis']);
?>

We could set #title and #size the same way.

As shown above, we can even add new classes by modifying the #attributes value (noting that in this case the number class was set in the #attributes array, and we would not want to unset it, so we list it again while adding the class inline):

Note: If you look at the HTML output for a form field, you may see additional classes. For instance, 'form-text' is supplied automatically by Drupal form_rendering, and does not need to be restated when adding a class attribute. Using the drupal_set_message('<pre>'.print_r($form['field_ageatdiagnosis'],TRUE).'</pre>'); trick to see what's there is a very good idea to make sure you don't overwrite any important attribute.

References:

 

Themeing/Displaying form_set_error messages

Thanks for the great article. I have some problems displaying the form_set_error messages in my new form template -- is there an easy to have them display?
dp

Posted by dario (not verified) on Sat, 2008-03-15 11:54
Make sure you are printing the $messages variable

I'm pretty certain form_set_error messages uses the standard drupal_set_message system, and so will show up so long as your theme outputs the $messages variable in page.tpl.php.

For example, this is from bluemarine's page.tpl.php file:

      <div id="main">
        <?php print $breadcrumb ?>
        <h1 class="title"><?php print $title ?></h1>
        <div class="tabs"><?php print $tabs ?></div>
        <?php print $help ?>
        <?php print $messages ?>
        <?php print $content; ?>
        <?php print $feed_icons; ?>
      </div>

Update: absolutely certain

Posted by Benjamin Melançon on Fri, 2008-05-23 15:13
missed $messages

I used this example and thank you for it.

I had a little trouble getting $messages to show up when the form had a validation error (ie using form_set_error()).

I created a content type for form variables, then called page.tpl.php to build a form based on the CCK data. The errors wouldn't show up until the second time I submitted the form with the validation problem. The problem seems similar to http://drupal.org/node/304556 and I'm unsure of how to resolve it correctly.

I suppose that calling from form.tpl.php from settings.php from drupal_get_form() from page.tpl.php was messing with the session and $messages. So instead I loaded the template as a function call from hook_menu that sends the whole thing through theme('page'). This works, but it means my page.tpl.php can't mix up $messages in the middle of the page content the way I wanted to.

I don't know if it matters, but in my original page.tpl.php I wasn't calling $content, just building the form out of $node, so maybe a better approach would have been to call hook_nodeapi().

Do you have any thoughts about how to go about this the Right Honorable Drupal way?

Posted by caleb (not verified) on Wed, 2009-04-15 02:04
the big question...

The $50,000 dollar question is, how do you achieve this in Drupal 6.x ?

Looking back, D5 was cake. That's progress for ya...

Posted by xenu (not verified) on Mon, 2008-11-17 16:54
For $50,000 I answer that tonight!

Otherwise, it'll keep until we need it too ;-)

But the belief is that most everything theming-wise from Drupal 5 is still possible in 6, and frequently easier... I take it you've tried?

Posted by Benjamin Melançon on Mon, 2008-11-17 19:03
D6 custom node edit form

Yes I tried. I searched for clear articles and asked for help in chat, to no avail.

Outputting a custom content type in D6 is easy. Just create node-my_content_type.tpl.php and bam!, it works.

Controlling the edit form is not so simple...

In D6, you cannot add a snippet to the template.php and create node-my_content_type-edit.tpl.php. Apparently its much more complicated. I yearn for the good ol' days of D5. Great tutorial by the way.

If you know how to create a custom content edit form in D6 please share!

Adieu.

Posted by xenu (not verified) on Tue, 2008-11-18 15:46
Of course you can theme forms in Drupal 6!

The best tutorial we've seen is Caroline Schnapp's Theming the register form in Drupal 6 which is the same process for theming any form, including node edit forms (I think the use of hook_theme, or rather THEMENAME_theme, is necessary for theming a custom node type's edit form.)

For theming forms in general, Agaric Design Collective's own Dan Hakimzadeh already had a Drupal 6 how-to up: theme the search form in Drupal 6.

The first example is like Dan's, and may not work for custom node forms. The second will work for any form no matter where a form is defined. In both, replace capitalized terms (EXAMPLETHEME, YOUR_FORM_ID) with your own values:

<?php
function EXAMPLETHEME_YOUR_FORM_ID($form) {
 
// go wild
}
?>

or register your own. This will allow you to use a template file to theme your form:

<?php
function EXAMPLETHEME_theme() {
  return array(
   
'YOUR_FORM_ID' => array(
     
'arguments' => array('form' => NULL),
     
'template' => 'your-form-id'// following convention, replace underscores with dashes
   
)
  )
}
?>

Form elements' theming can still be overridden with #theme.

More Drupal 6 form theming resources:

http://api.drupal.org/api/group/themeable/6
http://api.drupal.org/api/file/developer/topics/forms_api_reference.html...

Emma Jane's slides:
http://www.slideshare.net/emmajane/theming-drupal-6x-forms-presentation

Posted by Benjamin Melançon on Wed, 2008-11-26 05:15
I am unclear as to how to

I am unclear as to how to specify a specific content type 'node_form' for themeing in D6 with the function call in template.php when all of the forms appear to be 'node_form'. how do I tell the function to just use the new .tpl file for a specific content-type 'node_form' only?

Posted by Visitor (not verified) on Sun, 2008-12-14 17:14
form submit is not working while using custom user-register.tpl.

Hi,

I have used customization in user registration page

my code in template.php is

function CUSTOMTHEME _theme($existing, $type, $theme, $path) {
return array(

// tell Drupal what template to use for the user register form
'user_register' => array(
'arguments' => array('form' => NULL),
'template' => 'user-register', // this is the name of the template
),

);
}

and in user-register.tpl.php is

<?php
     
print drupal_render($form['name']); // prints the username field
  
?>

<?php
     
print drupal_render($form['mail']); // print the password field
  
?>

<?php
       
print drupal_render($form['submit']); // print the submit button
    
?>

But the forms are displaying ( username + email + submit ) in q=user/register url

but while new user register submit button hits the values are not stored in database any solution plz help me to solve this issue

Posted by A jeya Sankar (not verified) on Mon, 2009-05-11 01:47
This was already covered in the post

Unset what you do not want, and print the rest of the form with drupal_render($form) -- or else hidden elements that the form needs to work are not included.

Posted by Benjamin Melançon on Fri, 2009-05-15 23:03
phptemplate_ name_of_form($form) is not working in my system

Hi Benjamin Melançon,

phptemplate_ name_of_form($form) is not working in my system.

Below is my code in template.php

function cricdee_comment_form($form) {
print_r($form);
exit;
}

when the page renders, this function is not getting called.
Can u pls tell me the issue with my code.

Posted by Raghunath (not verified) on Wed, 2009-05-13 09:40
Assuming that cricdee is

Assuming that cricdee is your theme name, then you are probably using Drupal 6 and should see the comment a little above yours, on theming forms in Drupal 6.

Posted by Benjamin Melançon on Fri, 2009-05-15 23:07
Nice explanation.......

I would be really interested to learn more from your up coming tutorial. please keep me inform cheers!

Posted by kaay (not verified) on Fri, 2009-05-22 15:50
Post new comment
The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <blockquote> <h1> <h2> <h3> <h4> <h5> <h6> <small> <pre> <strike> <sub> <sup> <kbd> <s>
  • Lines and paragraphs break automatically.
  • Web page addresses and e-mail addresses turn into links automatically.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Copy the characters (respecting upper/lower case) from the image.