function contextual_links_example_block_view in Examples for Developers 7
Implements hook_block_view().
Related topics
File
- contextual_links_example/
contextual_links_example.module, line 186 - Shows how to use Drupal's contextual links functionality.
Code
function contextual_links_example_block_view($delta = '') {
if ($delta == 'example') {
// Display our module's content inside a block. In a real use case, we
// might define a new block for each object that exists. For the sake of
// this example, though, we only define one block and hardcode it to always
// display object #1.
$id = 1;
$object = contextual_links_example_object_load($id);
$block['subject'] = t('Contextual links example block for object @id', array(
'@id' => $id,
));
$block['content'] = array(
// In order to attach contextual links, the block's content must be a
// renderable array. (Normally this would involve themed output using
// #theme, but for simplicity we just use HTML markup directly here.)
'#type' => 'markup',
'#markup' => filter_xss($object->content),
// Contextual links are attached to the block array using the special
// #contextual_links property. The #contextual_links property contains an
// array, keyed by the name of each module that is attaching contextual
// links to it.
'#contextual_links' => array(
'contextual_links_example' => array(
// Each element is itself an array, containing two elements which are
// combined together to form the base path whose contextual links
// should be attached. The two elements are split such that the first
// is the static part of the path and the second is the dynamic part.
// (This split is for performance reasons.) For example, the code
// below tells Drupal to load the menu item corresponding to the path
// "examples/contextual-links/$id" and attach all this item's
// contextual links (which were defined in hook_menu()) to the object
// when it is rendered. If the contextual links you are attaching
// don't have any dynamic elements in their path, you can pass an
// empty array as the second element.
'examples/contextual-links',
array(
$id,
),
),
),
);
// Since we are attaching our contextual links to a block, and the Block
// module takes care of rendering the block in such a way that contextual
// links are supported, we do not need to do anything else here. When the
// appropriate conditions are met, the contextual links we have defined
// will automatically appear attached to the block, next to the "Configure
// block" link that the Block module itself provides.
return $block;
}
}