Drupal 7 我第一次尝试将Drupal7模块作为自定义块输出失败

Drupal 7 我第一次尝试将Drupal7模块作为自定义块输出失败,drupal-7,drupal-modules,drupal-hooks,Drupal 7,Drupal Modules,Drupal Hooks,这是我第一次尝试创建Drupal模块:Hello World 我需要它显示为一个自定义块,我发现这可以通过两个Drupal7钩子实现:我的helloworld模块内的钩子块信息和钩子块视图。Drupal 6中使用了不推荐使用的hook_块 在实际形式中,它可以工作,但它只显示文本:“这是一个块,它是我的模块”。我实际上需要显示我的主函数的输出:helloworld_output,t变量 <?php function helloworld_menu(){

这是我第一次尝试创建Drupal模块:Hello World

我需要它显示为一个自定义块,我发现这可以通过两个Drupal7钩子实现:我的helloworld模块内的钩子块信息和钩子块视图。Drupal 6中使用了不推荐使用的hook_块

在实际形式中,它可以工作,但它只显示文本:“这是一个块,它是我的模块”。我实际上需要显示我的主函数的输出: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;
    }
    ?>
现在我只需要在block:helloworld\u block\u视图中显示主函数output:helloworld\u output的内容

你知道为什么$block['content']=$h不起作用吗?感谢您的帮助。

$h是helloworld\u输出函数的局部变量,因此在helloworld\u块视图中不可用

我不知道为什么要在helloworld_输出中设置标题,应该删除这些行,因为它们只会给您带来问题

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

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

您在helloworld_输出中设置为$h的内容将被放入块的内容区域。

谢谢Clive,它实际上是一种魅力。我认为这是开发模块的一个良好开端。