Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
By himagarwal on
I've been using drupal for last couple of months and still learning. I came around with "hook" word several times and this time I have to implement it.
I know implementing function override in template.php file but don't know how and where to use hooks?
Will really appreciate if someone can point me on the right direction. ;-)
Comments
hooks are implemented in
hooks are implemented in modules and you can read about the core hooks here: http://api.drupal.org/api/group/hooks/5. Basically they provide a way for a module to extend the functionality of another module. For example the 'node' module provides the core features of a node (content type). You can use CCK or a custom module to create a custom content. You can also use the nodeapi hook to extend the functionality of any content type. The are core hooks for working with node, users, taxonomy and more. Contributed module may also add addition hooks your module can implement.
Ok. But where to add these
Ok. But where to add these codes? In template.php file?
They would go in a module
They would go in a module and 'hook' would be replaced by the actual module name. On the other hand it looks like you simply want to enable the upload module.
If I add this to suppose
If I add this to suppose blog.module then it would be something like this:
The problem is, after I add this hook/code in the core or contributed module and in case I need to upgrade my modules or drupal itself then it would get deleted/overwrite. Is there a better option for it?
I used above code just for an example. The main hook that I want to implement is at http://www.ubercart.org/comment/23163/Re_NutriPrima_Launch but don't know where to add it?
Just write a small custom
Just write a small custom module, place the code in the .module file and you will need a .info file.
Thank you nevets for your
Thank you nevets for your great help.
Just one thing to clear up: while creating a small custom module which starts with
<?php
but it doesn't end with?>
. So by just keeping the following code in a module file called simplenews_custom.module with this code http://www.ubercart.org/comment/23163/Re_NutriPrima_Launch just under<?php
and creating a .info file similar to core simplenews module with just a name change.....will it work?You need a
You need a simplenews_custom.info something like this
The first two lines are required, the third says the module depends on the simple news module
thanks. will report to that
thanks. will report to that if it worked.... :-)
I created a
I created a simplenews_theme.info file with following code:
and I created a simplenews_theme.module file with following code:
and copied both of files in /sites/all/modules/simplenews_theme folder
But when I enable it from /admin/build/modules I'm getting following error:
Parse error: syntax error, unexpected $end in E:\xampp\htdocs\drupalnew\sites\all\modules\simplenews_theme\simplenews_theme.module on line 29
Is there something wrong that I did when creating hook for simplenews module?
You are missing a closing
You are missing a closing '}', you have 4 {'s (function two if's, one else) but only three }'s.
A small but important side node, your function name should reflect your module name so the function name should be simplenews_theme_form_alter.
thanks again "nevets". I
thanks again "nevets".
I added the missing closing braces as you aid and changed the hook name to simplenews_theme. Now it doesn't shows any problem, but I didn't seen any changes in my simplenews........
Well the first thing to do
Well the first thing to do is make sure you have the form id correct, inside the out 'if' statement you might add
drupal_set_message("About the alter the simplenews form")
, visit a page with the form and see if your message appears in the message area. You might also check to see if your js code is being added to the page.*._.*
I checked the html source for "js code" but it doesn't appear anywhere in the simplenews form page. I tried drupa_set_message inside the 'if' code but it showed prase error (syntax) warnings.
Since simplenews_custom module is enabled and the js code is not showing up I think the form id is wrong as you said........:-(
Make sure there is a
Make sure there is a semicolon (;) after the call to drupal_set_message(). Also as the first line of the function (before the 'if') you might add
drupal_set_message("Alter form: $form_id");
and visit a page with the form again.I did exactly as you said
I did exactly as you said but nothing happens......
Thank you so much for trying to help me out with my problems but I think I can never get this working....
A handson video tutorial from Drupal chicago 2011
Hi,
Here is a a very nice videocast which will surely help in understanding more. Hopefully you will be successful this time
http://chicago2011.drupal.org/sessions/introduction-module-development
Thanks for the question though and the following discussions.
So, in event-driven terms,
So, in event-driven terms, the hook code in "my" module is a self-registering event-handler for an event that is fired by the related function on the "master" module. Hope this saves those who have already had the event-driven migraine from having another ;)
Your function name should be
Your function name should be simplenews_theme_form_alter($form_id, &$form) {