I define variables in template.php file preprocess_page but it come undefined in tpl.php files. What can be a problem? Here is code

template.php file

$user_profile = profile2_load_by_user($uid, 'user_profile');
$vars['user_profile'] = $user_profile;

tpl.php file

  • Where is $uid coming from? based on the code you have provided, $uid is undefined. – No Sssweat Oct 10 '17 at 10:57
  • 1
    Friend that comes from here {global $user; $uid = user_load($user->uid);} – Alen Simonyan Oct 10 '17 at 11:12
  • Are you sure that you are accessing the variable in the correct tpl.php file?Check the tpl.php name and make sure that is correct – Suresh R Oct 10 '17 at 12:14

Firstly, check to see that profile2_load_by_user is a function available via a .module file and not something you will need to load via module_load_include() - which will load other files, such as .inc files.

Second, check the argument list for the preprocess hook. Did you define it as &$variables and mistakenly assign it to $vars which would mean nothing in this context? Did you forget to pass by reference via & which makes the array modifiable?

Third, if you just added this code, clear your site caches so the changes are picked up.

Fourth, the function name isn't preprocess_page. It should be MYTHEME_preprocess_page.


Solution is write not one preprocess_page but, many preprocesses depend on page... Say you have user_page page, preprocess should be theme_name_preprocess_user_page or use ['theme_hook_suggestion'][] = 'tpl.php - name';

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.