Hi all,
I need to create a custom hook for my custom module. Could any one help me on this ?

Comments

nevets’s picture

a hook is a function your module would invoke using module_invoke_all and would be used by your module to allow other modules the ability to modify your modules behavior

drupsforyou’s picture

Thank for your response. That's correct. But how can i implement that to my custom module.
Let say i am building a custom module, with in that module i have written some functions, like check_user etc..

My question is that how can i call this function using hook_check_user() from some others module, where hook is the custom module name.

Hope i am able to clear my point to u.

nevets’s picture

Any function can be called by any other PHP code so it sounds like you really just need to document the API your module provides. In the module that calls your function it is a good practice to declare a dependency in the .info file or if optional functionality use function_exists() to check for the existence of your function.

WorldFallz’s picture

Please don't post duplicate threads, I've deleted the dupe. you're already getting assistance here, why would you want to waste someone else's time starting over from scratch? sheesh....

aaustin’s picture

Although module_invoke_all is probably the preferred way to do it, you could also use module_implements(). I have had better luck doing it this way when I want to pass a variable by reference.

In your first module:

  function custom1_do_something(){
    $hook = 'myapi';
    $data = array('Text from custom1 module);
    foreach (module_implements($hook) as $module) {
      $function = $module . '_' . $hook;
      $function('process', $data);
    }
    return implode(' ', $data);
  }

In Second module:

function custom2_myapi($op, &$data){
  switch($op){
    case 'process':
      $data[] = 'Text from custom2 module';
      break;
  }
}
aaustin’s picture

I just came across this, it is a nice succinct write-up: http://erikwebb.net/blog/drupal-design-patterns/hooks

mahendra.sharma’s picture

U can make your own hook in following steps :---

1st step :-
in your module in any function use  module_invoke_all  and pass parmeters.This function will check for all other modules how have implemented ur hook and return the data.

module_invoke_all('sitenews', $param1, $param2, $param3);
 
here sitenews is the name of yourmodule_sitenews($param1, $param2, $param3);
other module will implement this using theirmodulename_sitenews($param1, $param2, $param3){}

2nd step:--

define your urmodule_sitenews($param1, $param2, $param3); in your own module.This should return somthing.like hook_menu 

function hook_sitenews() {
$content['report'] = array(
'#weight' => 0,
'#title' => t('Sample Title'),
'#body' => t('Sample content')
);
$content['another report'] = array(
'#weight' => 0,
'#title' => t('Another Sample Title'),
'#body' => t('More sample content')
);
return $content;
}

3rd step ---

implement in other module:-

like

user_sitenews();

4th step :-----


call  module_invoke_all('sitenews', $param1, $param2, $param3); in ur module and get data from it and play with data.THis data will come from all the modules who hav implemented this hook..like mail it as newsletter