Can I track a variable in my extract () function?

I am working with a Drupal theme and I can see that a lot of the variables that look like were created with extract()

. Is it possible to track back and see where this array is?

+3


source to share


3 answers


Just highlight the variable $GLOBALS

, and you can find where it came from, if the array was not not installed.



+1


source


I assume you are referencing the variables passed to the template file that are efficiently retrieved from the array.

The code that does this in Drupal 7 is in theme_render_template () .

function theme_render_template($template_file, $variables) {
  extract($variables, EXTR_SKIP); // Extract the variables to a local namespace
  ob_start(); // Start output buffering
  include DRUPAL_ROOT . '/' . $template_file; // Include the template file
  return ob_get_clean(); // End buffering and return its contents
}

      

The function is called from theme () , which executes the following code.



// Render the output using the template file.
$template_file = $info['template'] . $extension;
if (isset($info['path'])) {
  $template_file = $info['path'] . '/' . $template_file;
}
$output = $render_function($template_file, $variables);

      

$render_function

the default is set to a value 'theme_render_template'

, but its value is set using the following code (c theme()

).

// The theme engine may use a different extension and a different renderer.
global $theme_engine;
if (isset($theme_engine)) {
  if ($info['type'] != 'module') {
    if (function_exists($theme_engine . '_render_template')) {
      $render_function = $theme_engine . '_render_template';
    }
    $extension_function = $theme_engine . '_extension';
    if (function_exists($extension_function)) {
      $extension = $extension_function();
    }
  }
}

      

+2


source


I am not familiar with Drupal, so this is just a suggestion, but if drupal has a template structure or if an array is passed from a controller or such, then you can use this extract, you can use get_defined_vars

in your view to get all vars and its possible existence there of an array where you can cross-reference variables you know about in the same array or such.

<?php 
$vars = get_defined_vars();
print_r($vars);
//or maybe
print_r($this);
?>

      

0


source







All Articles