Sorry I haven't blogged in a while, I have been working very hard on commercial D7 sites in my day job but haven't really come across anything spectacularly D7 I wanted to talk about.
So here's something unspectacular but quite important, and applies almost as much to D6 as D7.
Every module eats memory. Every x.module file (for enabled modules) gets loaded for every access (well, let's ignore caching). So anything that can be done to reduce the files loaded (size in this case) can only be a good thing. (Back in the day I wrote code for machines with 128K of memory of which 20K might be used for the screen, and another 32K for the OS - in those days you really had to think about keeping code as compact as possible.)
Drupal uses hooks to gather information from modules, and then it stores this information in the database caches (or other tables), after which it no longer calls these hooks - unless the caches get cleared and it has to build the information again.
Hooks like hook_menu, hook_theme, hook_permission and so on. But these hooks need to be available in the x.module file - and yet they are only called infrequently.
Solution? Put a minimal hook in the x.module file which loads another file (say x.registry.inc) and then calls the actual hook code which is in there. Like this:
In x.module:
function x_menu() {
module_load_include('registry.inc', 'x');
return _x_menu();
}
And in x.registry.inc:
function _x_menu() {
$items = array();
$items['my/path'] = array(
// my/path data );
return $items;
}
If a hook is called on pretty much every page there is no advantage in doing this. But if it's a hook that's called infrequently it's definitely worth it.
This is something I'd played with back in my D6 days but then stopped using. But then I saw another contributed module that was using it and thought "yep, I should be doing that". So I am and here it is.
hook_hook_info()
There is another solution using hook_hook_info() which allows you to define a file in which hooks can be found. This does not work for the majority of system modules because they use customised hook-calling methods. Only one system module - Tokens - has it defined so token code can be located in x.tokens.inc.
However the Block module only uses standard hook-calling functions and you could specify a file for block management using hook_hook_info_alter(); I wrote something about this here.
Showing posts with label hook_hook_info. Show all posts
Showing posts with label hook_hook_info. Show all posts
Wednesday, 7 September 2011
Sunday, 13 February 2011
Blocks and hook_hook_info()
As an ongoing attempt to keep the amount of code loaded for any given page down to a minimum the Drupal 7 developers came up with hook_hook_info() - which is a completely different hook from the one in Drupal 6.
What this hook does is allow a module to define a "group" for its hooks, so the system itself defines the group "tokens" for any token-related hook.
Which means that, when attempting to find implementations of the hook the core checks to see whether a module has a file modulename.tokens.inc - and if it does, it loads that before looking for the token hook. (And records if it finds it in the .inc file.)
This is all good and allows modules to put its token-related hooks in a file that isn't loaded unless its needed.
But 'tokens' is the only group defined for core hooks. Which seems a bit of a waste.
So one thing I've done is to intercept hook_hook_info_alter() and my own group for blocks, like this:
/**
* Implements hook_hook_info_alter().
*/
function mymodule_hook_info_alter(&$hooks) {
static $myhooks = array(
'blocks' => array('block_configure', 'block_view_alter', 'block_view', 'block_save', 'block_list_alter', 'block_info_alter', 'block_info')
);
foreach ($myhooks as $group => $items) {
foreach ($items as $hook) {
$hooks[$hook] = array('group' => $group);
}
}
}
And that's all you need. So you can now happily put your block-related hooks into modulename.blocks.inc. This is not future-proofed, you should really check to see whether the hook has been defined already before doing this, just to be completely safe.
One point worth noting is that there is now a hook_view_BLOCK_DELTA_alter() as well, which you can't put in a group specifically because you'd need an entry for every defined block. However it's not a problem because that hook is called immediately after hook_view_alter() - which will ensure that the .inc file is loaded first.
Don't bother trying to put node hooks into a separate file - it won't work because the node module just goes its own way and doesn't use the hook_info data.
What this hook does is allow a module to define a "group" for its hooks, so the system itself defines the group "tokens" for any token-related hook.
Which means that, when attempting to find implementations of the hook the core checks to see whether a module has a file modulename.tokens.inc - and if it does, it loads that before looking for the token hook. (And records if it finds it in the .inc file.)
This is all good and allows modules to put its token-related hooks in a file that isn't loaded unless its needed.
But 'tokens' is the only group defined for core hooks. Which seems a bit of a waste.
So one thing I've done is to intercept hook_hook_info_alter() and my own group for blocks, like this:
/**
* Implements hook_hook_info_alter().
*/
function mymodule_hook_info_alter(&$hooks) {
static $myhooks = array(
'blocks' => array('block_configure', 'block_view_alter', 'block_view', 'block_save', 'block_list_alter', 'block_info_alter', 'block_info')
);
foreach ($myhooks as $group => $items) {
foreach ($items as $hook) {
$hooks[$hook] = array('group' => $group);
}
}
}
And that's all you need. So you can now happily put your block-related hooks into modulename.blocks.inc. This is not future-proofed, you should really check to see whether the hook has been defined already before doing this, just to be completely safe.
One point worth noting is that there is now a hook_view_BLOCK_DELTA_alter() as well, which you can't put in a group specifically because you'd need an entry for every defined block. However it's not a problem because that hook is called immediately after hook_view_alter() - which will ensure that the .inc file is loaded first.
Don't bother trying to put node hooks into a separate file - it won't work because the node module just goes its own way and doesn't use the hook_info data.
Subscribe to:
Posts (Atom)