I need to add a block to an existing Drupal site and would like to if possible automate the process using hook_update_N or something similar.

Specifically I'm referring to the position and region of the block relative to adjacent and following blocks. Is there a Drupal standard method to this process or do I simply need to use SQL to achieve this.

Is there a way to force a block in between others with some trick with the weights, or will I need to actively increment the weight of blocks below this in the same region?

Thanks for any advice!

  • You question is not clear for me, What kind of block you need to create and what is association with hook_update_N? Are you concerned about the position and region of any block or a specific block you want to create? Jun 16, 2011 at 5:30
  • I have a contrib module that adds a block, so I am using hook_update_N and module_enable to enable that module when i run update.php. However there are block settings that I also want to apply such as the theme/region/weight as part of the update process. Forcefully setting the weight of the block however will make it clash with the weight of another block (since I want to insert it between 2 existing blocks).
    – DanH
    Jun 16, 2011 at 7:04

1 Answer 1


You could do this if you like, but this is an unstable and hacky way of fixing your problem. The thing is that blocks can be rearranged at any time so using an update function to sort the block weights is not the best way to deal with this.

What you could do instead is to use the context module to deal with blocks. If offers a better interface to control blocks and allow you to display the same block in different regions. It also gives you a more advanced control of when blocks should be displayed, like display on all nodes of type x, or on these views etc.

In addition to all the nice features described above, contexts can be exported as features. This mean that you can with a click of a button create a module that have your block setup stored in code, including weights etc. If those blocks were to be rearranged, you could revert the feature back to the old state.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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