Php 从drupal模块打印主题文件中的变量

Php 从drupal模块打印主题文件中的变量,php,drupal-7,drupal-modules,drupal-theming,Php,Drupal 7,Drupal Modules,Drupal Theming,这是vegas.module文件的代码。它用于从特定文件夹加载图像 function vegas_init() { // Load all the images to be added to Vegas. $backgrounds = array(); $fade = variable_get('vegas_fade', 0); for ($i = 0; $i < 10; $i++) { $fid = variable_get('vegas_images_'

这是vegas.module文件的代码。它用于从特定文件夹加载图像

   function vegas_init() {
  // Load all the images to be added to Vegas.
  $backgrounds = array();
  $fade = variable_get('vegas_fade', 0);
  for ($i = 0; $i < 10; $i++) {
    $fid = variable_get('vegas_images_' . $i, '');
    if (!empty($fid)) {
      $image = file_load($fid);
      if ($image) {
        $background = array(
          'src' => file_create_url($image->uri),
        );
        if (!empty($fade)) {
          $background['fade'] = intval($fade);
        }
        $backgrounds[] = $background;
      }
    }
  }

如果我在主题的page.tpl.php中打印它,它不会返回任何值。是否有任何方法加载模块的变量

您需要使用hook\u preprocess\u页面将变量添加到页面模板,或使用hook\u preprocess\u节点将变量添加到节点模板

函数MYMODULE_preprocess_node(&$variables){//可以是MYTHEME_preprocess_node,并位于template.php中
//加载要添加到Vegas的所有图像。
$backgrounds=array();
$fade=变量获取('vegas\U fade',0);
对于($i=0;$i<10;$i++){
$fid=variable_get('vegas_images_.'$i',);
如果(!空($fid)){
$image=文件加载($fid);
如果($image){
$background=数组(
“src'=>文件创建url($image->uri),
);
如果(!空($fade)){
$background['fade']=intval($fade);
}
$variables['backgrounds'][=$background;
}
}
}
试试这段代码,在yoot node.tpl.php中可以使用$backgrounds数组


我认为把这段代码放到主题中的template.php中更为正确。查看节点变量的变化情况将是最容易的。

我的主题名是自定义的。下面是我粘贴到template.php文件中的内容

function custom_preprocess_node(&$variables) { //can be MYTHEME_preprocess_node and locate in template.php
  // Load all the images to be added to Vegas.
  $backgrounds = array();
  $fade = variable_get('vegas_fade', 0);
  for ($i = 0; $i < 10; $i++) {
    $fid = variable_get('vegas_images_' . $i, '');
    if (!empty($fid)) {
      $image = file_load($fid);
      if ($image) {
        $background = array(
          'src' => file_create_url($image->uri),
        );
        if (!empty($fade)) {
          $background['fade'] = intval($fade);
        }
        $variables['backgrounds'][] = $background;
      }
    }
  }
 } 

如果要在page.tpl.php中打印此变量,请使用hook\u preprocess\u页面


函数自定义预处理页面(&$variables),而不是节点。

为什么我要将该函数放在template.phpIf某些开发人员在您更改模板文件后-他将在一个文件中看到模板变量的所有更改,而不会在模块中搜索此更改。
function MYMODULE_preprocess_node(&$variables) { //can be MYTHEME_preprocess_node and locate in template.php
  // Load all the images to be added to Vegas.
  $backgrounds = array();
  $fade = variable_get('vegas_fade', 0);
  for ($i = 0; $i < 10; $i++) {
    $fid = variable_get('vegas_images_' . $i, '');
    if (!empty($fid)) {
      $image = file_load($fid);
      if ($image) {
        $background = array(
          'src' => file_create_url($image->uri),
        );
        if (!empty($fade)) {
          $background['fade'] = intval($fade);
        }
        $variables['backgrounds'][] = $background;
      }
    }
  }
function custom_preprocess_node(&$variables) { //can be MYTHEME_preprocess_node and locate in template.php
  // Load all the images to be added to Vegas.
  $backgrounds = array();
  $fade = variable_get('vegas_fade', 0);
  for ($i = 0; $i < 10; $i++) {
    $fid = variable_get('vegas_images_' . $i, '');
    if (!empty($fid)) {
      $image = file_load($fid);
      if ($image) {
        $background = array(
          'src' => file_create_url($image->uri),
        );
        if (!empty($fade)) {
          $background['fade'] = intval($fade);
        }
        $variables['backgrounds'][] = $background;
      }
    }
  }
 } 
print_r($backgrounds);