4

When there is a need to call a function inside a module, some of you say- it is good to use module_invoke() inside the template pages to call the function(it looks great-since I can pass arguments too).

Using Drupal 7. So for the first time, it is okay. Even for the second time, it makes a bit extra effort in the code, but somehow I can manage it.

Now it is invoking again and again, the same module. Say if the module is block, and the function is hook_block_view()- I doubt, it could affect the Drupal site performance(the time for loading the page contents), what do you think? Is there a solution for this problem?

<?php
  $header_block = module_invoke('block', 'block_view', '2');
  print drupal_render($header_block);
  $news_block = module_invoke('block', 'block_view', '3');
  print drupal_render($news_block);
  ...
  ...
  $footer_block = module_invoke('block', 'block_view', '12');
  print drupal_render($footer_block);
?>
  • Is it a good idea to use the module_invoke() every time when I have to call hook_block_view() with different arguments?
  • Is there any other function that can be used for calling the same function or any other function in the module after one- module_invoke()?
  • Is there any other way of doing thing?
2

You're calling the same function over and over, that's true. But the parameters are different, so from a pure programming point of view, the processing could be completely different. I don't see another option to approach this.

  • Is it a good idea to use the module_invoke() every time when I have to call hook_block_view() with different arguments? Yes
  • Is there any other function that can be used for calling the same function or any other function in the module after one- module_invoke()? Not that I'm aware of
  • Is there any other way of doing thing? I don't think so

More specifc, some functions allow passing an array of arguments instead of one. Eg node_load_multiple allows to pass 1 nid or whole list of nids. But hook_block_views doesn't allow this.

  • @Marten: Thank you for your time on this answer. How good is the call for invoking a module every time(when it is already invoked)? But leave the pure programming concepts, just basics. When I started using functions(procedures), it helped me to avoid the repeated lines of code with just a function call. And there should be a function for all such functionalities. Now if this answer is correct, then Drupal really lacks it. – Ataboy Josef Jan 18 '15 at 12:39

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.