【问题标题】:Drupal: Passing custom variable from custom module to my templateDrupal:将自定义变量从自定义模块传递到我的模板
【发布时间】:2011-10-12 05:57:20
【问题描述】:

我知道有人问过这个问题,但我要么根本不明白,要么之前的答案不适用于我的情况(或者我不明白如何应用它们)。如下:

我有一个自定义模块,名为:

/sites/all/modules/custom/my_module 中的“my_module”

我有一个模块文件:

/sites/all/modules/custom/my_module/my_module.module

我有一个页面模板名称“page-mypage”不在我的模块中:

/sites/all/themes/mytheme/pages/page-mypath-mypage.tpl.php

我为此制作了挂钩菜单:

$items['mypath/mypage'] = array(
    'title'         => 'My Page!',
    'page callback'         => 'my_module_mypage',
    'page arguments'    => array(1,2),
    'access callback'   => true,
    'type'          => MENU_CALLBACK,
);

在函数中,我构建了一些内容,如下所示:

function my_module_mypage($x, $y) {
    $output = "foo AND bar!";
    return $output;
}

在模板中(同样,不是在我的模块文件夹中,而是在主题子文件夹“pages”中,我有:

<?php print $content ?>

当我转到 http://mysite/mypath/mypage 时,我得到“foo AND bar!”

现在回答问题。我想要一个在 my_module_mypage() 中定义的新变量,名为“$moar_content”。我想在我的 page-mypath-mypage.tpl.php 中输出 $moar_content。我只需要为这个模块和这个模板做这个。我在主题范围内不需要它,所以我认为使用 mytheme 的 'template.php' 不合适。

我认为我需要使用某种预处理,但我尝试的一切都失败了,而且我阅读的所有内容似乎都缺少某种神奇的成分。

我的想法是:

function my_module_preprocess_page_mypath_mypage(&$variables) {
    $variables['moar_content'] = 'OATMEAL';
}

function my_module_preprocess_my_module_mypage(&$variables) {
    $variables['moar_content'] = 'OATMEAL';
}

什么的。我很确定我走在正确的轨道上,但我遇到了障碍。

【问题讨论】:

  • 很大程度上取决于您使用的 Drupal 版本 - 6 还是 7?
  • Drupal 6。抱歉,我应该在前面提到。

标签: drupal drupal-6 preprocessor drupal-modules customization


【解决方案1】:

要完成这项工作,您必须遵循 Drupal 的最佳实践,假设您使用的是 D6,因此您可以像这样在模板中插入一些变量:

// You menu path is good
$items['mypath/mypage'] = array(
    'title' => 'My Page!',
    'page callback' => 'my_module_mypage',
    'page arguments' => array(1,2),
    'access callback' => true,
    'type' => MENU_CALLBACK,
);

第二件事,我们为页面定义主题钩子

// We define here a new theme file for your your page
// Your theme file must be located in your module's folder
// you can use a subfolder to group all your module's theme files
// E.g : themes/my-module-theme.tpl.php
// Note that in theme files, we change _ by -
function my_module_theme() {
    return array(
        'my_module_theme' => array( // Keep that name in your mind
            'template' => 'my_module_theme',
                'arguments' => array(
                'my_var' => NULL,
                'my_var2' => NULL,
            ),
        )
    );
}

现在我们可以在模块的根文件夹中创建一个文件“my-module-theme.tpl.php”,然后粘贴类似“foo AND bar!”的内容。 回到我们的 my_module.module,回调必须是这样的:

function my_module_mypage($x, $y) {
    // $x and $y are optionnal, so this is the first manner
    // to inject variables into your theme's file
    $output = theme("my_module_theme", $x, $y);
    return $output;
}

你也可以使用预处理钩子插入变量

// The hook must be named like this : template_preprocess_NAME_OF_THEME_FILE
// where NAME_OF_THEME_FILE is the name that you kept in your mind ;)
function template_preprocess_my_module_theme(&$variables) {
    // Do some job
    $var1 = 'Foobar';

    // So in "my-module-theme.tpl.php", $my_var1 will print Foobar
    $variables['my_var1'] = $var1;
}

【讨论】:

  • 我的页面模板不在我的模块目录中。它们在主题/mytheme/pages 中。其中很多。不幸的是,这是交给我的,所以这是我必须使用的。
  • 抱歉,这个答案没有回答我的问题。这是我在其他任何地方都能找到的答案。如果我按照这些说明进行操作,那么我的模块文件夹中就会有一个模板,但我仍然无法在 /sites/all/themes/mytheme/pages/page-mypage 中的页面模板的上下文中呈现该模板。 tpl.php——所以我还是被卡住了。不过谢谢你:)
猜你喜欢
  • 1970-01-01
  • 2016-12-31
  • 2022-11-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多