开发者

My first try to output my Drupal7 module as a custom block failed

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() insid开发者_开发知识库e 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.


$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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜