module.inc

  1. drupal
    1. 4.6 includes/module.inc
    2. 4.7 includes/module.inc
    3. 5 includes/module.inc
    4. 6 includes/module.inc
    5. 7 includes/module.inc
    6. 8 core/includes/module.inc

API for loading and interacting with Drupal modules.

Functions & methods

NameDescription
module_existDetermine whether a given module exists.
module_hookDetermine whether a module implements a hook.
module_implementsDetermine which modules are implementing a hook.
module_invokeInvoke a hook in a particular module.
module_invoke_allInvoke a hook in all enabled modules that implement it.
module_iterateCall a function repeatedly with each module in turn as an argument.
module_listCollect a list of all loaded modules. During the bootstrap, return only vital modules. See bootstrap.inc
module_load_allLoad all the modules that have been enabled in the system table.

File

includes/module.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. * API for loading and interacting with Drupal modules.
  5. */
  6. /**
  7. * Load all the modules that have been enabled in the system table.
  8. */
  9. function module_load_all() {
  10. foreach (module_list(TRUE, FALSE) as $module) {
  11. drupal_load('module', $module);
  12. }
  13. }
  14. /**
  15. * Call a function repeatedly with each module in turn as an argument.
  16. */
  17. function module_iterate($function, $argument = '') {
  18. foreach (module_list() as $name) {
  19. $function($name, $argument);
  20. }
  21. }
  22. /**
  23. * Collect a list of all loaded modules. During the bootstrap, return only
  24. * vital modules. See bootstrap.inc
  25. *
  26. * @param $refresh
  27. * Whether to force the module list to be regenerated (such as after the
  28. * administrator has changed the system settings).
  29. * @param $bootstrap
  30. * Whether to return the reduced set of modules loaded in "bootstrap mode"
  31. * for cached pages. See bootstrap.inc.
  32. * @param $sort
  33. * By default, modules are ordered by weight and filename, settings this option
  34. * to TRUE, module list will be ordered by module name.
  35. * @return
  36. * An associative array whose keys and values are the names of all loaded
  37. * modules.
  38. */
  39. function module_list($refresh = FALSE, $bootstrap = TRUE, $sort = FALSE) {
  40. static $list, $sorted_list;
  41. if ($refresh) {
  42. $list = array();
  43. $sorted_list = NULL;
  44. }
  45. if (!$list) {
  46. $list = array('filter' => 'filter', 'node' => 'node', 'system' => 'system', 'user' => 'user', 'watchdog' => 'watchdog');
  47. if ($bootstrap) {
  48. $result = db_query("SELECT name, filename, throttle, bootstrap FROM {system} WHERE type = 'module' AND status = 1 AND bootstrap = 1 ORDER BY weight ASC, filename ASC");
  49. }
  50. else {
  51. $result = db_query("SELECT name, filename, throttle, bootstrap FROM {system} WHERE type = 'module' AND status = 1 ORDER BY weight ASC, filename ASC");
  52. }
  53. while ($module = db_fetch_object($result)) {
  54. if (file_exists($module->filename)) {
  55. // Determine the current throttle status and see if the module should be
  56. // loaded based on server load. We have to directly access the throttle
  57. // variables, since throttle.module may not be loaded yet.
  58. $throttle = ($module->throttle && variable_get('throttle_level', 0) > 0);
  59. if (!$throttle) {
  60. drupal_get_filename('module', $module->name, $module->filename);
  61. $list[$module->name] = $module->name;
  62. }
  63. }
  64. }
  65. }
  66. if ($sort) {
  67. if (!isset($sorted_list)) {
  68. $sorted_list = $list;
  69. ksort($sorted_list);
  70. }
  71. return $sorted_list;
  72. }
  73. return $list;
  74. }
  75. /**
  76. * Determine whether a given module exists.
  77. *
  78. * @param $module
  79. * The name of the module (without the .module extension).
  80. * @return
  81. * TRUE if the module is both installed and enabled.
  82. */
  83. function module_exist($module) {
  84. $list = module_list();
  85. return array_key_exists($module, $list);
  86. }
  87. /**
  88. * @defgroup hooks Hooks
  89. * @{
  90. * Allow modules to interact with the Drupal core.
  91. *
  92. * Drupal's module system is based on the concept of "hooks". A hook is a PHP
  93. * function that is named foo_bar(), where "foo" is the name of the module (whose
  94. * filename is thus foo.module) and "bar" is the name of the hook. Each hook has
  95. * a defined set of parameters and a specified result type.
  96. *
  97. * To extend Drupal, a module need simply implement a hook. When Drupal wishes to
  98. * allow intervention from modules, it determines which modules implement a hook
  99. * and call that hook in all enabled modules that implement it.
  100. *
  101. * The available hooks to implement are explained here in the Hooks section of
  102. * the developer documentation. The string "hook" is used as a placeholder for
  103. * the module name is the hook definitions. For example, if the module file is
  104. * called example.module, then hook_help() as implemented by that module would be
  105. * defined as example_help().
  106. */
  107. /**
  108. * Determine whether a module implements a hook.
  109. *
  110. * @param $module
  111. * The name of the module (without the .module extension).
  112. * @param $hook
  113. * The name of the hook (e.g. "help" or "menu").
  114. * @return
  115. * TRUE if the module is both installed and enabled, and the hook is
  116. * implemented in that module.
  117. */
  118. function module_hook($module, $hook) {
  119. return function_exists($module .'_'. $hook);
  120. }
  121. /**
  122. * Determine which modules are implementing a hook.
  123. *
  124. * @param $hook
  125. * The name of the hook (e.g. "help" or "menu").
  126. * @param $sort
  127. * By default, modules are ordered by weight and filename, settings this option
  128. * to TRUE, module list will be ordered by module name.
  129. * @return
  130. * An array with the names of the modules which are implementing this hook.
  131. */
  132. function module_implements($hook, $sort = FALSE) {
  133. static $implementations;
  134. if (!isset($implementations[$hook])) {
  135. $implementations[$hook] = array();
  136. $list = module_list(FALSE, TRUE, $sort);
  137. foreach ($list as $module) {
  138. if (module_hook($module, $hook)) {
  139. $implementations[$hook][] = $module;
  140. }
  141. }
  142. }
  143. // The explicit cast forces a copy to be made. This is needed because
  144. // $implementations[$hook] is only a reference to an element of
  145. // $implementations and if there are nested foreaches (due to nested node
  146. // API calls, for example), they would both manipulate the same array's
  147. // references, which causes some modules' hooks not to be called.
  148. // See also http://www.zend.com/zend/art/ref-count.php.
  149. return (array)$implementations[$hook];
  150. }
  151. /**
  152. * Invoke a hook in a particular module.
  153. *
  154. * @param $module
  155. * The name of the module (without the .module extension).
  156. * @param $hook
  157. * The name of the hook to invoke.
  158. * @param ...
  159. * Arguments to pass to the hook implementation.
  160. * @return
  161. * The return value of the hook implementation.
  162. */
  163. function module_invoke() {
  164. $args = func_get_args();
  165. $module = array_shift($args);
  166. $hook = array_shift($args);
  167. $function = $module .'_'. $hook;
  168. if (module_hook($module, $hook)) {
  169. return call_user_func_array($function, $args);
  170. }
  171. }
  172. /**
  173. * Invoke a hook in all enabled modules that implement it.
  174. *
  175. * @param $hook
  176. * The name of the hook to invoke.
  177. * @param ...
  178. * Arguments to pass to the hook.
  179. * @return
  180. * An array of return values of the hook implementations. If modules return
  181. * arrays from their implementations, those are merged into one array.
  182. */
  183. function module_invoke_all() {
  184. $args = func_get_args();
  185. $hook = array_shift($args);
  186. $return = array();
  187. foreach (module_implements($hook) as $module) {
  188. $function = $module .'_'. $hook;
  189. $result = call_user_func_array($function, $args);
  190. if (isset($result) && is_array($result)) {
  191. $return = array_merge($return, $result);
  192. }
  193. else if (isset($result)) {
  194. $return[] = $result;
  195. }
  196. }
  197. return $return;
  198. }
  199. /**
  200. * @} End of "defgroup hooks".
  201. */
Login or register to post comments