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

发布于 2024-12-06 16:47:35 字数 1939 浏览 0 评论 0原文

这是我第一次尝试创建 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 技术交流群。

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

发布评论

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

评论(1

等风也等你 2024-12-13 16:47:35

$hhelloworld_output() 函数的本地变量,因此在 helloworld_block_view() 中不可用。

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

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

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

以及您设置的内容helloworld_output 中的 $h 将被放入块的内容区域中。

$h is a variable local to the helloworld_output() function so wouldn't be available in helloworld_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 the helloworld_block_view() function to this:

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

And the content you set to $h in helloworld_output will be put into the block's content region.

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