How to make hook in custom module to execute only on condition How to make contributed module hook execute only if conditions are met. Those functions seem to get automatically invoked. Environment is a custom module.

examples:
1.

if ($condition) {

   function mymodule_modulename_alter ($parameters) {
     //altering code here
   }

}


2.

    if ($condition) {
           //if condition is met, i call the function included in this custom module
           mymodule_modulename_alter ($parameters)
      }

  function mymodule_modulename_alter ($parameters) {
         //but this function will get auto-invoked! I want it run only if $condition is met.
       }


Thanks
Let the hook be called, but put your operations(that your hook executes) inside your required condition.

function mymodule_modulename_alter($parameters) {
  if ($condition) {
    // Your code.
  }  
}