0

I'd like to pass a variable ($myvariable) into the block_view function in my custom module, so i can see the content of the variable in the block content.

But there must be something i do wrong, because the below code gives me the following error:

Warning: Missing argument 2 for mymodule_subscribe_block_view ...

/**
 * Implements hook_block_view().
 *
 * Prepares the contents of the block.
 */
$myvariable = 'test';
function mymodule_subscribe_block_view($delta = '', $myvariable) {
  switch ($delta) {
    case 'mymodule_test':
      $block['subject'] = t('Test block');
      if (user_access('access content')) {
          // Pass data through theme function.
          $block['content'] = $myvariable;
      }
    return $block;
  }
}

I'd greatly appreciate any hints on what's going wrong here.

  • You can't do this. If you need variables in block it should be not a block. – FriOne Aug 16 '14 at 3:28
  • You may find your answer here drupal.stackexchange.com/questions/32341/… – TBI Infotech Aug 16 '14 at 6:10
  • Do you have any more information on this? Why can't a block get a variable, and what would be a better alternative? – kleinermann Aug 17 '14 at 8:50
  • Thanks for the link TBI Infotech. I still couldn't get this working, but i will do some more reading on Drupal block module development. If i find an answer to my question i'll post it here. – kleinermann Aug 17 '14 at 8:58
  • If you need some data inside a block view, then the code has to be able to obtain it for itself. So, for example, it can process the current request path and use it to get database keys. On a node view page you might extract the node Id and use it to fetch the node to get data for the block. – Alfred Armstrong Oct 14 '16 at 8:39
1

The error is caused from the fact you are declaring that hook_block_view() implementation as requiring two arguments, but the code invoking hook_block_view() is passing a single argument. If you would not want to see that error, you should use this code.

function mymodule_subscribe_block_view($delta = '', $myvariable = 0) {
  switch ($delta) {
    case 'mymodule_test':
      $block['subject'] = t('Test block');
      if (user_access('access content')) {
          // Pass data through theme function.
          $block['content'] = $myvariable;
      }
    return $block;
  }
}

Now, although that code would fix the error message you are seeing, it would not solve the fact mymodule_subscribe_block_view() would always receive 0 as second argument, since Drupal doesn't know anything about that implementation needing a second argument.

The question really is: How do I pass a value I calculate in some other function to a hook I implement in my module?
I take you are interested in passing a dynamic value, or the hook_block_view() implementation could easily use a literal value in its code (e.g. $myvariable = 42;).

Leaving out the case of hook_block_view() calling a function to get the dynamic value it needs, you could use drupal_static() to share a value between two functions (also in the case one of those functions is a hook, or both are hooks).
Keep in mind that:

  • If the value returned from drupal_static() is set from another hook, you need to be sure hook_block_view() is invoked after that hook; differently, hook_block_view() will always receive the default value (passed as second argument to drupal_static())
  • If the value returned from drupal_static() is set from a function (that is not a hook), you could as well directly call that function from the hook
1

If you need to pass a variable into a block, maybe you should try using template_preprocess_block ?

Inside this function you define your variable and afterwards in your block you use it.

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.