In my theme_preprocess_page() function, I set a variable to a default value. Then, in an if(!empty($variables['node'])) { } block, I do various calculations to give that variable the value I actually want.

At the bottom of that function, the variable has the value I want, confirmed by dpm(). I set $variables['my_var'] = $my_var. After that, $variables has an entry with the correct name and correct values, also confirmed by dpm(). The function ends immediately thereafter.

But in page.tpl.php, when I dpm($my_var), it shows not the correct value, not a "variable doesn't exist" error, but instead the default value I set at the top of theme_preprocess_page().

It gets weirder. When I started writing this question, a dpm($variables['my_var']) at the top of theme_process_page() correctly displayed the variable. I realized that I should have had a test at the bottom of that function as well. When I added it, the variable started having the correct value in page.tpl.php! Then I started messing with just the dpm() calls in theme_process_page(), and now I'm in a state where none of the dpms in theme_process_page() display the correct value!

I'm flummoxed and my boss is flummoxed. What is there between template_preprocess_page() and page.tpl.php that could be doing something strange with this variable, particularly to make its value in template_process_page() behave indeterminately?

This post, illustrated:



function theme_preprocess_page(&$variables) {
    $myvar = 'default';

    if(!empty($variables['node'])) {
        // ...

        $myvar = 'modified';

        // ...

    dpm($myvar, '$myvar at bottom of preprocess_page');

    $variables['myvar'] = $myvar;

    dpm($variables['myvar'], '$variables[‘myvar’] at bottom of preprocess_page');

function theme_process_page(&$variables) {
    dpm($variables['myvar'], '$variables[‘myvar’] in process_page');




dpm($myvar, '$myvar in page.tpl.php');


Messages area in loaded page

$variables[‘myvar’] in process_page => [Sometimes 'default', sometimes 'modified']

$myvar in page.tpl.php => 'default'

$myvar at bottom of preprocess_page => 'modified'

$variables[‘myvar’] at bottom of preprocess_page => 'modified'

  • 1
    Maybe the variable name you're using is not unique? – Darvanen Apr 21 '15 at 2:29
  • 1
    And are you checking $myvar in a conditional anywhere? It wouldn't be the first time someone did if ($myvar = 'default') {} – Chris Rockwell Apr 21 '15 at 2:45
  • I've quadruple-checked both those issues. The variable name isn't used anywhere else in my theme, and I'm checking the value at the last line of preprocess_page, the first and last lines of process_page, and the first line of page.tpl.php. It really, really seems like something is happening outside my theme, or at least outside these two files. I may post my real code later if I don't figure this out. – 75th Trombone Apr 21 '15 at 3:00
  • Why hook_process_page? – AyeshK Apr 21 '15 at 5:12
  • I have to do some logic on the page title, which isn't available in preprocess_page. – 75th Trombone Apr 21 '15 at 13:43

Your Answer

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

Browse other questions tagged or ask your own question.