Hello.

I need help in how I'll implement hook_block function in my module.

The situation I'm in is to display a block as requested.
I have a menu with 6 items. When the user clicks in one of the menu items, I want my module to display a block which is associated to that menu item only. Obviously, the content in the block varies depending upon which menu item was chosen.

Is this something possible in Drupal?

I'm trying to implement it using a multi-block scheme, I don't seem lucky.

Thanks for your help in advance.
Pol

Comments

nevets’s picture

The first thing you need is a way to map the menu item to the content shown. Typically this is done using the path (ex: taxonomy/term/1) and through code mapping that to some piece of content.

So you could make 6 blocks and set their visibility based on path (under 'configure' for the block)

Or one block that determine the content based on path.

palabat’s picture

Hello.

Thanks for your reply.
I don't see the 'visibility based on path' option when I try to configure the block.
Also as a background, the block content is actually a tree in which each node in the tree is a link to a node that I want to display on the main content pane.

Thanks.
Pol

nevets’s picture

On the block configuration page there should be a section labeled 'Page specific visibility settings'.

palabat’s picture

Thanks for pointing it out.
Initially I set the path to one page only.
Remember my block content is a tree of nodes. Now when the user clicks a node other than what was specified in the path, I will lose the block - which is expected.
What should I do if I want to keep the block(tree) for a huge number of nodes (same nodes in the tree)?

Thanks.

nevets’s picture

You can use PHP code (the expert mode) to make this "easier". Set the 'Show block on specific pages:' to 'Show if the following PHP code returns TRUE (PHP-mode, experts only).' and add the code below to 'Pages'. The code starts with the current menu item looking for a menu item with the desired path walking backwards till it finds a match (or runs out of parents). You will need to set $desired_path to the approriate value. And a word of WARNING bad PHP code can mess things up. If you want to play around with the code I suggest making content of type page, setting input format to PHP and fine tuning the code there.


// Set this to the path of the menu item 
// you want to base visibility on

$desired_path = 'node';

// We assume we do not want to show the block
$visible = FALSE;

// Get the current menu item id
$mid = menu_get_active_item();
while ( $mid ) {
   // Get the menu item
  $item = menu_get_item($mid);
  // Is the path the one we want?
  if ( $item['path'] == $desired_path ) {
    // Yes
    $visible = TRUE;
    // Found what we want, jump out of the loop
    break;
  }
   // Path not found look at the parent menu item
   $mid = $item['pid'];
}

return $visible;
palabat’s picture

Hello.
Thanks for your suggestion.
The problem I have is that all the (Drupal) nodes have the same path description i.e. http://domain.com/drupal/node/100. The only difference is its node id. Is it possible to retrive the node id being displayed on the address bar using Drupal/PHP functions? If this is possible then I can retrieve the node branch information from the table I created for all the nodes.

Thanks.
Pol

nevets’s picture

You have a menu structure something like

  • parent 1
    • node/100
    • node/101
    • node/102
    • ,,,,
  • parent 2
    • node/200
    • node/201
    • node/202
    • ,,,,

And if parent 1 is select or node 100, 101 or 102 you want a particular block to be visible. If this is the case you would use the path for parent 1 to test for.

palabat’s picture

I have one menu with 6 menu items.

Menu
- item 1
- item 2
- item 3
- item 4
- item 5
- item 6

Each item is associated to a block(navigation tree of nodes). The tree may comprise more than 300 nodes. All in all I have around 2000 nodes/pages for all the 6 blocks. I have a table in my db that has a field which describes each node on which block(tree) that a node belongs.
So if I know the node id of the page being viewed by the user, I can process if the given node is part of the existing block or not.

Thanks.
Pol

palabat’s picture

The PHP variable, $_REQUEST holds the node path using the 'q' element.

Thanks for your help.
Pol