Integrating your custom module with MultiBlock in Drupal 7

Posted on 25/08/2016

One of the "late to the party" features of Drupal 8 is the ability to assign the same block to multiple locations in the theme. Drupal 7 still lacks this feature in the core, but it's easy to achieve it by using a contributed module such as MultiBlock.

While it works out of the box with all modules that provide blocks, you will need to modify your code in order to allow administrators to have separate configuration for each block instance. The official documentation for MultiBlock module is lacking and there is no straightforward example on what exactly you need to do in code. In this article I'll show how to integrate your custom module with MultiBlock and have different configuration for each instance of your block.

To continue, you can clone the Blank Module boilerplate. It will make it easier to create a new empty module and start coding immediately.

MultiBlock adds an extra page in the block admin section where you can create instances of existing blocks. Out of the box it works with all blocks but it does not allow you to have different configuration between instances. To achieve this, we must let MultiBlock know that our module can be integrated with it and add some extra code for fetching information related to the current instance.

Here's what we need to do in code:

  1. Implement hook_block_info(). To integrate with MultiBlock, we have to pass one extra key in the block array (mb_enabled) and set its value to TRUE.
    This will make MultiBlock instance ID available to us in hook_block_view() and hook_block_configure(), so we can use it for fetching configuration specific to the current instance.
  2. Implement hook_block_view(). MultiBlock will pass us instance ID only for blocks that integrate with MultiBlock.
  3. Implement hook_block_configure(). Just like above, we will get one extra argument with instance ID. Since the instance ID will not be provided by MultiBlock in the hook_block_save(), we will have to store it in the form as a hidden value.
  4. Implement hook_block_save(). Here we will just check if the instance ID is passed in the form, and store the settings under appropriate ID.

Below is full code. Do note that the $edit value provided by MultiBlock module will contain the value in the hidden field format (for whatever reason).

  1. /**
  2.  * Implements hook_block_info().
  3.  */
  4. function YOUR_MODULE_block_info() {
  5.   $blocks = array();
  6.  
  7.   // This is the block name.
  8.   $blocks['sample']['info'] = t('Sample Block');
  9.   // We have to enable administrative interface for configuring our block.
  10.   $blocks['sample']['properties']['administrative'] = TRUE;
  11.   // Enable MultiBlock integration.
  12.   $blocks['sample']['mb_enabled'] = TRUE;
  13.  
  14.   return $blocks;
  15. }
  16.  
  17. /**
  18.  * Implements hook_block_view().
  19.  */
  20. function YOUR_MODULE_block_view($delta = '', $edit = NULL) {
  21.   $block = array();
  22.   switch ($delta) {
  23.     case 'sample':
  24.       $block['subject'] = NULL;
  25.       // Check if multiblock module is in use.
  26.       $prefix = '';
  27.       if ($edit && isset($edit['multiblock_delta']['#value'])) {
  28.         $prefix = '_' . $edit['multiblock_delta']['#value'];
  29.       }
  30.       $block['content'] = variable_get('YOUR_MODULE_block_config' . $prefix . '_text');
  31.  
  32.       return $block;
  33.   }
  34. }
  35.  
  36. /**
  37.  * Implements hook_block_configure().
  38.  */
  39. function YOUR_MODULE_block_configure($delta = '', $edit = NULL) {
  40.   $form = array();
  41.  
  42.   switch ($delta) {
  43.     case 'sample':
  44.       // If MultiBlock is installed, pass the delta as a hidden value so we can
  45.       // retrieve the correct variable from the database in hook_block_save()
  46.       // implementation.
  47.       $prefix = '';
  48.       if ($edit && isset($edit['multiblock_delta'])) {
  49.         $form['multiblock_delta'] = array(
  50.           '#type' => 'value',
  51.           '#value' => $edit['multiblock_delta']['#value'],
  52.         );
  53.         $prefix = '_' . $edit['multiblock_delta']['#value'];
  54.       }
  55.       $form['sample'] = array(
  56.         '#type' => 'fieldset',
  57.         '#title' => t('Block configuration'),
  58.       );
  59.       $form['sample']['text'] = array(
  60.         '#type' => 'textfield',
  61.         '#title' => t('Enter some text'),
  62.         '#required' => TRUE,
  63.         '#default_value' => variable_get('YOUR_MODULE_block_config' . $prefix . '_text'),
  64.       );
  65.  
  66.       return $form;
  67.   }
  68. }
  69.  
  70. /**
  71.  * Implements hook_block_save().
  72.  */
  73. function YOUR_MODULE_block_save($delta = '', $edit = array()) {
  74.   switch ($delta) {
  75.     case 'sample':
  76.       $prefix = ($edit['module'] == 'multiblock') ? '_' . $edit['delta'] : '';
  77.       variable_set('YOUR_MODULE_block_config' . $prefix . '_text', check_plain($edit['text']));
  78.       break;
  79.   }
  80. }

After enabling your custom module, you can go to Structure -> Blocks -> Instances and create as many instances of your block as you need. Each one of them can have different text.

Bonus points: do remember to delete your variables when your module is uninstalled from the system. To do that, the following code would go to your .install file:

  1. /**
  2.  * Implements hook_uninstall().
  3.  */
  4. function YOUR_MODULE_uninstall() {
  5.   // This is easiest approach because it will remove all variables at once.
  6.   db_query("DELETE FROM {variable} WHERE name LIKE :variables", array(':variables' => 'YOUR_MODULE_%'));
  7. }

See also: