0

I have a hook_menu page callback. I want to use the argument which it receives in hook_block.

For example

function myCustomFunction($args) {

}

modulename_block() {
  // Use $args here
}

How can this be done. Is there a way I can pass this as a parameter.

1

I'm not sure how this would be expected to work consistently; if your block relies on arguments built up from a particular page callback, then the block will only work on that page.

Having said that, if the page callback arguments are based on a known value, for example a node ID, then I can see how it might work for a particular path/set of paths.

Let's say this is your menu item in hook_menu():

$items['my/path/%node'] = array(
  'title' => 'A title',
  'access arguments' => array('access content'),
  'page callback' => 'MYMODULE_some_function',
  'page arguments' => array(2)
);

And this is your code for MYMODULE_some_function():

function MYMODULE_some_function($node) {
  $the_value = do_something_to($node);
}

In your hook_block() function you can grab the node ID out of the URL, load the node manually, and perform the same function on it that you have in MYMODULE_some_function(). For example:

function MYMODULE_block($op = 'list', $delta = 0, $edit = array()) {
  // I've omitted the 'list' op, obviously you'll need to implement that yourself

  if ($op == 'view' && $delta == $the_delta) {
    // Check that we're on an appropriate page
    if (arg(0) == 'my' && arg(1) == 'path' && is_numeric(arg(2))) {
      $nid = arg(2);

      $node = node_load($nid);

      $the_value = do_something_to($node);

      $block['content'] = $the_value;
    }
  }
}

You might want to centralise the code that runs $the_value = do_something_to($node); into it's own function so you're not repeating yourself.

| improve this answer | |
  • i am sending Client time using JQuery and ajax as an argument.I want to display a message in the block depending on the current time.Say like a greeting message.I have some if conditions in my block where i compare the currentTime to display a message. – yashbinani May 26 '12 at 12:35
  • I'm not sure I understand. The block obviously won't be processed until the next page request when you make the AJAX call, so the block content would always be out of date (i.e. the result of the previous AJAX request, not the current). It sounds like you might just want to to manipulate the block directly on the client side and not bother sending it to the server – Clive May 26 '12 at 12:40

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.