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

nevets’s picture

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.

himagarwal’s picture

Ok. But where to add these codes? In template.php file?

function hook_form_alter($form_id, &$form) {
  if (isset($form['type']) && $form['type']['#value'] .'_node_settings' == $form_id) {
    $form['workflow']['upload_'. $form['type']['#value']] = array(
      '#type' => 'radios',
      '#title' => t('Attachments'),
      '#default_value' => variable_get('upload_'. $form['type']['#value'], 1),
      '#options' => array(t('Disabled'), t('Enabled')),
    );
  }
nevets’s picture

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.

himagarwal’s picture

If I add this to suppose blog.module then it would be something like this:

function blog_form_alter($form_id, &$form) {
  if (isset($form['type']) && $form['type']['#value'] .'_node_settings' == $form_id) {
    $form['workflow']['upload_'. $form['type']['#value']] = array(
      '#type' => 'radios',
      '#title' => t('Attachments'),
      '#default_value' => variable_get('upload_'. $form['type']['#value'], 1),
      '#options' => array(t('Disabled'), t('Enabled')),
    );
  }

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?

nevets’s picture

Just write a small custom module, place the code in the .module file and you will need a .info file.

himagarwal’s picture

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?

nevets’s picture

You need a simplenews_custom.info something like this

name = Simple News Custom
description = Adds X to Simple News
dependencies = simplenews

The first two lines are required, the third says the module depends on the simple news module

himagarwal’s picture

thanks. will report to that if it worked.... :-)

himagarwal’s picture

I created a simplenews_theme.info file with following code:

name = Simplenews theme
description = "Themeing of simplenews"
dependencies = simplenews

and I created a simplenews_theme.module file with following code:

<?php
function simplenews_form_alter($form_id, &$form) {
  if($form_id == 'simplenews_block_form_1') {
 
  $settings = null;
   
  $form['mail']['#title'] = '';
    $form['display_mail']['#title'] = '';
  $form['action']['#type'] = 'hidden';
    $form['submit']['#button_type'] = 'image';
  if ($form['submit']['#value'] == 'Unsubscribe'){
    $form['submit']['#attributes'] = array(
'src' => base_path().path_to_theme().'/homepage_newsletter-remove.png',
'alt' => 'Remove me from your list.',
'class' => 'none'
);
} else {
    $form['submit']['#attributes'] = array(
'src' => base_path().path_to_theme().'/homepage_newsletter-submit.png',
'alt' => 'Sign Up!',
'class' => 'none'
);
}
  $settings = array('newsletter' => array(
      'emailaddress' => 'Email Address',
    ));
    drupal_add_js($settings, 'setting');
    drupal_add_js($module_path .'/newsletter.js', 'module');
  }

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?

nevets’s picture

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.

himagarwal’s picture

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........

nevets’s picture

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.

himagarwal’s picture

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........:-(

nevets’s picture

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.

himagarwal’s picture

I did exactly as you said but nothing happens......

function simplenews_theme_form_alter($form_id, &$form) {
drupal_set_message("Alter form: $form_id");

Thank you so much for trying to help me out with my problems but I think I can never get this working....

pal4life’s picture

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.

jebbushell’s picture

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 ;)

Jegan2668’s picture

Your function name should be simplenews_theme_form_alter($form_id, &$form) {