我第一次尝试将 Drupal7 模块输出为自定义块失败
这是我第一次尝试创建 Drupal 模块:Hello World。
我需要它将其显示为自定义块,我发现这可以通过 2 个 Drupal7 钩子来实现:我的 helloworld 模块中的 hook_block_info() 和 hook_block_view() 。在 Drupal 6 中,使用了已弃用的 hook_block()。
在实际的形式中它可以工作,但它只显示文本:“这是一个属于我的模块的块”。我实际上需要显示主函数的输出:helloworld_output(),即 t 变量。
<?php
function helloworld_menu(){
$items = array();
//this is the url item
$items['helloworlds'] = array(
'title' => t('Hello world'),
//sets the callback, we call it down
'page callback' => 'helloworld_output',
//without this you get access denied
'access arguments' => array('access content'),
);
return $items;
}
/*
* Display output...this is the callback edited up
*/
function helloworld_output() {
header('Content-type: text/plain; charset=UTF-8');
header('Content-Disposition: inline');
$h = 'hellosworld';
return $h;
}
/*
* We need the following 2 functions: hook_block_info() and _block_view() hooks to create a new block where to display the output. These are 2 news functions in Drupal 7 since hook_block() is deprecated.
*/
function helloworld_block_info() {
$blocks = array();
$blocks['info'] = array(
'info' => t('My Module block')
);
return $blocks;
}
/*delta si used as a identifier in case you create multiple blocks from your module.*/
function helloworld_block_view($delta = ''){
$block = array();
$block['subject'] = "My Module";
$block['content'] = "This is a block which is My Module";
/*$block['content'] = $h;*/
return $block;
}
?>
我现在需要的只是在块中显示主函数输出的内容:helloworld_output():helloworld_block_view()。
您知道为什么 $block['content'] = $h 不起作用吗?感谢您的帮助。
This is my first try to create a Drupal module: Hello World.
I need it to have it displayed as a custom block and I found this can be implemented by 2 Drupal7 hooks: hook_block_info() and hook_block_view() inside my my helloworld module. In Drupal 6 was used the deprecated hook_block().
In the actual form it works but it only display the text: 'This is a block which is My Module'. I actually need to display the output of my main function: helloworld_output(), the t variable.
<?php
function helloworld_menu(){
$items = array();
//this is the url item
$items['helloworlds'] = array(
'title' => t('Hello world'),
//sets the callback, we call it down
'page callback' => 'helloworld_output',
//without this you get access denied
'access arguments' => array('access content'),
);
return $items;
}
/*
* Display output...this is the callback edited up
*/
function helloworld_output() {
header('Content-type: text/plain; charset=UTF-8');
header('Content-Disposition: inline');
$h = 'hellosworld';
return $h;
}
/*
* We need the following 2 functions: hook_block_info() and _block_view() hooks to create a new block where to display the output. These are 2 news functions in Drupal 7 since hook_block() is deprecated.
*/
function helloworld_block_info() {
$blocks = array();
$blocks['info'] = array(
'info' => t('My Module block')
);
return $blocks;
}
/*delta si used as a identifier in case you create multiple blocks from your module.*/
function helloworld_block_view($delta = ''){
$block = array();
$block['subject'] = "My Module";
$block['content'] = "This is a block which is My Module";
/*$block['content'] = $h;*/
return $block;
}
?>
All I need now is to display the content of my main function output: helloworld_output() inside the block: helloworld_block_view().
Do you have an idea why $block['content'] = $h won't work? Thanks for help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
$h
是helloworld_output()
函数的本地变量,因此在helloworld_block_view()
中不可用。我不确定为什么要在 helloworld_output() 中设置标头,您应该删除这些行,因为它们只会给您带来问题。
删除该函数中对
header()
的两次调用后,只需将helloworld_block_view()
函数中的代码行更改为:以及您设置的内容
helloworld_output
中的$h
将被放入块的内容区域中。$h
is a variable local to thehelloworld_output()
function so wouldn't be available inhelloworld_block_view()
.I'm not sure why you're setting headers in
helloworld_output()
, you should remove those lines as they will only cause you problems.After you've removed the two calls to
header()
in that function simply change your line of code in thehelloworld_block_view()
function to this:And the content you set to
$h
inhelloworld_output
will be put into the block's content region.