annotate core/includes/module.inc @ 17:129ea1e6d783

Update, including to Drupal core 8.6.10
author Chris Cannam
date Thu, 28 Feb 2019 13:21:36 +0000
parents 4c8ae668cc8c
children af1871eacc83
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 /**
Chris@0 4 * @file
Chris@0 5 * API for loading and interacting with Drupal modules.
Chris@0 6 */
Chris@0 7
Chris@0 8 use Drupal\Core\Extension\ExtensionDiscovery;
Chris@0 9
Chris@0 10 /**
Chris@0 11 * Builds a list of installed themes.
Chris@0 12 *
Chris@0 13 * @param $type
Chris@0 14 * The type of list to return:
Chris@0 15 * - theme: All installed themes.
Chris@0 16 *
Chris@0 17 * @return
Chris@0 18 * An associative array of themes, keyed by name.
Chris@0 19 * For $type 'theme', the array values are objects representing the
Chris@0 20 * respective database row, with the 'info' property already unserialized.
Chris@0 21 *
Chris@0 22 * @see \Drupal\Core\Extension\ThemeHandler::listInfo()
Chris@0 23 */
Chris@0 24 function system_list($type) {
Chris@0 25 $lists = &drupal_static(__FUNCTION__);
Chris@0 26 if ($cached = \Drupal::cache('bootstrap')->get('system_list')) {
Chris@0 27 $lists = $cached->data;
Chris@0 28 }
Chris@0 29 else {
Chris@0 30 $lists = [
Chris@0 31 'theme' => [],
Chris@0 32 'filepaths' => [],
Chris@0 33 ];
Chris@0 34 // ThemeHandler maintains the 'system.theme.data' state record.
Chris@0 35 $theme_data = \Drupal::state()->get('system.theme.data', []);
Chris@0 36 foreach ($theme_data as $name => $theme) {
Chris@0 37 $lists['theme'][$name] = $theme;
Chris@0 38 $lists['filepaths'][] = [
Chris@0 39 'type' => 'theme',
Chris@0 40 'name' => $name,
Chris@0 41 'filepath' => $theme->getPathname(),
Chris@0 42 ];
Chris@0 43 }
Chris@0 44 \Drupal::cache('bootstrap')->set('system_list', $lists);
Chris@0 45 }
Chris@0 46 // To avoid a separate database lookup for the filepath, prime the
Chris@0 47 // drupal_get_filename() static cache with all enabled themes.
Chris@0 48 foreach ($lists['filepaths'] as $item) {
Chris@0 49 system_register($item['type'], $item['name'], $item['filepath']);
Chris@0 50 }
Chris@0 51
Chris@0 52 return $lists[$type];
Chris@0 53 }
Chris@0 54
Chris@0 55 /**
Chris@0 56 * Resets all system_list() caches.
Chris@0 57 */
Chris@0 58 function system_list_reset() {
Chris@0 59 drupal_static_reset('system_list');
Chris@17 60 \Drupal::service('extension.list.module')->reset();
Chris@0 61 \Drupal::cache('bootstrap')->delete('system_list');
Chris@0 62 }
Chris@0 63
Chris@0 64 /**
Chris@0 65 * Registers an extension in runtime registries for execution.
Chris@0 66 *
Chris@0 67 * @param string $type
Chris@0 68 * The extension type; e.g., 'module' or 'theme'.
Chris@0 69 * @param string $name
Chris@0 70 * The internal name of the extension; e.g., 'node'.
Chris@0 71 * @param string $uri
Chris@0 72 * The relative URI of the primary extension file; e.g.,
Chris@0 73 * 'core/modules/node/node.module'.
Chris@0 74 */
Chris@0 75 function system_register($type, $name, $uri) {
Chris@0 76 drupal_get_filename($type, $name, $uri);
Chris@0 77 drupal_classloader_register($name, dirname($uri));
Chris@0 78 }
Chris@0 79
Chris@0 80 /**
Chris@0 81 * Loads a module's installation hooks.
Chris@0 82 *
Chris@0 83 * @param $module
Chris@0 84 * The name of the module (without the .module extension).
Chris@0 85 *
Chris@0 86 * @return
Chris@0 87 * The name of the module's install file, if successful; FALSE otherwise.
Chris@0 88 */
Chris@0 89 function module_load_install($module) {
Chris@0 90 // Make sure the installation API is available
Chris@0 91 include_once __DIR__ . '/install.inc';
Chris@0 92
Chris@0 93 return module_load_include('install', $module);
Chris@0 94 }
Chris@0 95
Chris@0 96 /**
Chris@0 97 * Loads a module include file.
Chris@0 98 *
Chris@0 99 * Examples:
Chris@0 100 * @code
Chris@0 101 * // Load node.admin.inc from the node module.
Chris@0 102 * module_load_include('inc', 'node', 'node.admin');
Chris@0 103 * // Load content_types.inc from the node module.
Chris@0 104 * module_load_include('inc', 'node', 'content_types');
Chris@0 105 * @endcode
Chris@0 106 *
Chris@0 107 * Do not use this function to load an install file, use module_load_install()
Chris@0 108 * instead. Do not use this function in a global context since it requires
Chris@0 109 * Drupal to be fully bootstrapped, use require_once DRUPAL_ROOT . '/path/file'
Chris@0 110 * instead.
Chris@0 111 *
Chris@0 112 * @param $type
Chris@0 113 * The include file's type (file extension).
Chris@0 114 * @param $module
Chris@0 115 * The module to which the include file belongs.
Chris@0 116 * @param $name
Chris@0 117 * (optional) The base file name (without the $type extension). If omitted,
Chris@0 118 * $module is used; i.e., resulting in "$module.$type" by default.
Chris@0 119 *
Chris@0 120 * @return
Chris@0 121 * The name of the included file, if successful; FALSE otherwise.
Chris@0 122 *
Chris@0 123 * @todo The module_handler service has a loadInclude() method which performs
Chris@0 124 * this same task but only for enabled modules. Figure out a way to move this
Chris@0 125 * functionality entirely into the module_handler while keeping the ability to
Chris@0 126 * load the files of disabled modules.
Chris@0 127 */
Chris@0 128 function module_load_include($type, $module, $name = NULL) {
Chris@0 129 if (!isset($name)) {
Chris@0 130 $name = $module;
Chris@0 131 }
Chris@0 132
Chris@0 133 if (function_exists('drupal_get_path')) {
Chris@0 134 $file = DRUPAL_ROOT . '/' . drupal_get_path('module', $module) . "/$name.$type";
Chris@0 135 if (is_file($file)) {
Chris@0 136 require_once $file;
Chris@0 137 return $file;
Chris@0 138 }
Chris@0 139 }
Chris@0 140 return FALSE;
Chris@0 141 }
Chris@0 142
Chris@0 143 /**
Chris@0 144 * Returns an array of modules required by core.
Chris@0 145 */
Chris@0 146 function drupal_required_modules() {
Chris@0 147 $listing = new ExtensionDiscovery(\Drupal::root());
Chris@0 148 $files = $listing->scan('module');
Chris@0 149 $required = [];
Chris@0 150
Chris@0 151 // Unless called by the installer, an installation profile is required and
Chris@0 152 // must always be loaded. drupal_get_profile() also returns the installation
Chris@0 153 // profile in the installer, but only after it has been selected.
Chris@0 154 if ($profile = drupal_get_profile()) {
Chris@0 155 $required[] = $profile;
Chris@0 156 }
Chris@0 157
Chris@0 158 foreach ($files as $name => $file) {
Chris@0 159 $info = \Drupal::service('info_parser')->parse($file->getPathname());
Chris@0 160 if (!empty($info) && !empty($info['required']) && $info['required']) {
Chris@0 161 $required[] = $name;
Chris@0 162 }
Chris@0 163 }
Chris@0 164
Chris@0 165 return $required;
Chris@0 166 }
Chris@0 167
Chris@0 168 /**
Chris@0 169 * Sets weight of a particular module.
Chris@0 170 *
Chris@0 171 * The weight of uninstalled modules cannot be changed.
Chris@0 172 *
Chris@0 173 * @param string $module
Chris@0 174 * The name of the module (without the .module extension).
Chris@0 175 * @param int $weight
Chris@0 176 * An integer representing the weight of the module.
Chris@0 177 */
Chris@0 178 function module_set_weight($module, $weight) {
Chris@0 179 $extension_config = \Drupal::configFactory()->getEditable('core.extension');
Chris@0 180 if ($extension_config->get("module.$module") !== NULL) {
Chris@0 181 // Pre-cast the $weight to an integer so that we can save this without using
Chris@0 182 // schema. This is a performance improvement for module installation.
Chris@0 183 $extension_config
Chris@0 184 ->set("module.$module", (int) $weight)
Chris@0 185 ->set('module', module_config_sort($extension_config->get('module')))
Chris@0 186 ->save(TRUE);
Chris@0 187
Chris@0 188 // Prepare the new module list, sorted by weight, including filenames.
Chris@0 189 // @see \Drupal\Core\Extension\ModuleInstaller::install()
Chris@0 190 $module_handler = \Drupal::moduleHandler();
Chris@0 191 $current_module_filenames = $module_handler->getModuleList();
Chris@0 192 $current_modules = array_fill_keys(array_keys($current_module_filenames), 0);
Chris@0 193 $current_modules = module_config_sort(array_merge($current_modules, $extension_config->get('module')));
Chris@0 194 $module_filenames = [];
Chris@0 195 foreach ($current_modules as $name => $weight) {
Chris@0 196 $module_filenames[$name] = $current_module_filenames[$name];
Chris@0 197 }
Chris@0 198 // Update the module list in the extension handler.
Chris@0 199 $module_handler->setModuleList($module_filenames);
Chris@0 200 return;
Chris@0 201 }
Chris@0 202 }
Chris@0 203
Chris@0 204 /**
Chris@0 205 * Sorts the configured list of enabled modules.
Chris@0 206 *
Chris@0 207 * The list of enabled modules is expected to be ordered by weight and name.
Chris@0 208 * The list is always sorted on write to avoid the overhead on read.
Chris@0 209 *
Chris@0 210 * @param array $data
Chris@0 211 * An array of module configuration data.
Chris@0 212 *
Chris@0 213 * @return array
Chris@0 214 * An array of module configuration data sorted by weight and name.
Chris@0 215 */
Chris@0 216 function module_config_sort($data) {
Chris@0 217 // PHP array sorting functions such as uasort() do not work with both keys and
Chris@0 218 // values at the same time, so we achieve weight and name sorting by computing
Chris@0 219 // strings with both information concatenated (weight first, name second) and
Chris@0 220 // use that as a regular string sort reference list via array_multisort(),
Chris@0 221 // compound of "[sign-as-integer][padded-integer-weight][name]"; e.g., given
Chris@0 222 // two modules and weights (spaces added for clarity):
Chris@0 223 // - Block with weight -5: 0 0000000000000000005 block
Chris@0 224 // - Node with weight 0: 1 0000000000000000000 node
Chris@0 225 $sort = [];
Chris@0 226 foreach ($data as $name => $weight) {
Chris@0 227 // Prefix negative weights with 0, positive weights with 1.
Chris@0 228 // +/- signs cannot be used, since + (ASCII 43) is before - (ASCII 45).
Chris@0 229 $prefix = (int) ($weight >= 0);
Chris@0 230 // The maximum weight is PHP_INT_MAX, so pad all weights to 19 digits.
Chris@0 231 $sort[] = $prefix . sprintf('%019d', abs($weight)) . $name;
Chris@0 232 }
Chris@0 233 array_multisort($sort, SORT_STRING, $data);
Chris@0 234 return $data;
Chris@0 235 }