0

Anyone ever accomplished building a module that manages to have block configuration per instance?

The main issue with blocks is that each block does only one thing no matter where it shows up, you can't have the same block taking different parameters based on eg region.

  • That's because per-theme region placement is itself a parameter of the block's configuration. Having every or any parameter configurable based on every or any other available parameter would get very messy very quickly. What are you trying to accomplish specifically? A block can only be in one region per theme so are you looking for a way to get different config per theme? – Clive Aug 9 '14 at 15:16
  • Not sure what your use case is but have a look at drupal.org/project/panelizer which let you place blocks in different region per entity (per node etc). – Pontus Nilsson Aug 10 '14 at 14:29
0

If you just need the functionality, you can try MultiBlock to see if it fits your needs:

"Each block has a 1:1 relationship with its region, weight, visibility (and other) settings. This means that it is impossible to have blocks in multiple regions or to have blocks that have different settings on different pages. MultiBlock module solves this problem by allowing you to create multiple Block Instances of already existing blocks."

However, you question is about being able to accomplish building a module that manages this. So, yes, I recently did something similar. I needed a homepage with blocks displaying a list of articles, each one with custom options (e.g. different categories, with / without images, etc.) and also in different regions across the site. I created one module, and then instead of having each block with a harcoded-fixed name, I generate the block's names ($delta) dynamically, like this:

function MY_BLOCK_block_info() {
  // Instead of having a static number of blocks, let's choose how many
  // we want, via the administration.
  $blocks_qty = variable_get('MY_BLOCK_blocks_qty');
  for($i = 1; $i <= $blocks_qty; $i++) {
    $blocks['MY_BLOCK-' . $i] = array(
      'info' => t('My block ' . $i), 
      'cache' => DRUPAL_CACHE_PER_ROLE,
      'region' => BLOCK_REGION_NONE,
    );
  }
  return $blocks;
}

Then you will have different names for each block (MY_BLOCK-1, MY_BLOCK-2, etc). So you can individually configure them like this:

function MY_BLOCK_block_configure($delta = '') {
  $form = array();
  $parts = explode('-', $delta); 
  $block_number = $parts[1];

  // Your form here...
  $form['MY_BLOCK_' . $block_number . '_title'] = array( ...

So, in the general Configuration page you get to choose how many blocks you want to generate. And then, in the individual block's configuration page you choose the individual block's settings.

| improve this answer | |
  • I tried the multiblock crashed my installation :P guess the install file wasn't run. – kristian nissen Aug 11 '14 at 11:55
  • You can always try it on a fresh installation, to test if it has the behaviour you are looking for. Then you can decide if using it or trying something for yourself. – typologist Aug 11 '14 at 15:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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