Is there an option to change $vars array in form function? I would like to pass some variables to page template in form creation function .. If not, what are other options to influence page.tpl from form?

EDIT: I have a function for form creation and i would like to add page class if some params are sent to this function.

function mymodule_test_form($form, &$form_state, $param){
  if ($param == 'special'){
    #add class to page


  • I think you'll need to provide some more detail @gregab. Your question is not very clear.
    – Darvanen
    Sep 8 '14 at 7:09

It seems to me that you are talking about two different things: altering form fields vs. modifying the $variables array in page.tpl.php.

1) When you say "form creation function", what I hear is that you want to change fields in an existing form. For this, you would want to implement hook_form_alter(). If you want to learn more about how to structure a form, visit the Form Generation page in the Drupal API.

2) If actually you meant that you want to add or change variables in your page.tpl.php file, then you'll want to implement hook_preprocess_page().


I think you want add some variable to prepared variables in page.tpl.php for this job you should add it in you template.php file in your current theme.

create a function start with your theme name ,(as example if I use mytheme , it will be function mytheme.preprocess_page) , then add your variables to $variables array ( be sure that it passed by reference in function (&$variables) )

as example

function ThemeName_preprocess_page(&$variables) {

$variables['gregab'] = 'Hello,I am new variable :)';  

//Part of Page.tpl.php Anywhere put this variable

print $gregab;

Then clear the cache (dont forget every time you change the 'tpl` files clear the caceh)


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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