danielebarchiesi@0: $module) { danielebarchiesi@0: drupal_get_filename('module', $name, $module['filename']); danielebarchiesi@0: $list[$name] = $name; danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: else { danielebarchiesi@0: if ($refresh) { danielebarchiesi@0: // For the $refresh case, make sure that system_list() returns fresh danielebarchiesi@0: // data. danielebarchiesi@0: drupal_static_reset('system_list'); danielebarchiesi@0: } danielebarchiesi@0: if ($bootstrap_refresh) { danielebarchiesi@0: $list = system_list('bootstrap'); danielebarchiesi@0: } danielebarchiesi@0: else { danielebarchiesi@0: // Not using drupal_map_assoc() here as that requires common.inc. danielebarchiesi@0: $list = array_keys(system_list('module_enabled')); danielebarchiesi@0: $list = (!empty($list) ? array_combine($list, $list) : array()); danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: if ($sort) { danielebarchiesi@0: if (!isset($sorted_list)) { danielebarchiesi@0: $sorted_list = $list; danielebarchiesi@0: ksort($sorted_list); danielebarchiesi@0: } danielebarchiesi@0: return $sorted_list; danielebarchiesi@0: } danielebarchiesi@0: return $list; danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Builds a list of bootstrap modules and enabled modules and themes. danielebarchiesi@0: * danielebarchiesi@0: * @param $type danielebarchiesi@0: * The type of list to return: danielebarchiesi@0: * - module_enabled: All enabled modules. danielebarchiesi@0: * - bootstrap: All enabled modules required for bootstrap. danielebarchiesi@0: * - theme: All themes. danielebarchiesi@0: * danielebarchiesi@0: * @return danielebarchiesi@0: * An associative array of modules or themes, keyed by name. For $type danielebarchiesi@0: * 'bootstrap', the array values equal the keys. For $type 'module_enabled' danielebarchiesi@0: * or 'theme', the array values are objects representing the respective danielebarchiesi@0: * database row, with the 'info' property already unserialized. danielebarchiesi@0: * danielebarchiesi@0: * @see module_list() danielebarchiesi@0: * @see list_themes() danielebarchiesi@0: */ danielebarchiesi@0: function system_list($type) { danielebarchiesi@0: $lists = &drupal_static(__FUNCTION__); danielebarchiesi@0: danielebarchiesi@0: // For bootstrap modules, attempt to fetch the list from cache if possible. danielebarchiesi@0: // if not fetch only the required information to fire bootstrap hooks danielebarchiesi@0: // in case we are going to serve the page from cache. danielebarchiesi@0: if ($type == 'bootstrap') { danielebarchiesi@0: if (isset($lists['bootstrap'])) { danielebarchiesi@0: return $lists['bootstrap']; danielebarchiesi@0: } danielebarchiesi@0: if ($cached = cache_get('bootstrap_modules', 'cache_bootstrap')) { danielebarchiesi@0: $bootstrap_list = $cached->data; danielebarchiesi@0: } danielebarchiesi@0: else { danielebarchiesi@0: $bootstrap_list = db_query("SELECT name, filename FROM {system} WHERE status = 1 AND bootstrap = 1 AND type = 'module' ORDER BY weight ASC, name ASC")->fetchAllAssoc('name'); danielebarchiesi@0: cache_set('bootstrap_modules', $bootstrap_list, 'cache_bootstrap'); danielebarchiesi@0: } danielebarchiesi@0: // To avoid a separate database lookup for the filepath, prime the danielebarchiesi@0: // drupal_get_filename() static cache for bootstrap modules only. danielebarchiesi@0: // The rest is stored separately to keep the bootstrap module cache small. danielebarchiesi@0: foreach ($bootstrap_list as $module) { danielebarchiesi@0: drupal_get_filename('module', $module->name, $module->filename); danielebarchiesi@0: } danielebarchiesi@0: // We only return the module names here since module_list() doesn't need danielebarchiesi@0: // the filename itself. danielebarchiesi@0: $lists['bootstrap'] = array_keys($bootstrap_list); danielebarchiesi@0: } danielebarchiesi@0: // Otherwise build the list for enabled modules and themes. danielebarchiesi@0: elseif (!isset($lists['module_enabled'])) { danielebarchiesi@0: if ($cached = cache_get('system_list', 'cache_bootstrap')) { danielebarchiesi@0: $lists = $cached->data; danielebarchiesi@0: } danielebarchiesi@0: else { danielebarchiesi@0: $lists = array( danielebarchiesi@0: 'module_enabled' => array(), danielebarchiesi@0: 'theme' => array(), danielebarchiesi@0: 'filepaths' => array(), danielebarchiesi@0: ); danielebarchiesi@0: // The module name (rather than the filename) is used as the fallback danielebarchiesi@0: // weighting in order to guarantee consistent behavior across different danielebarchiesi@0: // Drupal installations, which might have modules installed in different danielebarchiesi@0: // locations in the file system. The ordering here must also be danielebarchiesi@0: // consistent with the one used in module_implements(). danielebarchiesi@0: $result = db_query("SELECT * FROM {system} WHERE type = 'theme' OR (type = 'module' AND status = 1) ORDER BY weight ASC, name ASC"); danielebarchiesi@0: foreach ($result as $record) { danielebarchiesi@0: $record->info = unserialize($record->info); danielebarchiesi@0: // Build a list of all enabled modules. danielebarchiesi@0: if ($record->type == 'module') { danielebarchiesi@0: $lists['module_enabled'][$record->name] = $record; danielebarchiesi@0: } danielebarchiesi@0: // Build a list of themes. danielebarchiesi@0: if ($record->type == 'theme') { danielebarchiesi@0: $lists['theme'][$record->name] = $record; danielebarchiesi@0: } danielebarchiesi@0: // Build a list of filenames so drupal_get_filename can use it. danielebarchiesi@0: if ($record->status) { danielebarchiesi@0: $lists['filepaths'][] = array('type' => $record->type, 'name' => $record->name, 'filepath' => $record->filename); danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: foreach ($lists['theme'] as $key => $theme) { danielebarchiesi@0: if (!empty($theme->info['base theme'])) { danielebarchiesi@0: // Make a list of the theme's base themes. danielebarchiesi@0: require_once DRUPAL_ROOT . '/includes/theme.inc'; danielebarchiesi@0: $lists['theme'][$key]->base_themes = drupal_find_base_themes($lists['theme'], $key); danielebarchiesi@0: // Don't proceed if there was a problem with the root base theme. danielebarchiesi@0: if (!current($lists['theme'][$key]->base_themes)) { danielebarchiesi@0: continue; danielebarchiesi@0: } danielebarchiesi@0: // Determine the root base theme. danielebarchiesi@0: $base_key = key($lists['theme'][$key]->base_themes); danielebarchiesi@0: // Add to the list of sub-themes for each of the theme's base themes. danielebarchiesi@0: foreach (array_keys($lists['theme'][$key]->base_themes) as $base_theme) { danielebarchiesi@0: $lists['theme'][$base_theme]->sub_themes[$key] = $lists['theme'][$key]->info['name']; danielebarchiesi@0: } danielebarchiesi@0: // Add the base theme's theme engine info. danielebarchiesi@0: $lists['theme'][$key]->info['engine'] = isset($lists['theme'][$base_key]->info['engine']) ? $lists['theme'][$base_key]->info['engine'] : 'theme'; danielebarchiesi@0: } danielebarchiesi@0: else { danielebarchiesi@0: // A plain theme is its own engine. danielebarchiesi@0: $base_key = $key; danielebarchiesi@0: if (!isset($lists['theme'][$key]->info['engine'])) { danielebarchiesi@0: $lists['theme'][$key]->info['engine'] = 'theme'; danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: // Set the theme engine prefix. danielebarchiesi@0: $lists['theme'][$key]->prefix = ($lists['theme'][$key]->info['engine'] == 'theme') ? $base_key : $lists['theme'][$key]->info['engine']; danielebarchiesi@0: } danielebarchiesi@0: cache_set('system_list', $lists, 'cache_bootstrap'); danielebarchiesi@0: } danielebarchiesi@0: // To avoid a separate database lookup for the filepath, prime the danielebarchiesi@0: // drupal_get_filename() static cache with all enabled modules and themes. danielebarchiesi@0: foreach ($lists['filepaths'] as $item) { danielebarchiesi@0: drupal_get_filename($item['type'], $item['name'], $item['filepath']); danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: return $lists[$type]; danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Resets all system_list() caches. danielebarchiesi@0: */ danielebarchiesi@0: function system_list_reset() { danielebarchiesi@0: drupal_static_reset('system_list'); danielebarchiesi@0: drupal_static_reset('system_rebuild_module_data'); danielebarchiesi@0: drupal_static_reset('list_themes'); danielebarchiesi@0: cache_clear_all('bootstrap_modules', 'cache_bootstrap'); danielebarchiesi@0: cache_clear_all('system_list', 'cache_bootstrap'); danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Determines which modules require and are required by each module. danielebarchiesi@0: * danielebarchiesi@0: * @param $files danielebarchiesi@0: * The array of filesystem objects used to rebuild the cache. danielebarchiesi@0: * danielebarchiesi@0: * @return danielebarchiesi@0: * The same array with the new keys for each module: danielebarchiesi@0: * - requires: An array with the keys being the modules that this module danielebarchiesi@0: * requires. danielebarchiesi@0: * - required_by: An array with the keys being the modules that will not work danielebarchiesi@0: * without this module. danielebarchiesi@0: */ danielebarchiesi@0: function _module_build_dependencies($files) { danielebarchiesi@0: require_once DRUPAL_ROOT . '/includes/graph.inc'; danielebarchiesi@0: foreach ($files as $filename => $file) { danielebarchiesi@0: $graph[$file->name]['edges'] = array(); danielebarchiesi@0: if (isset($file->info['dependencies']) && is_array($file->info['dependencies'])) { danielebarchiesi@0: foreach ($file->info['dependencies'] as $dependency) { danielebarchiesi@0: $dependency_data = drupal_parse_dependency($dependency); danielebarchiesi@0: $graph[$file->name]['edges'][$dependency_data['name']] = $dependency_data; danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: drupal_depth_first_search($graph); danielebarchiesi@0: foreach ($graph as $module => $data) { danielebarchiesi@0: $files[$module]->required_by = isset($data['reverse_paths']) ? $data['reverse_paths'] : array(); danielebarchiesi@0: $files[$module]->requires = isset($data['paths']) ? $data['paths'] : array(); danielebarchiesi@0: $files[$module]->sort = $data['weight']; danielebarchiesi@0: } danielebarchiesi@0: return $files; danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Determines whether a given module exists. danielebarchiesi@0: * danielebarchiesi@0: * @param $module danielebarchiesi@0: * The name of the module (without the .module extension). danielebarchiesi@0: * danielebarchiesi@0: * @return danielebarchiesi@0: * TRUE if the module is both installed and enabled. danielebarchiesi@0: */ danielebarchiesi@0: function module_exists($module) { danielebarchiesi@0: $list = module_list(); danielebarchiesi@0: return isset($list[$module]); danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Loads a module's installation hooks. danielebarchiesi@0: * danielebarchiesi@0: * @param $module danielebarchiesi@0: * The name of the module (without the .module extension). danielebarchiesi@0: * danielebarchiesi@0: * @return danielebarchiesi@0: * The name of the module's install file, if successful; FALSE otherwise. danielebarchiesi@0: */ danielebarchiesi@0: function module_load_install($module) { danielebarchiesi@0: // Make sure the installation API is available danielebarchiesi@0: include_once DRUPAL_ROOT . '/includes/install.inc'; danielebarchiesi@0: danielebarchiesi@0: return module_load_include('install', $module); danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Loads a module include file. danielebarchiesi@0: * danielebarchiesi@0: * Examples: danielebarchiesi@0: * @code danielebarchiesi@0: * // Load node.admin.inc from the node module. danielebarchiesi@0: * module_load_include('inc', 'node', 'node.admin'); danielebarchiesi@0: * // Load content_types.inc from the node module. danielebarchiesi@0: * module_load_include('inc', 'node', 'content_types'); danielebarchiesi@0: * @endcode danielebarchiesi@0: * danielebarchiesi@0: * Do not use this function to load an install file, use module_load_install() danielebarchiesi@0: * instead. Do not use this function in a global context since it requires danielebarchiesi@0: * Drupal to be fully bootstrapped, use require_once DRUPAL_ROOT . '/path/file' danielebarchiesi@0: * instead. danielebarchiesi@0: * danielebarchiesi@0: * @param $type danielebarchiesi@0: * The include file's type (file extension). danielebarchiesi@0: * @param $module danielebarchiesi@0: * The module to which the include file belongs. danielebarchiesi@0: * @param $name danielebarchiesi@0: * (optional) The base file name (without the $type extension). If omitted, danielebarchiesi@0: * $module is used; i.e., resulting in "$module.$type" by default. danielebarchiesi@0: * danielebarchiesi@0: * @return danielebarchiesi@0: * The name of the included file, if successful; FALSE otherwise. danielebarchiesi@0: */ danielebarchiesi@0: function module_load_include($type, $module, $name = NULL) { danielebarchiesi@0: if (!isset($name)) { danielebarchiesi@0: $name = $module; danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: if (function_exists('drupal_get_path')) { danielebarchiesi@0: $file = DRUPAL_ROOT . '/' . drupal_get_path('module', $module) . "/$name.$type"; danielebarchiesi@0: if (is_file($file)) { danielebarchiesi@0: require_once $file; danielebarchiesi@0: return $file; danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: return FALSE; danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Loads an include file for each module enabled in the {system} table. danielebarchiesi@0: */ danielebarchiesi@0: function module_load_all_includes($type, $name = NULL) { danielebarchiesi@0: $modules = module_list(); danielebarchiesi@0: foreach ($modules as $module) { danielebarchiesi@0: module_load_include($type, $module, $name); danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Enables or installs a given list of modules. danielebarchiesi@0: * danielebarchiesi@0: * Definitions: danielebarchiesi@0: * - "Enabling" is the process of activating a module for use by Drupal. danielebarchiesi@0: * - "Disabling" is the process of deactivating a module. danielebarchiesi@0: * - "Installing" is the process of enabling it for the first time or after it danielebarchiesi@0: * has been uninstalled. danielebarchiesi@0: * - "Uninstalling" is the process of removing all traces of a module. danielebarchiesi@0: * danielebarchiesi@0: * Order of events: danielebarchiesi@0: * - Gather and add module dependencies to $module_list (if applicable). danielebarchiesi@0: * - For each module that is being enabled: danielebarchiesi@0: * - Install module schema and update system registries and caches. danielebarchiesi@0: * - If the module is being enabled for the first time or had been danielebarchiesi@0: * uninstalled, invoke hook_install() and add it to the list of installed danielebarchiesi@0: * modules. danielebarchiesi@0: * - Invoke hook_enable(). danielebarchiesi@0: * - Invoke hook_modules_installed(). danielebarchiesi@0: * - Invoke hook_modules_enabled(). danielebarchiesi@0: * danielebarchiesi@0: * @param $module_list danielebarchiesi@0: * An array of module names. danielebarchiesi@0: * @param $enable_dependencies danielebarchiesi@0: * If TRUE, dependencies will automatically be added and enabled in the danielebarchiesi@0: * correct order. This incurs a significant performance cost, so use FALSE danielebarchiesi@0: * if you know $module_list is already complete and in the correct order. danielebarchiesi@0: * danielebarchiesi@0: * @return danielebarchiesi@0: * FALSE if one or more dependencies are missing, TRUE otherwise. danielebarchiesi@0: * danielebarchiesi@0: * @see hook_install() danielebarchiesi@0: * @see hook_enable() danielebarchiesi@0: * @see hook_modules_installed() danielebarchiesi@0: * @see hook_modules_enabled() danielebarchiesi@0: */ danielebarchiesi@0: function module_enable($module_list, $enable_dependencies = TRUE) { danielebarchiesi@0: if ($enable_dependencies) { danielebarchiesi@0: // Get all module data so we can find dependencies and sort. danielebarchiesi@0: $module_data = system_rebuild_module_data(); danielebarchiesi@0: // Create an associative array with weights as values. danielebarchiesi@0: $module_list = array_flip(array_values($module_list)); danielebarchiesi@0: danielebarchiesi@0: while (list($module) = each($module_list)) { danielebarchiesi@0: if (!isset($module_data[$module])) { danielebarchiesi@0: // This module is not found in the filesystem, abort. danielebarchiesi@0: return FALSE; danielebarchiesi@0: } danielebarchiesi@0: if ($module_data[$module]->status) { danielebarchiesi@0: // Skip already enabled modules. danielebarchiesi@0: unset($module_list[$module]); danielebarchiesi@0: continue; danielebarchiesi@0: } danielebarchiesi@0: $module_list[$module] = $module_data[$module]->sort; danielebarchiesi@0: danielebarchiesi@0: // Add dependencies to the list, with a placeholder weight. danielebarchiesi@0: // The new modules will be processed as the while loop continues. danielebarchiesi@0: foreach (array_keys($module_data[$module]->requires) as $dependency) { danielebarchiesi@0: if (!isset($module_list[$dependency])) { danielebarchiesi@0: $module_list[$dependency] = 0; danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: if (!$module_list) { danielebarchiesi@0: // Nothing to do. All modules already enabled. danielebarchiesi@0: return TRUE; danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: // Sort the module list by pre-calculated weights. danielebarchiesi@0: arsort($module_list); danielebarchiesi@0: $module_list = array_keys($module_list); danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: // Required for module installation checks. danielebarchiesi@0: include_once DRUPAL_ROOT . '/includes/install.inc'; danielebarchiesi@0: danielebarchiesi@0: $modules_installed = array(); danielebarchiesi@0: $modules_enabled = array(); danielebarchiesi@0: foreach ($module_list as $module) { danielebarchiesi@0: // Only process modules that are not already enabled. danielebarchiesi@0: $existing = db_query("SELECT status FROM {system} WHERE type = :type AND name = :name", array( danielebarchiesi@0: ':type' => 'module', danielebarchiesi@0: ':name' => $module)) danielebarchiesi@0: ->fetchObject(); danielebarchiesi@0: if ($existing->status == 0) { danielebarchiesi@0: // Load the module's code. danielebarchiesi@0: drupal_load('module', $module); danielebarchiesi@0: module_load_install($module); danielebarchiesi@0: danielebarchiesi@0: // Update the database and module list to reflect the new module. This danielebarchiesi@0: // needs to be done first so that the module's hook implementations, danielebarchiesi@0: // hook_schema() in particular, can be called while it is being danielebarchiesi@0: // installed. danielebarchiesi@0: db_update('system') danielebarchiesi@0: ->fields(array('status' => 1)) danielebarchiesi@0: ->condition('type', 'module') danielebarchiesi@0: ->condition('name', $module) danielebarchiesi@0: ->execute(); danielebarchiesi@0: // Refresh the module list to include it. danielebarchiesi@0: system_list_reset(); danielebarchiesi@0: module_list(TRUE); danielebarchiesi@0: module_implements('', FALSE, TRUE); danielebarchiesi@0: _system_update_bootstrap_status(); danielebarchiesi@0: // Update the registry to include it. danielebarchiesi@0: registry_update(); danielebarchiesi@0: // Refresh the schema to include it. danielebarchiesi@0: drupal_get_schema(NULL, TRUE); danielebarchiesi@0: // Update the theme registry to include it. danielebarchiesi@0: drupal_theme_rebuild(); danielebarchiesi@0: // Clear entity cache. danielebarchiesi@0: entity_info_cache_clear(); danielebarchiesi@0: danielebarchiesi@0: // Now install the module if necessary. danielebarchiesi@0: if (drupal_get_installed_schema_version($module, TRUE) == SCHEMA_UNINSTALLED) { danielebarchiesi@0: drupal_install_schema($module); danielebarchiesi@0: danielebarchiesi@0: // Set the schema version to the number of the last update provided danielebarchiesi@0: // by the module. danielebarchiesi@0: $versions = drupal_get_schema_versions($module); danielebarchiesi@0: $version = $versions ? max($versions) : SCHEMA_INSTALLED; danielebarchiesi@0: danielebarchiesi@0: // If the module has no current updates, but has some that were danielebarchiesi@0: // previously removed, set the version to the value of danielebarchiesi@0: // hook_update_last_removed(). danielebarchiesi@0: if ($last_removed = module_invoke($module, 'update_last_removed')) { danielebarchiesi@0: $version = max($version, $last_removed); danielebarchiesi@0: } danielebarchiesi@0: drupal_set_installed_schema_version($module, $version); danielebarchiesi@0: // Allow the module to perform install tasks. danielebarchiesi@0: module_invoke($module, 'install'); danielebarchiesi@0: // Record the fact that it was installed. danielebarchiesi@0: $modules_installed[] = $module; danielebarchiesi@0: watchdog('system', '%module module installed.', array('%module' => $module), WATCHDOG_INFO); danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: // Enable the module. danielebarchiesi@0: module_invoke($module, 'enable'); danielebarchiesi@0: danielebarchiesi@0: // Record the fact that it was enabled. danielebarchiesi@0: $modules_enabled[] = $module; danielebarchiesi@0: watchdog('system', '%module module enabled.', array('%module' => $module), WATCHDOG_INFO); danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: // If any modules were newly installed, invoke hook_modules_installed(). danielebarchiesi@0: if (!empty($modules_installed)) { danielebarchiesi@0: module_invoke_all('modules_installed', $modules_installed); danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: // If any modules were newly enabled, invoke hook_modules_enabled(). danielebarchiesi@0: if (!empty($modules_enabled)) { danielebarchiesi@0: module_invoke_all('modules_enabled', $modules_enabled); danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: return TRUE; danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Disables a given set of modules. danielebarchiesi@0: * danielebarchiesi@0: * @param $module_list danielebarchiesi@0: * An array of module names. danielebarchiesi@0: * @param $disable_dependents danielebarchiesi@0: * If TRUE, dependent modules will automatically be added and disabled in the danielebarchiesi@0: * correct order. This incurs a significant performance cost, so use FALSE danielebarchiesi@0: * if you know $module_list is already complete and in the correct order. danielebarchiesi@0: */ danielebarchiesi@0: function module_disable($module_list, $disable_dependents = TRUE) { danielebarchiesi@0: if ($disable_dependents) { danielebarchiesi@0: // Get all module data so we can find dependents and sort. danielebarchiesi@0: $module_data = system_rebuild_module_data(); danielebarchiesi@0: // Create an associative array with weights as values. danielebarchiesi@0: $module_list = array_flip(array_values($module_list)); danielebarchiesi@0: danielebarchiesi@0: $profile = drupal_get_profile(); danielebarchiesi@0: while (list($module) = each($module_list)) { danielebarchiesi@0: if (!isset($module_data[$module]) || !$module_data[$module]->status) { danielebarchiesi@0: // This module doesn't exist or is already disabled, skip it. danielebarchiesi@0: unset($module_list[$module]); danielebarchiesi@0: continue; danielebarchiesi@0: } danielebarchiesi@0: $module_list[$module] = $module_data[$module]->sort; danielebarchiesi@0: danielebarchiesi@0: // Add dependent modules to the list, with a placeholder weight. danielebarchiesi@0: // The new modules will be processed as the while loop continues. danielebarchiesi@0: foreach ($module_data[$module]->required_by as $dependent => $dependent_data) { danielebarchiesi@0: if (!isset($module_list[$dependent]) && $dependent != $profile) { danielebarchiesi@0: $module_list[$dependent] = 0; danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: // Sort the module list by pre-calculated weights. danielebarchiesi@0: asort($module_list); danielebarchiesi@0: $module_list = array_keys($module_list); danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: $invoke_modules = array(); danielebarchiesi@0: danielebarchiesi@0: foreach ($module_list as $module) { danielebarchiesi@0: if (module_exists($module)) { danielebarchiesi@0: // Check if node_access table needs rebuilding. danielebarchiesi@0: if (!node_access_needs_rebuild() && module_hook($module, 'node_grants')) { danielebarchiesi@0: node_access_needs_rebuild(TRUE); danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: module_load_install($module); danielebarchiesi@0: module_invoke($module, 'disable'); danielebarchiesi@0: db_update('system') danielebarchiesi@0: ->fields(array('status' => 0)) danielebarchiesi@0: ->condition('type', 'module') danielebarchiesi@0: ->condition('name', $module) danielebarchiesi@0: ->execute(); danielebarchiesi@0: $invoke_modules[] = $module; danielebarchiesi@0: watchdog('system', '%module module disabled.', array('%module' => $module), WATCHDOG_INFO); danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: if (!empty($invoke_modules)) { danielebarchiesi@0: // Refresh the module list to exclude the disabled modules. danielebarchiesi@0: system_list_reset(); danielebarchiesi@0: module_list(TRUE); danielebarchiesi@0: module_implements('', FALSE, TRUE); danielebarchiesi@0: entity_info_cache_clear(); danielebarchiesi@0: // Invoke hook_modules_disabled before disabling modules, danielebarchiesi@0: // so we can still call module hooks to get information. danielebarchiesi@0: module_invoke_all('modules_disabled', $invoke_modules); danielebarchiesi@0: // Update the registry to remove the newly-disabled module. danielebarchiesi@0: registry_update(); danielebarchiesi@0: _system_update_bootstrap_status(); danielebarchiesi@0: // Update the theme registry to remove the newly-disabled module. danielebarchiesi@0: drupal_theme_rebuild(); danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: // If there remains no more node_access module, rebuilding will be danielebarchiesi@0: // straightforward, we can do it right now. danielebarchiesi@0: if (node_access_needs_rebuild() && count(module_implements('node_grants')) == 0) { danielebarchiesi@0: node_access_rebuild(); danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * @defgroup hooks Hooks danielebarchiesi@0: * @{ danielebarchiesi@0: * Allow modules to interact with the Drupal core. danielebarchiesi@0: * danielebarchiesi@0: * Drupal's module system is based on the concept of "hooks". A hook is a PHP danielebarchiesi@0: * function that is named foo_bar(), where "foo" is the name of the module danielebarchiesi@0: * (whose filename is thus foo.module) and "bar" is the name of the hook. Each danielebarchiesi@0: * hook has a defined set of parameters and a specified result type. danielebarchiesi@0: * danielebarchiesi@0: * To extend Drupal, a module need simply implement a hook. When Drupal wishes danielebarchiesi@0: * to allow intervention from modules, it determines which modules implement a danielebarchiesi@0: * hook and calls that hook in all enabled modules that implement it. danielebarchiesi@0: * danielebarchiesi@0: * The available hooks to implement are explained here in the Hooks section of danielebarchiesi@0: * the developer documentation. The string "hook" is used as a placeholder for danielebarchiesi@0: * the module name in the hook definitions. For example, if the module file is danielebarchiesi@0: * called example.module, then hook_help() as implemented by that module would danielebarchiesi@0: * be defined as example_help(). danielebarchiesi@0: * danielebarchiesi@0: * The example functions included are not part of the Drupal core, they are danielebarchiesi@0: * just models that you can modify. Only the hooks implemented within modules danielebarchiesi@0: * are executed when running Drupal. danielebarchiesi@0: * danielebarchiesi@0: * @see themeable danielebarchiesi@0: * @see callbacks danielebarchiesi@0: */ danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * @defgroup callbacks Callbacks danielebarchiesi@0: * @{ danielebarchiesi@0: * Callback function signatures. danielebarchiesi@0: * danielebarchiesi@0: * Drupal's API sometimes uses callback functions to allow you to define how danielebarchiesi@0: * some type of processing happens. A callback is a function with a defined danielebarchiesi@0: * signature, which you define in a module. Then you pass the function name as danielebarchiesi@0: * a parameter to a Drupal API function or return it as part of a hook danielebarchiesi@0: * implementation return value, and your function is called at an appropriate danielebarchiesi@0: * time. For instance, when setting up batch processing you might need to danielebarchiesi@0: * provide a callback function for each processing step and/or a callback for danielebarchiesi@0: * when processing is finished; you would do that by defining these functions danielebarchiesi@0: * and passing their names into the batch setup function. danielebarchiesi@0: * danielebarchiesi@0: * Callback function signatures, like hook definitions, are described by danielebarchiesi@0: * creating and documenting dummy functions in a *.api.php file; normally, the danielebarchiesi@0: * dummy callback function's name should start with "callback_", and you should danielebarchiesi@0: * document the parameters and return value and provide a sample function body. danielebarchiesi@0: * Then your API documentation can refer to this callback function in its danielebarchiesi@0: * documentation. A user of your API can usually name their callback function danielebarchiesi@0: * anything they want, although a standard name would be to replace "callback_" danielebarchiesi@0: * with the module name. danielebarchiesi@0: * danielebarchiesi@0: * @see hooks danielebarchiesi@0: * @see themeable danielebarchiesi@0: * danielebarchiesi@0: * @} danielebarchiesi@0: */ danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Determines whether a module implements a hook. danielebarchiesi@0: * danielebarchiesi@0: * @param $module danielebarchiesi@0: * The name of the module (without the .module extension). danielebarchiesi@0: * @param $hook danielebarchiesi@0: * The name of the hook (e.g. "help" or "menu"). danielebarchiesi@0: * danielebarchiesi@0: * @return danielebarchiesi@0: * TRUE if the module is both installed and enabled, and the hook is danielebarchiesi@0: * implemented in that module. danielebarchiesi@0: */ danielebarchiesi@0: function module_hook($module, $hook) { danielebarchiesi@0: $function = $module . '_' . $hook; danielebarchiesi@0: if (function_exists($function)) { danielebarchiesi@0: return TRUE; danielebarchiesi@0: } danielebarchiesi@0: // If the hook implementation does not exist, check whether it may live in an danielebarchiesi@0: // optional include file registered via hook_hook_info(). danielebarchiesi@0: $hook_info = module_hook_info(); danielebarchiesi@0: if (isset($hook_info[$hook]['group'])) { danielebarchiesi@0: module_load_include('inc', $module, $module . '.' . $hook_info[$hook]['group']); danielebarchiesi@0: if (function_exists($function)) { danielebarchiesi@0: return TRUE; danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: return FALSE; danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Determines which modules are implementing a hook. danielebarchiesi@0: * danielebarchiesi@0: * @param $hook danielebarchiesi@0: * The name of the hook (e.g. "help" or "menu"). danielebarchiesi@0: * @param $sort danielebarchiesi@0: * By default, modules are ordered by weight and filename, settings this option danielebarchiesi@0: * to TRUE, module list will be ordered by module name. danielebarchiesi@0: * @param $reset danielebarchiesi@0: * For internal use only: Whether to force the stored list of hook danielebarchiesi@0: * implementations to be regenerated (such as after enabling a new module, danielebarchiesi@0: * before processing hook_enable). danielebarchiesi@0: * danielebarchiesi@0: * @return danielebarchiesi@0: * An array with the names of the modules which are implementing this hook. danielebarchiesi@0: * danielebarchiesi@0: * @see module_implements_write_cache() danielebarchiesi@0: */ danielebarchiesi@0: function module_implements($hook, $sort = FALSE, $reset = FALSE) { danielebarchiesi@0: // Use the advanced drupal_static() pattern, since this is called very often. danielebarchiesi@0: static $drupal_static_fast; danielebarchiesi@0: if (!isset($drupal_static_fast)) { danielebarchiesi@0: $drupal_static_fast['implementations'] = &drupal_static(__FUNCTION__); danielebarchiesi@0: } danielebarchiesi@0: $implementations = &$drupal_static_fast['implementations']; danielebarchiesi@0: danielebarchiesi@0: // We maintain a persistent cache of hook implementations in addition to the danielebarchiesi@0: // static cache to avoid looping through every module and every hook on each danielebarchiesi@0: // request. Benchmarks show that the benefit of this caching outweighs the danielebarchiesi@0: // additional database hit even when using the default database caching danielebarchiesi@0: // backend and only a small number of modules are enabled. The cost of the danielebarchiesi@0: // cache_get() is more or less constant and reduced further when non-database danielebarchiesi@0: // caching backends are used, so there will be more significant gains when a danielebarchiesi@0: // large number of modules are installed or hooks invoked, since this can danielebarchiesi@0: // quickly lead to module_hook() being called several thousand times danielebarchiesi@0: // per request. danielebarchiesi@0: if ($reset) { danielebarchiesi@0: $implementations = array(); danielebarchiesi@0: cache_set('module_implements', array(), 'cache_bootstrap'); danielebarchiesi@0: drupal_static_reset('module_hook_info'); danielebarchiesi@0: drupal_static_reset('drupal_alter'); danielebarchiesi@0: cache_clear_all('hook_info', 'cache_bootstrap'); danielebarchiesi@0: return; danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: // Fetch implementations from cache. danielebarchiesi@0: if (empty($implementations)) { danielebarchiesi@0: $implementations = cache_get('module_implements', 'cache_bootstrap'); danielebarchiesi@0: if ($implementations === FALSE) { danielebarchiesi@0: $implementations = array(); danielebarchiesi@0: } danielebarchiesi@0: else { danielebarchiesi@0: $implementations = $implementations->data; danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: if (!isset($implementations[$hook])) { danielebarchiesi@0: // The hook is not cached, so ensure that whether or not it has danielebarchiesi@0: // implementations, that the cache is updated at the end of the request. danielebarchiesi@0: $implementations['#write_cache'] = TRUE; danielebarchiesi@0: $hook_info = module_hook_info(); danielebarchiesi@0: $implementations[$hook] = array(); danielebarchiesi@0: $list = module_list(FALSE, FALSE, $sort); danielebarchiesi@0: foreach ($list as $module) { danielebarchiesi@0: $include_file = isset($hook_info[$hook]['group']) && module_load_include('inc', $module, $module . '.' . $hook_info[$hook]['group']); danielebarchiesi@0: // Since module_hook() may needlessly try to load the include file again, danielebarchiesi@0: // function_exists() is used directly here. danielebarchiesi@0: if (function_exists($module . '_' . $hook)) { danielebarchiesi@0: $implementations[$hook][$module] = $include_file ? $hook_info[$hook]['group'] : FALSE; danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: // Allow modules to change the weight of specific implementations but avoid danielebarchiesi@0: // an infinite loop. danielebarchiesi@0: if ($hook != 'module_implements_alter') { danielebarchiesi@0: drupal_alter('module_implements', $implementations[$hook], $hook); danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: else { danielebarchiesi@0: foreach ($implementations[$hook] as $module => $group) { danielebarchiesi@0: // If this hook implementation is stored in a lazy-loaded file, so include danielebarchiesi@0: // that file first. danielebarchiesi@0: if ($group) { danielebarchiesi@0: module_load_include('inc', $module, "$module.$group"); danielebarchiesi@0: } danielebarchiesi@0: // It is possible that a module removed a hook implementation without the danielebarchiesi@0: // implementations cache being rebuilt yet, so we check whether the danielebarchiesi@0: // function exists on each request to avoid undefined function errors. danielebarchiesi@0: // Since module_hook() may needlessly try to load the include file again, danielebarchiesi@0: // function_exists() is used directly here. danielebarchiesi@0: if (!function_exists($module . '_' . $hook)) { danielebarchiesi@0: // Clear out the stale implementation from the cache and force a cache danielebarchiesi@0: // refresh to forget about no longer existing hook implementations. danielebarchiesi@0: unset($implementations[$hook][$module]); danielebarchiesi@0: $implementations['#write_cache'] = TRUE; danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: return array_keys($implementations[$hook]); danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Retrieves a list of hooks that are declared through hook_hook_info(). danielebarchiesi@0: * danielebarchiesi@0: * @return danielebarchiesi@0: * An associative array whose keys are hook names and whose values are an danielebarchiesi@0: * associative array containing a group name. The structure of the array danielebarchiesi@0: * is the same as the return value of hook_hook_info(). danielebarchiesi@0: * danielebarchiesi@0: * @see hook_hook_info() danielebarchiesi@0: */ danielebarchiesi@0: function module_hook_info() { danielebarchiesi@0: // This function is indirectly invoked from bootstrap_invoke_all(), in which danielebarchiesi@0: // case common.inc, subsystems, and modules are not loaded yet, so it does not danielebarchiesi@0: // make sense to support hook groups resp. lazy-loaded include files prior to danielebarchiesi@0: // full bootstrap. danielebarchiesi@0: if (drupal_bootstrap(NULL, FALSE) != DRUPAL_BOOTSTRAP_FULL) { danielebarchiesi@0: return array(); danielebarchiesi@0: } danielebarchiesi@0: $hook_info = &drupal_static(__FUNCTION__); danielebarchiesi@0: danielebarchiesi@0: if (!isset($hook_info)) { danielebarchiesi@0: $hook_info = array(); danielebarchiesi@0: $cache = cache_get('hook_info', 'cache_bootstrap'); danielebarchiesi@0: if ($cache === FALSE) { danielebarchiesi@0: // Rebuild the cache and save it. danielebarchiesi@0: // We can't use module_invoke_all() here or it would cause an infinite danielebarchiesi@0: // loop. danielebarchiesi@0: foreach (module_list() as $module) { danielebarchiesi@0: $function = $module . '_hook_info'; danielebarchiesi@0: if (function_exists($function)) { danielebarchiesi@0: $result = $function(); danielebarchiesi@0: if (isset($result) && is_array($result)) { danielebarchiesi@0: $hook_info = array_merge_recursive($hook_info, $result); danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: // We can't use drupal_alter() for the same reason as above. danielebarchiesi@0: foreach (module_list() as $module) { danielebarchiesi@0: $function = $module . '_hook_info_alter'; danielebarchiesi@0: if (function_exists($function)) { danielebarchiesi@0: $function($hook_info); danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: cache_set('hook_info', $hook_info, 'cache_bootstrap'); danielebarchiesi@0: } danielebarchiesi@0: else { danielebarchiesi@0: $hook_info = $cache->data; danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: return $hook_info; danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Writes the hook implementation cache. danielebarchiesi@0: * danielebarchiesi@0: * @see module_implements() danielebarchiesi@0: */ danielebarchiesi@0: function module_implements_write_cache() { danielebarchiesi@0: $implementations = &drupal_static('module_implements'); danielebarchiesi@0: if (isset($implementations['#write_cache'])) { danielebarchiesi@0: unset($implementations['#write_cache']); danielebarchiesi@0: cache_set('module_implements', $implementations, 'cache_bootstrap'); danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Invokes a hook in a particular module. danielebarchiesi@0: * danielebarchiesi@0: * All arguments are passed by value. Use drupal_alter() if you need to pass danielebarchiesi@0: * arguments by reference. danielebarchiesi@0: * danielebarchiesi@0: * @param $module danielebarchiesi@0: * The name of the module (without the .module extension). danielebarchiesi@0: * @param $hook danielebarchiesi@0: * The name of the hook to invoke. danielebarchiesi@0: * @param ... danielebarchiesi@0: * Arguments to pass to the hook implementation. danielebarchiesi@0: * danielebarchiesi@0: * @return danielebarchiesi@0: * The return value of the hook implementation. danielebarchiesi@0: * danielebarchiesi@0: * @see drupal_alter() danielebarchiesi@0: */ danielebarchiesi@0: function module_invoke($module, $hook) { danielebarchiesi@0: $args = func_get_args(); danielebarchiesi@0: // Remove $module and $hook from the arguments. danielebarchiesi@0: unset($args[0], $args[1]); danielebarchiesi@0: if (module_hook($module, $hook)) { danielebarchiesi@0: return call_user_func_array($module . '_' . $hook, $args); danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Invokes a hook in all enabled modules that implement it. danielebarchiesi@0: * danielebarchiesi@0: * All arguments are passed by value. Use drupal_alter() if you need to pass danielebarchiesi@0: * arguments by reference. danielebarchiesi@0: * danielebarchiesi@0: * @param $hook danielebarchiesi@0: * The name of the hook to invoke. danielebarchiesi@0: * @param ... danielebarchiesi@0: * Arguments to pass to the hook. danielebarchiesi@0: * danielebarchiesi@0: * @return danielebarchiesi@0: * An array of return values of the hook implementations. If modules return danielebarchiesi@0: * arrays from their implementations, those are merged into one array. danielebarchiesi@0: * danielebarchiesi@0: * @see drupal_alter() danielebarchiesi@0: */ danielebarchiesi@0: function module_invoke_all($hook) { danielebarchiesi@0: $args = func_get_args(); danielebarchiesi@0: // Remove $hook from the arguments. danielebarchiesi@0: unset($args[0]); danielebarchiesi@0: $return = array(); danielebarchiesi@0: foreach (module_implements($hook) as $module) { danielebarchiesi@0: $function = $module . '_' . $hook; danielebarchiesi@0: if (function_exists($function)) { danielebarchiesi@0: $result = call_user_func_array($function, $args); danielebarchiesi@0: if (isset($result) && is_array($result)) { danielebarchiesi@0: $return = array_merge_recursive($return, $result); danielebarchiesi@0: } danielebarchiesi@0: elseif (isset($result)) { danielebarchiesi@0: $return[] = $result; danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: return $return; danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * @} End of "defgroup hooks". danielebarchiesi@0: */ danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Returns an array of modules required by core. danielebarchiesi@0: */ danielebarchiesi@0: function drupal_required_modules() { danielebarchiesi@0: $files = drupal_system_listing('/^' . DRUPAL_PHP_FUNCTION_PATTERN . '\.info$/', 'modules', 'name', 0); danielebarchiesi@0: $required = array(); danielebarchiesi@0: danielebarchiesi@0: // An installation profile is required and one must always be loaded. danielebarchiesi@0: $required[] = drupal_get_profile(); danielebarchiesi@0: danielebarchiesi@0: foreach ($files as $name => $file) { danielebarchiesi@0: $info = drupal_parse_info_file($file->uri); danielebarchiesi@0: if (!empty($info) && !empty($info['required']) && $info['required']) { danielebarchiesi@0: $required[] = $name; danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: return $required; danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Passes alterable variables to specific hook_TYPE_alter() implementations. danielebarchiesi@0: * danielebarchiesi@0: * This dispatch function hands off the passed-in variables to type-specific danielebarchiesi@0: * hook_TYPE_alter() implementations in modules. It ensures a consistent danielebarchiesi@0: * interface for all altering operations. danielebarchiesi@0: * danielebarchiesi@0: * A maximum of 2 alterable arguments is supported (a third is supported for danielebarchiesi@0: * legacy reasons, but should not be used in new code). In case more arguments danielebarchiesi@0: * need to be passed and alterable, modules provide additional variables danielebarchiesi@0: * assigned by reference in the last $context argument: danielebarchiesi@0: * @code danielebarchiesi@0: * $context = array( danielebarchiesi@0: * 'alterable' => &$alterable, danielebarchiesi@0: * 'unalterable' => $unalterable, danielebarchiesi@0: * 'foo' => 'bar', danielebarchiesi@0: * ); danielebarchiesi@0: * drupal_alter('mymodule_data', $alterable1, $alterable2, $context); danielebarchiesi@0: * @endcode danielebarchiesi@0: * danielebarchiesi@0: * Note that objects are always passed by reference in PHP5. If it is absolutely danielebarchiesi@0: * required that no implementation alters a passed object in $context, then an danielebarchiesi@0: * object needs to be cloned: danielebarchiesi@0: * @code danielebarchiesi@0: * $context = array( danielebarchiesi@0: * 'unalterable_object' => clone $object, danielebarchiesi@0: * ); danielebarchiesi@0: * drupal_alter('mymodule_data', $data, $context); danielebarchiesi@0: * @endcode danielebarchiesi@0: * danielebarchiesi@0: * @param $type danielebarchiesi@0: * A string describing the type of the alterable $data. 'form', 'links', danielebarchiesi@0: * 'node_content', and so on are several examples. Alternatively can be an danielebarchiesi@0: * array, in which case hook_TYPE_alter() is invoked for each value in the danielebarchiesi@0: * array, ordered first by module, and then for each module, in the order of danielebarchiesi@0: * values in $type. For example, when Form API is using drupal_alter() to danielebarchiesi@0: * execute both hook_form_alter() and hook_form_FORM_ID_alter() danielebarchiesi@0: * implementations, it passes array('form', 'form_' . $form_id) for $type. danielebarchiesi@0: * @param $data danielebarchiesi@0: * The variable that will be passed to hook_TYPE_alter() implementations to be danielebarchiesi@0: * altered. The type of this variable depends on the value of the $type danielebarchiesi@0: * argument. For example, when altering a 'form', $data will be a structured danielebarchiesi@0: * array. When altering a 'profile', $data will be an object. danielebarchiesi@0: * @param $context1 danielebarchiesi@0: * (optional) An additional variable that is passed by reference. danielebarchiesi@0: * @param $context2 danielebarchiesi@0: * (optional) An additional variable that is passed by reference. If more danielebarchiesi@0: * context needs to be provided to implementations, then this should be an danielebarchiesi@0: * associative array as described above. danielebarchiesi@0: * @param $context3 danielebarchiesi@0: * (optional) An additional variable that is passed by reference. This danielebarchiesi@0: * parameter is deprecated and will not exist in Drupal 8; consequently, it danielebarchiesi@0: * should not be used for new Drupal 7 code either. It is here only for danielebarchiesi@0: * backwards compatibility with older code that passed additional arguments danielebarchiesi@0: * to drupal_alter(). danielebarchiesi@0: */ danielebarchiesi@0: function drupal_alter($type, &$data, &$context1 = NULL, &$context2 = NULL, &$context3 = NULL) { danielebarchiesi@0: // Use the advanced drupal_static() pattern, since this is called very often. danielebarchiesi@0: static $drupal_static_fast; danielebarchiesi@0: if (!isset($drupal_static_fast)) { danielebarchiesi@0: $drupal_static_fast['functions'] = &drupal_static(__FUNCTION__); danielebarchiesi@0: } danielebarchiesi@0: $functions = &$drupal_static_fast['functions']; danielebarchiesi@0: danielebarchiesi@0: // Most of the time, $type is passed as a string, so for performance, danielebarchiesi@0: // normalize it to that. When passed as an array, usually the first item in danielebarchiesi@0: // the array is a generic type, and additional items in the array are more danielebarchiesi@0: // specific variants of it, as in the case of array('form', 'form_FORM_ID'). danielebarchiesi@0: if (is_array($type)) { danielebarchiesi@0: $cid = implode(',', $type); danielebarchiesi@0: $extra_types = $type; danielebarchiesi@0: $type = array_shift($extra_types); danielebarchiesi@0: // Allow if statements in this function to use the faster isset() rather danielebarchiesi@0: // than !empty() both when $type is passed as a string, or as an array with danielebarchiesi@0: // one item. danielebarchiesi@0: if (empty($extra_types)) { danielebarchiesi@0: unset($extra_types); danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: else { danielebarchiesi@0: $cid = $type; danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: // Some alter hooks are invoked many times per page request, so statically danielebarchiesi@0: // cache the list of functions to call, and on subsequent calls, iterate danielebarchiesi@0: // through them quickly. danielebarchiesi@0: if (!isset($functions[$cid])) { danielebarchiesi@0: $functions[$cid] = array(); danielebarchiesi@0: $hook = $type . '_alter'; danielebarchiesi@0: $modules = module_implements($hook); danielebarchiesi@0: if (!isset($extra_types)) { danielebarchiesi@0: // For the more common case of a single hook, we do not need to call danielebarchiesi@0: // function_exists(), since module_implements() returns only modules with danielebarchiesi@0: // implementations. danielebarchiesi@0: foreach ($modules as $module) { danielebarchiesi@0: $functions[$cid][] = $module . '_' . $hook; danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: else { danielebarchiesi@0: // For multiple hooks, we need $modules to contain every module that danielebarchiesi@0: // implements at least one of them. danielebarchiesi@0: $extra_modules = array(); danielebarchiesi@0: foreach ($extra_types as $extra_type) { danielebarchiesi@0: $extra_modules = array_merge($extra_modules, module_implements($extra_type . '_alter')); danielebarchiesi@0: } danielebarchiesi@0: // If any modules implement one of the extra hooks that do not implement danielebarchiesi@0: // the primary hook, we need to add them to the $modules array in their danielebarchiesi@0: // appropriate order. module_implements() can only return ordered danielebarchiesi@0: // implementations of a single hook. To get the ordered implementations danielebarchiesi@0: // of multiple hooks, we mimic the module_implements() logic of first danielebarchiesi@0: // ordering by module_list(), and then calling danielebarchiesi@0: // drupal_alter('module_implements'). danielebarchiesi@0: if (array_diff($extra_modules, $modules)) { danielebarchiesi@0: // Merge the arrays and order by module_list(). danielebarchiesi@0: $modules = array_intersect(module_list(), array_merge($modules, $extra_modules)); danielebarchiesi@0: // Since module_implements() already took care of loading the necessary danielebarchiesi@0: // include files, we can safely pass FALSE for the array values. danielebarchiesi@0: $implementations = array_fill_keys($modules, FALSE); danielebarchiesi@0: // Let modules adjust the order solely based on the primary hook. This danielebarchiesi@0: // ensures the same module order regardless of whether this if block danielebarchiesi@0: // runs. Calling drupal_alter() recursively in this way does not result danielebarchiesi@0: // in an infinite loop, because this call is for a single $type, so we danielebarchiesi@0: // won't end up in this code block again. danielebarchiesi@0: drupal_alter('module_implements', $implementations, $hook); danielebarchiesi@0: $modules = array_keys($implementations); danielebarchiesi@0: } danielebarchiesi@0: foreach ($modules as $module) { danielebarchiesi@0: // Since $modules is a merged array, for any given module, we do not danielebarchiesi@0: // know whether it has any particular implementation, so we need a danielebarchiesi@0: // function_exists(). danielebarchiesi@0: $function = $module . '_' . $hook; danielebarchiesi@0: if (function_exists($function)) { danielebarchiesi@0: $functions[$cid][] = $function; danielebarchiesi@0: } danielebarchiesi@0: foreach ($extra_types as $extra_type) { danielebarchiesi@0: $function = $module . '_' . $extra_type . '_alter'; danielebarchiesi@0: if (function_exists($function)) { danielebarchiesi@0: $functions[$cid][] = $function; danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: // Allow the theme to alter variables after the theme system has been danielebarchiesi@0: // initialized. danielebarchiesi@0: global $theme, $base_theme_info; danielebarchiesi@0: if (isset($theme)) { danielebarchiesi@0: $theme_keys = array(); danielebarchiesi@0: foreach ($base_theme_info as $base) { danielebarchiesi@0: $theme_keys[] = $base->name; danielebarchiesi@0: } danielebarchiesi@0: $theme_keys[] = $theme; danielebarchiesi@0: foreach ($theme_keys as $theme_key) { danielebarchiesi@0: $function = $theme_key . '_' . $hook; danielebarchiesi@0: if (function_exists($function)) { danielebarchiesi@0: $functions[$cid][] = $function; danielebarchiesi@0: } danielebarchiesi@0: if (isset($extra_types)) { danielebarchiesi@0: foreach ($extra_types as $extra_type) { danielebarchiesi@0: $function = $theme_key . '_' . $extra_type . '_alter'; danielebarchiesi@0: if (function_exists($function)) { danielebarchiesi@0: $functions[$cid][] = $function; danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: foreach ($functions[$cid] as $function) { danielebarchiesi@0: $function($data, $context1, $context2, $context3); danielebarchiesi@0: } danielebarchiesi@0: }