找到你要的答案

Q:Print a variable in theme file from drupal module

Q:打印一个变量在主题文件从Drupal模块

This is code of vegas.module file. it is used to load images from specific folder.

   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;
      }
    }
  }

I print it in .module file. It gives the expected result.

print_r($backgrounds);

If i print it in my theme's page.tpl.php it doesn't return any values. Is there any way to load module's variable

这是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;
      }
    }
  }

我把它打印在模块文件中。给出了预期的结果。

print_r($backgrounds);

如果我把它打印在我的主题的page.tpl.php它不返回任何值。有没有办法加载模块的变量

answer1: 回答1:

If you want to print this varibale in page.tpl.php - use hook_preprocess_page

function custom_preprocess_page(&$variables), not node.

如果你想在page.tpl.php打印这个变量使用hook_preprocess_page

功能custom_preprocess_page(&;$变量),不结。

answer2: 回答2:

You need to use hook_preprocess_page to add variables to page template or hook_preprocess_node to add variable to node template.

https://api.drupal.org/api/drupal/modules!node!node.module/function/template_preprocess_node/7

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;
      }
    }
  }

Try this code and in yoot node.tpl.php will be avaliable $backgrounds array.

I think more correctly to put this code to template.php in your theme. It will be easiest to view how chaged node variables

你需要使用hook_preprocess_page添加变量页面模板或hook_preprocess_node添加变量节点模板。

https://api.drupal.org/api/drupal/modules!结!node.module/function/template_preprocess_node/7

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;
      }
    }
  }

试试这个代码和yoot node.tpl.php将可用美元背景阵列。

我认为更准确地把这个代码template.php在你的主题。这将是容易的看法如何改变节点变量

answer3: 回答3:

My theme name is custom. Here's what i have pasted into template.php file

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;
      }
    }
  }
 } 

and print it page.tpl.php file

print_r($backgrounds);

我的主题是自定义。这是我粘贴到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文件

print_r($backgrounds);
php  drupal-7  drupal-modules  drupal-theming