Drupal:将自定义变量从自定义模块传递到我的模板

发布于 2024-12-08 14:36:46 字数 1592 浏览 0 评论 0原文

我意识到有人问了这个问题,但我要么根本不明白,要么以前的答案不适用于(或者我不明白如何应用它们)我的情况。这里是:

我有一个名为:“my_module”的自定义模块,

位于 /sites/all/modules/custom/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';
}

或者什么。我很确定我走在正确的道路上,但我却碰壁了。

I realize this question has been asked, but I either simply don't understand or the previous answers don't apply (or I don't understand how to apply them) to my circumstance. Here goes:

I have a custom module named:

"my_module" in /sites/all/modules/custom/my_module

I have a module file:

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

I have a page template name "page-mypage" which is NOT in my module:

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

I made the hook menu for this:

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

In the function, I build up some content like so:

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

In the template (again, NOT in my module folder, but in the THEME subfolder "pages", I have:

<?php print $content ?>

When I go to http://mysite/mypath/mypage I get "foo AND bar!"

Now for the question. I want a new variable, defined in my_module_mypage(), called '$moar_content'. I want to output $moar_content in my page-mypath-mypage.tpl.php. I only need to do this for this module and for this template. I do not need it theme-wide, so I don't think using mytheme's 'template.php' is appropriate.

I think I need to use some kind of preprocessing, but everything I try fails, and everything I read seems to be missing some kind of magic ingredient.

My thinking was:

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

or

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

or something. I'm pretty sure I'm on the right track, but I'm hitting a brick wall.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

避讳 2024-12-15 14:36:47

要完成这项工作,您必须遵循 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;
}

To do the job, you must follow Drupal's best practices, supposing you are using D6, so you can insert some variables to your template like this :

// 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,
);

Second thing, we define the theme hook for our page

// 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,
            ),
        )
    );
}

Now we can create a file "my-module-theme.tpl.php" in the root folder of our module, and paste something like "foo AND bar!"
Back to our my_module.module, the callback must be something like :

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

Also you can use preprocess hook to insert variables

// 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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文