【问题标题】:Print a variable in theme file from drupal module从drupal模块打印主题文件中的变量
【发布时间】:2015-10-08 10:29:57
【问题描述】:

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

我在 .module 文件中打印它。它给出了预期的结果。

print_r($backgrounds);

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

【问题讨论】:

    标签: php drupal-7 drupal-modules drupal-theming


    【解决方案1】:

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

    函数 custom_preprocess_page(&$variables),不是节点。

    【讨论】:

    • 我为什么要把这个函数放在 template.php 中
    • 如果某个开发人员在您更改模板文件之后 - 他会在一个文件中看到模板变量的所有更改,并且他不会在模块中搜索这些更改。
    【解决方案2】:

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

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

    试试这段代码,在 yoot node.tpl.php 中将有可用的 $backgrounds 数组。

    我认为将这段代码放在你的主题中的 template.php 更正确。查看如何更改节点变量将最容易

    【讨论】:

      【解决方案3】:

      我的主题名称是自定义的。这是我粘贴到 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);
      

      【讨论】:

        猜你喜欢
        • 2012-06-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-10-19
        • 1970-01-01
        相关资源
        最近更新 更多