Page Templates

Many of the forms in the default Drupal system appear inside the content area of pages. For those forms, it is sometimes desirable to provide dedicated page templates. In most cases this is a straightforward matter; we treat it like any other page template override.

^ Overriding page templates is discussed in depth in Chapters 7. J

By way of example, let's set up a dedicated page template for the site-wide contact form.

First, create the page template where your form will appear. It's easiest just to copy the existing page. tpl. php, rename it page-eontaet. tpl. php, and save it to the root directory of your theme. Make your changes to the new template file and you are done. The system will automatically give precedence to the more specific page-eontaet. tpl. php and display it instead of the default page. tpl. php.

While the contact form is a simple job, the combined nature of the Login Page form, Request Password Form, and User Registration Form presents some special challenges for providing a dedicated page template. It can be done, but you need to include some logic to enable the template to work logically.

The function _phptemplate_variables () comes in handy in this situation. We can implement the page hook to add additional logic that helps determine whether the user is already logged in.

Add the following to your template . php file:

function _phptemplate_variables($hook, $variables = array{)) { switch ($hook) { case 'page': global $user; if (arg(O) == 'user'){

if (!$user->uid) { //only shows the page to users who are not // logged in $variables['template_file'] = 'page-login';

elseif (arg(l) == 'login' || arg(l) == 'register' || arg(l) == 'password' ) { $variables['template_file'] = 'page-login';

break;

return $variables;

Next, create a copy of your page .tpl. php file and save it as a new template file namedpage-login.tpl .php. Make your changes to the new page template and save the file.

Note that you could also override the default page .tpl. php with the filepage-user. tpl .php. The system would automatically apply the new template for all output generated by the user module — including the Login Page Form. This is not the result we want, however, as the user module includes not only the Login, Register, and Request Password pages, but also various other user-related pages. In the above example, we have avoided the unwanted styling of other unrelated pages by targeting the Login page.

Was this article helpful?

0 0

Post a comment