0

In Views we can create a block that the content depends on the content of a node using contextual filters. So we can pass variables and check the visibility of the Views-Block.

Let’s say in our content type we have a field that has a text-field. In Views->Contextual->Our field->Provide default value-> content ID from URL can grab this.

But creating a block programmatically

hook_block_view($delta = '') {
…
$block['content'] = theme('item_list', array(
         'items' => ???,
          'type' => 'ul',
        ));
…
}

How we can grab these values form the node-field?

And if there are no values how we will hide the block?

  • So do you want the contextual nid and relevant field of that node in block ? – mohit_rocks Jun 17 '14 at 10:39
  • Short answer: yes. I don't use views at all (in this implementation), so I want a (proper) way to grab the content of the node field from a certain content type in a block (while the node is rendered normally in the main content), and check if exist. Because the block will be assigned in a region but the field (I want to display) is only in one content-type. – tvl Jun 17 '14 at 10:54
0

You can use menu_get_object() to get the current node. Refer menu_get_object

menu_get_object() provides access to objects loaded by the current router item. For example, on the page node/%node, the router loads the %node object, and calling menu_get_object() will return that.

If you want to hide the block (not only the content) warp the return $block; in if.

if (isset(menu_get_object()->some_field) {
  ...
  return block;
}
|improve this answer|||||
  • It's exactly that I need! So simple but I was so far away! Many thanks! – tvl Jun 17 '14 at 12:50
  • Add/edit my solution – tvl Jun 17 '14 at 13:17
  • The hook_block_view() says if you return an empty block array then the block won't be displayed. – wolverine Jun 17 '14 at 13:21
0

Found a few modules depending on your need:

Node field block

An easy way to create blocks with a collection of node fields. Focused on letting customers manage most of the content on a page in just one node. You can create multiple blocks for a node with one or a combination of fields and move them in different regions.

Field as block

Field as Block provides an easy way to display one or more fields of the current node in a block.

There's a custom way also if you want. You can use below function for this purpose.

render(field_view_field('node', $node, 'field_myfield'));
|improve this answer|||||
  • Thanks for the advice but as I say I need it to do programmatically. The problem behind it's much more complicated.. And because it's complicated I can't use the render. – tvl Jun 17 '14 at 12:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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