What or where are the rules which refine whether I can call a function in another module or call it via module_invoke? Also does module_invoke allow variable references to be passed to the invoke module, which is to say changed in the module invoking the other module?

Comments

jferber’s picture

I'm new and was just looking at the core to answer this question myself. The answer is probably coming too late to help you but I may as well post it for anyone else who's curious.

Module_invoke doesn't do much more than call the function but it does impose Drupal's "_hook" naming convention and it checks that the function exists before calling it.

Module_invoke wants the first argument to be the module name, the second the hook, and then whatever arguments the function takes. For example:

$block = module_invoke('user', 'block', 'view', 3);
print $block['content'];

Returns something like:
There are currently 1 user and 0 guests online.
Online users
* admin

It's asking the user module to display its block #3. Same results from: $block= user_block('view',3);

I pasted in the relevant core code for the module_invoke function below. (from module.inc, v4.7 RC1)

    For people unfamiliar with all the PHP used below:
  • func_get_args: crams whatever arguments were passed to the function into an array
  • array_shift: returns the first item of the array and removes it from the original array
  • call_user_func_array: calls the function, using an array to hold the arguments
function module_invoke() {
  $args = func_get_args();
  $module = array_shift($args);
  $hook = array_shift($args);
  $function = $module .'_'. $hook;
  if (module_hook($module, $hook)) {
    return call_user_func_array($function, $args);
  }
}

function module_hook($module, $hook) {
  return function_exists($module .'_'. $hook);
}
repe-1’s picture

That was a usefull clarification, thanks.

josoroma’s picture


    <?php if ($is_front) : ?>

    <?php if ($mission): print '<div id="mission">'. $mission .'</div>'; endif; ?>

    <div id="frontpage_top" class="frontpage">

	<?php
	
		/* ---------------------------------------------------------- */
	
		$module = 'event';
		$delta = 1;
	
		$block = (object) module_invoke($module, 'block', 'view', $delta);
		$block->module = $module;
		$block->delta = $delta;
	
		$tab_A = theme('block', $block);
	
		/* ---------------------------------------------------------- */
	
		$module = 'views';
		$delta = 'latest_videos';
	
		$block = (object) module_invoke($module, 'block', 'view', $delta);
		$block->module = $module;
		$block->delta = $delta;
	
		$tab_B = theme('block', $block);
	
		/* ---------------------------------------------------------- */
	
	?>
	
	<table width="100%"  border="0" cellspacing="6" cellpadding="6">
	  <tr align="left" valign="top">
		<td width="50%"><?php print $tab_A; ?></td>
		<td width="50%"><?php print $tab_B; ?></td>
	  </tr>
	  <tr align="left" valign="top">
		<td width="50%">Audio</td>
		<td width="50%">Images</td>
	  </tr>
	</table>