Create own Hooks and allow intervention from other modules

Drupal APIs and Drupal framework allows developer to provide means for other modules to interact or intervene using hooks. This can be done by simply using an API called module_invoke_all(), a comprehensive detail for this function can be found here. Details about hooks in drupal can be found here

I'll try to explain this in steps by using small example:

  • Create a module called "math", definitely this will have atleast two file in it i.e. math.info and math.module
  • In math.module, i have a function math_hello_world()

function math_hello_world(){
  print "Hello World";
  module_invoke_all('math', 'after print', $node);
}

  • By calling module_invode_all() function with arguments mean its looking for all enabled modules that are implementing hook_math() where 'hook' replaces module name e.g. anymodule_math(). Hence this will allowing other modules to include their functionality after "Hello World" is printed. e.g.

function anymodule_math($op, $node) {
  switch ($op) {
    case 'after print':
      // Add your code here, which will called after Hello World is printed
      print "After Hello World in Module 'Any Module'"
      break;

    case 'transition post':
      break;
  }
}