I am trying to modify the login and register page drupal 7 but i actually don't understand what is the goal of template_preprocess_page(&$variables) ... Someone can give me a better explanation of that ?

1 Answer 1


It is an entry point in your theme or module that allows you to modify or add template variables before the page (or applicable item) is rendered. This hook affects any page*.tpl.php files.

Similarly, template_preprocess_node is the same, but for node templates.


I need to change existing variable foo:

function mytheme_preprocess_page(&$variables) {
  // $foo used to be 'Old Value'
  $variables['foo'] = 'New Value';

I need to add new variable:

function mytheme_preprocess_page(&$variables) {
  // $baz does not yet exist
  $variables['baz'] = 'Baz!';

These are injected into the template, then they can be rendered with render or printed:


<?php print $foo; ?>
<?php print $baz; ?>

This is because $variables are passed by reference (note the & symbol) which allows you to modify the array.

  • Well, there are template suggestions (file name patterns) you have to adhere to. Of course, you can create your own too - but you have to define them first.
    – Kevin
    Jul 10, 2017 at 13:08
  • Ah ok i understand, so if i want to modify login page : I create a page-user-login.tpl file; In template.php i modify the $var with a function mytheme_preprocess_user_login(&$variables); and i call my form in user-login.tpl ? It is not better to do a hook-form-alter in a module ? Or can i just modify the login form without preprocess function ? Thank you for your help !
    – Rifipop
    Jul 10, 2017 at 13:12
  • Depends, if you are altering a form, form alter is the only option. If you are altering template variables, you want preprocess hooks.
    – Kevin
    Jul 10, 2017 at 13:12
  • I am a little lost about what method i need to use. In this post, they seems to modify a form without using form_alter : drupal.org/node/1152816
    – Rifipop
    Jul 10, 2017 at 13:14
  • No, thats all preprocessing particular page hooks, and adding variables surrounding the form. I wouldn't advise printing form array items piecemeal, because it is very easy to miss critical parts like the form token and other items that make forms actually work.
    – Kevin
    Jul 10, 2017 at 13:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.