Drupal 自定义模块 |如何输出块中节点的所有字段

发布于 2024-12-29 08:56:46 字数 1000 浏览 2 评论 0原文

我有一个节点,我想在块中输出,因为该节点可以包含不同类型的字段,我不想指定这些字段。

有人可以解释一下如何做到这一点吗?

我尝试了这个,但我只得到一长串通知,没有任何输出。

    function mymodule_block_view($delta = '') {
    $node = node_load(6);
    $node_content = node_view($node);

    switch($delta){
        case 'mymodule':
        $block['subject'] = t('title');   
        $block['content'] = theme('block', $node_content);  
    }
    return $block;
} 

提前致谢! 结果:无内容和通知列表:

*Notice: Undefined index: #block in template_preprocess_block() (line 937 of /home/user/public_html/drupal/modules/block/block.module).
Notice: Trying to get property of non-object in template_preprocess_block() (line 939 of /home/user/public_html/drupal/modules/block/block.module).
Notice: Trying to get property of non-object in template_preprocess_block() (line 940 of /home/user/public_html/drupal/modules/block/block.module).
Notice: Trying to get property of non-object in template_preprocess_block() (line 943 of*

I have a node that I want to output in a block, because the node can contain different kind of fields I don't want to specify the fields.

Can someone explain me how this can be done?

I tried this, but I just get a long list of notices without any output.

    function mymodule_block_view($delta = '') {
    $node = node_load(6);
    $node_content = node_view($node);

    switch($delta){
        case 'mymodule':
        $block['subject'] = t('title');   
        $block['content'] = theme('block', $node_content);  
    }
    return $block;
} 

Thanks in advance!
Result: No content and a list of notices:

*Notice: Undefined index: #block in template_preprocess_block() (line 937 of /home/user/public_html/drupal/modules/block/block.module).
Notice: Trying to get property of non-object in template_preprocess_block() (line 939 of /home/user/public_html/drupal/modules/block/block.module).
Notice: Trying to get property of non-object in template_preprocess_block() (line 940 of /home/user/public_html/drupal/modules/block/block.module).
Notice: Trying to get property of non-object in template_preprocess_block() (line 943 of*

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

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

发布评论

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

评论(2

夏雨凉 2025-01-05 08:56:46

当我使用钩子节点时,我得到了结果。
在发布这个问题之前我已经测试了钩子节点,但是,是的,我不知道为什么它当时不起作用。

不知道这种方式是否是最好/最简单的方法,但我得到了输出,所以我很高兴!

function mymodule_block_view($delta = '') {

    switch($delta){
        case 'mymodule':            
               $node_content = node_view(node_load(6));
               $content = theme('node', $node_content);

               $block = array(
                 'subject' => t('My Module'),
                 'content' => $content);
    }
    return $block;
}

无论如何,谢谢!

I've got a result when I use the hook node.
I already tested the hook node before posting this question, but yeah, i don't know why it wasn't working then.

Don't know if this manner is the best/easiest way but I've got an output so I'm glad!

function mymodule_block_view($delta = '') {

    switch($delta){
        case 'mymodule':            
               $node_content = node_view(node_load(6));
               $content = theme('node', $node_content);

               $block = array(
                 'subject' => t('My Module'),
                 'content' => $content);
    }
    return $block;
}

Thanks anyway!

つ可否回来 2025-01-05 08:56:46

您有两个问题:

1)node_view() 返回一个需要使用 drupal_render()

2) 您不需要在此内容上使用 theme('block')。

如果您想将节点的对象传递给 tepmlate_preprocess_block 和匹配的 theme 文件,您可以使用此文件

case 'mymodule':
  $block['subject'] = t('title');   
  $block['#node'] = $node;
  $block['content'] = '';

然后,您可以在您的主题文件,通过访问 $block['#node']。

You have two problems:

1) node_view() returns an array which needs to be rendered using drupal_render()

2) You don't need to use theme('block') on this content.

If you want to pass the node's object to your tepmlate_preprocess_block and to your matching theme file, you can use this

case 'mymodule':
  $block['subject'] = t('title');   
  $block['#node'] = $node;
  $block['content'] = '';

Then, you can use whatever attribute of the node you want in your theming file, by accesing $block['#node'].

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