| 5 core.php | hook_init() |
| 6 core.php | hook_init() |
| 7 system.api.php | hook_init() |
| 8 system.api.php | hook_init() |
Perform setup tasks for non-cached page requests.
This hook is run at the beginning of the page request. It is typically used to set up global parameters that are needed later in the request. When this hook is called, the theme and all modules are already loaded in memory.
This hook is not run on cached pages.
To add CSS or JS that should be present on all pages, modules should not implement this hook, but declare these files in their .info file.
See also
Related topics
14 functions implement hook_init()
1 invocation of hook_init()
File
- modules/
system/ system.api.php, line 1826 - Hooks provided by Drupal core and the System module.
Code
function hook_init() {
// Since this file should only be loaded on the front page, it cannot be
// declared in the info file.
if (drupal_is_front_page()) {
drupal_add_css(drupal_get_path('module', 'foo') . '/foo.css');
}
}
Login or register to post comments
Comments
Adding CSS from a .info file
To follow the above suggestion :
CSS files can be added to a
.infofile using the following format:stylesheets[all][] = node.cssSee the developer's guide: "Writing .info files (Drupal 7.x)" for specifications on the .info file.
Also see the D6 theming guide: "Adding style sheets".
Or subtheme
Even core themes may be subthemed: Creating a Subtheme. I used this technique to add a CSS file for admin functions on a site with the Seven admin theme.
D6 heads up
In D6 you cannot add stylesheets in the .info file. So contrary to the documentation recommendation, you may have to use hook_init.
This documentation is for D7
This documentation is for D7 only. Here you can found D6 documentation about hook_init.
Although you are right with
Although you are right with that (there is a different page for D6), this is not really obvious unless checking the URL:
This should be fixed, btw, and either split properly or merged to just one page, eventually with aliases.
D6, adding js with hook_init()
Yes, that is basically right. And somehow, again, not.
hook_init() will be invoked on every full bootstrap. Even if your module does not have anything of worth to add to the actual page request. This results in unnecessary processing time unless your module is involved in literally *every* page request. I could tell of few modules that really do so, block maybe one of these rare.
So in most cases, it is a good idea to add javascript exactly when it is needed. Assume your module implements a block that will only appear on user profile pages (even more, only for users with the role "foo") and contents some fancy jQuery effect based random tips. That said, why would you want to have not just all jQuery stuff added, but also an additional function call and, in the worst case, even unnecessary database queries or other stuff on literally every request, be it even just an AHAH callback for other modules?
Just imagine an average community site with, say, an average of 50 parallel visitors and 20 contrib modules enabled each of which just adds a few JS variables and calls for one or two string translations. Things sum up really quick.
And this is why there are many warnings to not use hook_init unless really inevitable.
As for the example above, the best place for dispatching all effect related javascript files and setting related to this block, will likely be in hook_block and only when $op is set to 'view'.
This is just an example, but hopefully it helps figuring out a bit.
external js
External Javascript can not seem to be added through the info file using the line 'scripts[] = http://yada.com/stuff'
Add a script tag to your template file
You can just add a script tag to your template file ( page.tpl.php in D6 and html.tpl.php in D7).
LIke this for example:
<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?key=YOUR-MAPPING-KEY&sensor=false"></script>