Monday, 28 February 2011

The trouble with block_view_MODULE_DELTA_alter

If you want to modify a specific menu block using hook_block_view_MODULE_DELTA_alter() you are out of luck. Let's say you have a menu called "Footer menu" and you want to change its styling so you set up a function like this:

function mymodule_block_view_menu_menu_footer_menu_alter() {}

That is not a mistake: the first "menu" is the module name, and the menu module adds "menu" to the front of the menu name converted to lower case, so the delta for my "Footer menu" is "menu-footer-menu".

But this function will never get called (as of 7-rc4). Why? Because the menu module uses hyphens in the delta name, and the block module does not convert these hyphens to underscores so it tries to call: mymodule_block_view_menu_menu-footer-menu_alter() - which will fail, of course.

You have two choices: Either use only hook_block_view_alter() and check you've got the right block before changing it, or patch block.module until it gets fixed in core.

I've already issued a bug report here with the fix, so you can use it if you want to.

No comments: