0

I had some similar questions here, but would like ultimately to know best practices and possibilities for this case.

I have my custom module, in which I use two contrib modules hooks. As I understood, hooks are self-invoked (looking from my module point of view), and as soon as I am declaring it, they get executed.

That said, how I can get return of an hook function ?

I am trying to make $foo1 available in other hook and vice versa for $hook2. I would like to avoid oop solutions if possible.

Example:

function mymodule_contribmodulehook1 ($parameter1, $parameter2) {
  //$foo2 here somehow ? 
  $foo1 = 1;
  return $foo1; 
}

function mymodule_contribmodulehook2 ($parameter1, $parameter2) {
  //$foo1 here somehow ?
  $foo2 = 1;
  return $foo2; 
}
  • 1
    what's the point to have variables after the return statement in a function? – AjitS Jan 29 '13 at 8:08
  • sorry, just edited example. – NenadP Jan 29 '13 at 8:16
  • 1
    No need to say sorry. I was just sayin ;-) – AjitS Jan 29 '13 at 8:29
1

This is more like coding problem..

I will create a function that will store the value, the function has two parameters hook name and value. The function will both set and return values based on certain condition. Values will be set corresponding to hook names. While calling the function if we are passing an argument blank then it will return value, if we call the function and pass both arguments then it will set value.

Here's the sample code:

function mymodule_contribmodulehook1 ($parameter1, $parameter2) {
  // $foo2 here somehow ?
  $foo2 = to_and_fro('', 'hook1');
  $foo1=1;
  to_and_fro($foo1, 'hook2'); // Required by hook2.
  return $foo1;
}

function mymodule_contribmodulehook2 ($parameter1, $parameter2) {
  // $foo1 here somehow ?
  $foo1 = to_and_fro('', 'hook2');
  $foo2=1;
  to_and_fro($foo2, 'hook1'); // Required by hook1.
  return $foo2;
}

/**
 * $variable
 * Check whether $variable is present. If present we set the value, otherwise
 * return value.
 *
 * $hook
 * Hook name.
 */
function to_and_fro($variable, $hook) {
  if (!isset($variable)) {
    switch ($hook) {
      case 'hook1':
        return $hook2_var;
      case 'hook2':
        return $hook1_var;
    }
  }
  else {
    switch ($hook) {
      case 'hook1':
        $hook1_var = $variable;
        break;
      case 'hook2':
        $hook2_var = $variable;
        break;
    }
  }
}

Also this will help you if you have more than two hooks, all you need is to increase the number of case and variable inside the custom function.

Hope this helps you!

  • This seems very re-usable, though it needs a little brain-racking to accept :) I think this is most flexible solution. What do you think about other two methods provided here ? Thanks for your examples, they are very descriptive and valuable. – NenadP Jan 29 '13 at 19:43
  • Sithu has updated his answer, he was trying to do this using global varibales at first place. I was also thinking the same, but then I thought is it okay to use global variables in Drupal? I have not read the coding standards fully though :p.. Now I see that he is doing using variable_get() and variable_set(), this looks good though.. I think that is a better solution to your problem.. What do you think? – subhojit777 Jan 30 '13 at 4:19
  • Penten is calling the hook from inside another hook, also in Drupal we call hooks using module_invoke(). Whatever.. if you use his solution it will execute a hook twice (as you said that in comment) .. and it will execute code inside hook twice quite unnecessarily.. Also you do not have such requirement, do you? – subhojit777 Jan 30 '13 at 4:23
  • Thank you for replies. Solution with get and set works, but i am afraid to use them because they are of more persistant nature and those vars may get cached. I need those vars very temporarely and they need to be ultra reliable as they are used in shopping cart per user. To avoid need for variable transport, i am sending them now by attaching them in object that i found present in both functions output. I use, for example, globals when i need $user object in tpl files. Still need to do research how much bad it is, vs. gain :). As per question, i will accept yours as it seems most flexible. Ty – NenadP Jan 30 '13 at 7:49
  • @subhojit777, you can use global variables as you like. It is up to you. Drupal itself has its own global variables such as $user, $theme, etc. It depends on your requirement. – Sithu Jan 30 '13 at 8:03
4

Yes, if you implement a hook provided by a contrib module, it is invoked by that module function. You can declare those two variables in global scope so that they can be accessible each other in the functions.

<?php
// mymodule.module
global $foo1;
global $foo2;

function mymodule_contribmodulehook1 ($parameter1, $parameter2) {
  global $foo2;
  //$foo2 here 
  $foo1 = 1;
}

function mymodule_contribmodulehook2 ($parameter1, $parameter2) {
  global $foo1;
  //$foo1 here
  $foo2 = 1;
}

If you don't want to use global scope for your variables, you can save and retrieve them alternatively in your Drupal database using variable_set() and variable_get().

function mymodule_contribmodulehook1 ($parameter1, $parameter2) {
  $foo2 = variable_get('foo2');
  $foo1 = 1; 
  variable_set('foo1', $foo1);
}

function mymodule_contribmodulehook2 ($parameter1, $parameter2) {
  $foo1 = variable_get('foo1'); 
  $foo2 = 1;
  variable_set('foo2', $foo2);
}

Note that, for both approaches, you don't need to return each of them as long as you don't have any other purpose.

  • This seems simple, but use global, that is deemed unholy by the part of community. What do you cay of the other approaches proposed here ? Thanks for you tip! – NenadP Jan 29 '13 at 19:39
  • @HoverFusion, please check my updated answer. – Sithu Jan 30 '13 at 3:33
  • Thanks! I did use that approach before, but i have concerns about possible caching of those vars. I need them to be highly responsible :) . Both of your answers are valuable. – NenadP Jan 30 '13 at 7:40
  • @HoverFusion, the other way is to use session. – Sithu Jan 30 '13 at 8:05
1

You can call a hook yourself, so you could write

function mymodule_contribmodulehook1 ($parameter1, $parameter2) {
  $foo2 = mymodule_contribmodulehook2($parameter1, $parameter2);
  $foo1 = 1;
  return $foo1 + $foo2; 
}

function mymodule_contribmodulehook2 ($parameter1, $parameter2) {
  // don't call mymodule_contribmodulehook1 here though, it would result in an infinite loop!
  $foo2 = 1;
  return $foo2; 
}

You might want to refactor the above though:

function mymodule_getFoo1($a, $b) { ... }
function mymodule_getFoo2($a, $b) { ... }

function mymodule_contribmodulehook1 ($parameter1, $parameter2) {
  $foo1 = mymodule_getFoo1($paramater1, $paramater2);
  $foo2 = mymodule_getFoo2($paramater1, $paramater2);
  ...
}

function mymodule_contribmodulehook2 ($parameter1, $parameter2) {
  $foo1 = mymodule_getFoo1($paramater1, $paramater2);
  $foo2 = mymodule_getFoo2($paramater1, $paramater2);
  ...
}
  • I have concerns of avoiding invoking hooks two times accidentaly. I will experiment with your examples and see what it means in my code. Thanks! – NenadP Jan 29 '13 at 19:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.