1

I'm really struggling with how to get variables created in page.tpl.php and node.tpl.php in rendered content and blocks. I hope I'm just missing something simple.

Basically, I'm trying to have a unique ID number passed around the site. To do that (as far as I know) I need to add it to all the links, IE /?a_uid=123456.

I'm generating the random ID in success_preprocess_page (full code is below).

function success_preprocess_page(&$variables, $hook) {
  //echo '<pre>'; print_r($variables); echo '</pre>';\

        if (isset($_GET['utm_campaign']))   
            $adkey = $_GET['utm_campaign'];
        elseif (isset($_POST['utm_campaign']))  
            $adkey = $_POST['utm_campaign'];
        else
            $adkey = NULL;

        if (isset($_GET['a_uid']))  
            $a_uid = $_GET['a_uid'];
        elseif (isset($_POST['a_uid'])) 
            $a_uid = $_POST['a_uid'];
        else
            $a_uid = NULL;

    if ($adkey != NULL)
    {
        $a_uid = "123456";
    }
    $variables['adkey'] = $adkey;
    $variables['a_uid'] = $a_uid;

   if(isset($variables['a_uid'])){
    // add the flag to the node to say it's been set and there is only a single node to display
       if(isset($variables["page"]["content"]["system_main"]["nodes"]) && count($variables["page"]["content"]["system_main"]["nodes"]) == 2){
            $keys = array_keys($variables["page"]["content"]["system_main"]["nodes"]);
           // Set a new variable called sidebar_first equal to true on the node.tpl level
           $variables["page"]["content"]["system_main"]["nodes"][$keys[0]]['#node']->a_uid = $a_uid;
      }
    }
}

Any links that are in page.tpl.php, ex:

<a href="programs/?a_uid=<?php echo $a_uid; ?>">Programs</a>

Display the correct $a_uid variable result of 123456.

And if I try to echo $a_uid in node.tpl.php, ex:

<?php $a_uid = $node->a_uid; ?>
<?php //echo 'A_UID: '.$a_uid.'<br><br>'; ?>

Also displays 123456 fine.

But if I try anything like this in my content pages (even with them set as PHP Code Text Format, which correctly processes other PHP code), everything result is blank.

For instance, I currently have a content page that has this in it, trying to hit all the marks I can think of:

<?php echo $a_uid; ?>
<?php print $a_uid; ?>
<?php echo $node->a_uid; ?>
<?php echo $content->a_uid; ?>
<?php print $node->a_uid; ?>
<?php print $content->a_uid; ?>
<?php print ($page['a_uid']); ?>
<?php print ($node['a_uid']); ?>

Not a single one of those displays anything.

As far as I can tell it seems like the variable is just not getting passed to the content? I just don't understand why, or how to fix it.

I assume this is standard, but this is how my content is being called in node.tpl.php:

print render($content);

I am also having the same problems in my header and footer blocks. Those are being called from page.tpl.php as:

<?php print render($page['footer']); ?>
<?php print render($page['header_button']); ?>

Any help with this would be greatly appreciated. Thanks! Oh and 123456 is just a placeholder in here for my random unique ID generator code :)

0

PHP Code format is executed in isolation from other codes, so it is not in the domain of any preprocess function, So by using PHP Code format, there is no way get data from preprocess functions, Even the hook_preprocess_block.

About the variables, try to set them in hook_preprocess_node, because as far as I know Drupal builds components in the manner of inside to the outside, so first the node.tpl and node preprocess function get called then page.tpl and page preprocess function.

To display the variable in the block, you can also build global variable. How do I add a new Global Variable? explains how to do it.

1

Apparently I was just looking at my process wrong this morning. I went back to it after reading what you wrote and realized I could just use in my html.tpl file and it would handle everything I need it to do.

But thank you for answering! I'm not sure how but what you posted led me in the right direction after I was reading your links :)

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.