I loaded a block subject and content with module_invoke() and used render() for rendering them. I want to use contextual links for this block. How can I render them too?

This is the code I am using insde the page--front.tpl.php template.

$block = module_invoke('block', 'block_view', '4);
print render($block);

What code should I instead use?

3 Answers 3

$block = block_load('module', 'delta');
print drupal_render(_block_get_renderable_array(_block_render_blocks(array($block))));

Too bad we have to use private functions from the block module (_*()) to achieve this.

  • So for the OPs example it would be: $block = block_load('block', '4');
    – Cameron
    Jan 7, 2020 at 7:32

Bart's answer will give a Strict Standards warning (enabled by default in PHP 5.4). To fix this just use an intermediate variable for the renderable array:

$block = block_load('module', 'delta');
$renderable_array = _block_get_renderable_array(_block_render_blocks(array($block)));
print drupal_render($renderable_array);

The reason the warning occurs is because the drupal_render function expects its parameter to be a reference. It has the signature drupal_render(&$elements). For more information see the answer to this similar question.


Contextual links are helpful and really easy to implement. For your use-case you need to implement hook_block_view_alter() to change #contextual_links item to suit your needs. These references will help you out:

GL :)

  • 1
    What OP means is that he programmatically displays the block, but contextual links aren't added.
    – Bart
    Sep 17, 2013 at 10:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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