Is it possible to programmatically pass parameters to a block?
4 Answers
Per the other answer, blocks are 'dumb' and don't have the ability to have arguments 'passed in'. They can, however, grab arguments from the context of the page, like node IDs, current user etc.
You have two 'easy' choices really:
- Write your own block: It is pretty easy, if you can do a little PHP. Create a module and implement hook_block(). There are many examples on Drupal.org.
- Use the Panels and/or the Views module: Panels allows you to create panes that can have all manner of variables and contexts passed in, BUT without a special theme the block will have to live in the content area, not a Drupal region; so it depends on your site, and where you want the block. Views can create blocks too, and receive arguments from the page URL, current user and also custom code via the Views PHP module. Views and Panels work together very well.
-
1Generally too with panels, if your theme supports it, you can turn off all other blocks and display other content that will be context aware as "pseudo" blocks.– MalksCommented Dec 20, 2011 at 14:03
For the specialized (but widespread) case where your block is a display of a View, you can use views_embed_view
to render the block with parameters that it gets via the contextual filters of the View (that is, normally from the URL).
For example:
print views_embed_view('news', 'panel_pane_1', 'world');
Any parameters after the second will be passed as contextual filter values to the block.
Source: Rewritten from this answer by David Thomas, licenced CC-BY-SA 3.0
You can't pass anything programatically to a block AFAIK. You can allow a block to be configurable (using hook_block_configure in D7). Alternatively you could create your own theme function, and you can pass arguments to that. What is it you're trying to do?
Configure contextual filter choose "Provide default value" type => "php code" and use the argument you want. ex: return arg(1);