Overriding Form Functions from templatephp

^ ^ For a list of themeable functions applicable to forms, see Chapter 4. ^

You can create your own functions for forms, thereby overriding the original function and giving you the opportunity to apply modified styling or add additional HTML. Basic function overrides can either be placed in the template . php file, or made the subject of dedicated template files. In this section, we will discuss the former technique; in the next section, the latter.

As in so many other areas of the Drupal system, the naming convention is the key to the system recognizing the presence of your function override. Use the following naming convention for your new function:

function theme_form_id($form)

As an example: The form ID of the Login Block Form is user login block. Accordingly, if you wish to override the function that controls the form (and you are using PHPTemplate!) you would name your function as follows:

function phptemplate_user_login_block($form)

The function would be placed in the theme's template. php file.

Let's now work through the mechanics of a basic example and override the output function for the user login block.

Open your template . php file and add the following:

function phptemplate_user_login_block($form) { $output = drupal_render($form);

return $output; }

Save the file and you're done. This code now controls the output of the user login block. As written above, the new function adds nothing to the form output and will show only the default output. You can now add to that function as you see fit, adding additional classes or text that will be rendered with the form. For example, let's add a div to give us a few more options for styling this form:

function phptemplate_user_login_block($form) {

return '<div id="login-block-form">'. drupal_render($form) .'</

The example above is basic, and modifies only the form as a whole. What if you want to change the form elements?

To make changes to the form elements, you will need to go one further step. As an example, let's modify the data labels associated with our Login Block form. Add the following to the function we put in the template . php file:

function phptemplate_user_login_block($form) { $items = array();

$items[] = l(t('Create new account'), 'user/register', array('title' => t('Create a new user account.')));

$items[] = l(t('Send password reminder'), 'user/password', array('title' => t('Request new password via e-mail.'))); $form['links'] = array('#value' => theme('item_list', $items)); return _phptemplate_callback('user-login-block', array)'form' => $form));

This code does several things. First, it checks to see whether this user is logged in. Second, it modifies the text associated with the user register and password reminder links. Finally, it creates a callback to a specific dedicated template for the form, user-login-bloek.tpl.php

Note in the preceding example, if you did not wish to redirect to Q a template file, you could simply substitute return drupal_ render ($ form) ; for the last line in the example.

The approach just outlined is not necessarily intuitive, and may be difficult for non-programmers to maintain. As a general rule, if the modifications tend to be complex, or there is a need to be able to maintain the form easily, then you are better off creating a dedicated template, as discussed in the next section.

0 0

Post a comment