0

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*
2
  • Hey, how about using hook_block instead and setting the block content to the node->content value, which is the already rendered version of the node. Jan 30, 2012 at 15:34
  • FYI - There is a nodeblock module available. "This module allows you to specify content type(s) as being a block"
    – nmc
    Jan 30, 2012 at 15:37

2 Answers 2

1

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!

0

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'].

2
  • Thanks for the answer although I can't get any result: 1) I used drupal_render() but without any result.. doing something wrong I think 2) that's a path you take where I can't follow you. template_preprocess_block, does this mean the word 'template' needs to be replaced by your own modulename or is there an example or more documentation of giving custom variables to a certain function?
    – Mr.B
    Jan 29, 2012 at 19:39
  • try not calling theme('block') inside this hook. Jan 29, 2012 at 22:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.