1

这是我第一次尝试创建 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 不起作用吗?感谢帮助。

4

1 回答 1

0

$hhelloworld_output()函数的局部变量,因此在helloworld_block_view().

我不确定你为什么要在 中设置标题helloworld_output(),你应该删除这些行,因为它们只会给你带来问题。

删除该函数中的两个调用header()后,只需将函数中的代码行更改helloworld_block_view()为:

$block['content'] = helloworld_output();

您设置的内容$hhelloworld_output被放入块的内容区域。

于 2011-09-26T14:26:27.983 回答