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

Solutions

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.

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

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: Drupal 7 / Blocks

Similar questions

How can I disable default blocks in Drupal 7 without touching the Blocks Config admin?
I'm trying to disable the search form, navigation, and user login blocks from being enabled by default. My main goal is to not touch the admin at all because I want to version control this. Is there a way to disable the blocks with some sort of hook? I'm newb when it comes to php and coding for Drupal so please be kind.
Custom made blocks not getting displayed in Blocks page
I am new to Drupal. I tried to write custom module(Sample).. After completion of Module, Enabled at Module. But it is not showing at Block Page. Please check the below code and correct me.. first.info first.module
Does [Cache blocks] in Performance means that blocks generated by Views are also cached?
I am working on an old Drupal 6 project. My goal is to boost the performance of the website as much as I can. Among other things I have done, I have already enabled cache Configuration > Development > Performance My question is, does [Cache blocks] in Performance means that blocks generated by Views are also cached or I have to enable caching insid...
When adding Custom Blocks the weight is changed for the rest of the blocks
We are using custom blocks very extensively. We are also using block visibility groups and Domain Access. At this point when we make a new block it changes the weight of many of the other blocks at what seems to be random intervals. Has anyone else had this issue?
Admin page to show 2 blocks (or page+block) with 1 filter that affects both blocks
I am new to Drupal and would love some guidance on how best to achieve the following: An admin page that has (top-down): - a date selector, defaulting to today's date - a section displaying Arrivals for the date selected (this selects custom content type 'Bookings' with filter of arrivalDate = dateselected) - a section displaying Departures for the...
Create page of blocks only / Create a node of blocks
Sometimes I use a basic page as a place holder for the purpose of creating a node to hold blocks. So I create a basic page and link it to a menu, then I use the blocks interface to configure certain blocks to display on that page only. The problem with this approach is twofold. Firstly I don't need the page, but secondly when a user tries to edit t...

Also ask

We use cookies to deliver the best possible experience on our website. By continuing to use this site, accepting or closing this box, you consent to our use of cookies. To learn more, visit our privacy policy.