Is there a way to create a temporary variable in one module or theme TPL file and check the state of that variable in another file when processing a single page?

Perhaps there is a way to set/read a custom variable in the session? What are some strategies I can use to accomplish this?

Comments

pbarnett’s picture

Either use variable_set/variable_get (http://api.drupal.org/api/function/variable_set/6) or set $_SESSION['your-variable-name'] for a session-specific scope.

jaypan’s picture

variable_set and variable_get are sitewide, not user-based, so if you use them, keep that in mind.

Contact me to contract me for D7 -> D10/11 migrations.

comfixit’s picture

Thank you for the tip. And variable can be set and retrieved however it runs in a way that I did not expect.

I am overriding 2 view templates. In the first template which overrides (views-view-unformatted.tpl.php) it seems to try to print the row which gets its output from the template which overrides (views-view-fields.tpl.php). In that second template I print out some variables directly and also try to print the JTS variable I set which should print 1 the first time through the loop and 2 the second time. However when I run this code it shows only the 2 which suggests to me that Drupal is probably calculating the results for the variable at the end of this tpl page before applying the value when running the internal loops.

(views-view-unformatted.tpl.php)

<?php 
            variable_set('JTS', '1');

            foreach ($rows as $id => $row): ?>
              <?php print $row; ?>
          <?php endforeach; ?>

          <?php
          variable_set('JTS', '2'); 
          foreach ($rows as $id => $row): ?>
              <?php print $row; ?>
          <?php endforeach; ?>

(views-view-fields.tpl.php)

<?php
$JT_State = variable_get('JTS', NULL);
?>
<?php
$myImageUrl = trim(strip_tags($fields['field_image_fid']->content));
$myLongDesc = $fields['field_alt_value']->raw;
$myAlt = $fields['field_alt_value']->raw;
print "<a href='" . $myImageUrl . "'><img src='" . $myImageUrl . "' alt='" . $myAlt . "' /></a>";
print $JT_State;
?>

Please excuse the sloppy experimental state the code is in. However the output of the print $JT_State shows as 2 both times the loop is run.

I would like to eventually create a switch statement to act on the information differently depending on if its the first or second pass however if this behavior cannot be avoided I might need to attack the problem another way.

yfreeman’s picture

in your function do the following

global $conf;
$old_val = $conf[<variable>]; // store old value
$conf[<variable>] = "new value"; // set new value

/// run you code

$conf[<variable>] = $old_val; // set back to old value

It won't save it in the database because it's not using the variable_set() function. Will only work for the current running script and then next time will revert to original value.

But, it will be available for variable_get() functions
I use it to override the default location for storing files. I like to use drupal's powerful file_* functions, but drupal checks to see the destination of the file if it is in the default file directory. So I use the above code to temporarily set the default_file_directory to the place I want to use.

jenifer_raja’s picture

Hi comfixit

global $variable

declare the variable

Set the value for the variable

$variable="your value";

where u want to accomplish this?

use

unset($variable);