Drupal - How do I render contextual links for blocks I render in my code?

$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.


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:

  • http://drupal.org/documentation/modules/contextual
  • http://dominiquedecooman.com/blog/drupal-7-tip-add-contextual-links-anything

GL :)

Tags:

7

Blocks