5 * API for loading and interacting with Drupal modules.
8 use Drupal\Core\Extension\ExtensionDiscovery;
11 * Builds a list of installed themes.
14 * The type of list to return:
15 * - theme: All installed themes.
18 * An associative array of themes, keyed by name.
19 * For $type 'theme', the array values are objects representing the
20 * respective database row, with the 'info' property already unserialized.
22 * @see \Drupal\Core\Extension\ThemeHandler::listInfo()
24 function system_list($type) {
25 $lists = &drupal_static(__FUNCTION__);
26 if ($cached = \Drupal::cache('bootstrap')->get('system_list')) {
27 $lists = $cached->data;
34 // ThemeHandler maintains the 'system.theme.data' state record.
35 $theme_data = \Drupal::state()->get('system.theme.data', []);
36 foreach ($theme_data as $name => $theme) {
37 $lists['theme'][$name] = $theme;
38 $lists['filepaths'][] = [
41 'filepath' => $theme->getPathname(),
44 \Drupal::cache('bootstrap')->set('system_list', $lists);
46 // To avoid a separate database lookup for the filepath, prime the
47 // drupal_get_filename() static cache with all enabled themes.
48 foreach ($lists['filepaths'] as $item) {
49 system_register($item['type'], $item['name'], $item['filepath']);
56 * Resets all system_list() caches.
58 function system_list_reset() {
59 drupal_static_reset('system_list');
60 \Drupal::service('extension.list.module')->reset();
61 \Drupal::cache('bootstrap')->delete('system_list');
65 * Registers an extension in runtime registries for execution.
68 * The extension type; e.g., 'module' or 'theme'.
70 * The internal name of the extension; e.g., 'node'.
72 * The relative URI of the primary extension file; e.g.,
73 * 'core/modules/node/node.module'.
75 function system_register($type, $name, $uri) {
76 drupal_get_filename($type, $name, $uri);
77 drupal_classloader_register($name, dirname($uri));
81 * Loads a module's installation hooks.
84 * The name of the module (without the .module extension).
87 * The name of the module's install file, if successful; FALSE otherwise.
89 function module_load_install($module) {
90 // Make sure the installation API is available
91 include_once __DIR__ . '/install.inc';
93 return module_load_include('install', $module);
97 * Loads a module include file.
101 * // Load node.admin.inc from the node module.
102 * module_load_include('inc', 'node', 'node.admin');
103 * // Load content_types.inc from the node module.
104 * module_load_include('inc', 'node', 'content_types');
107 * Do not use this function to load an install file, use module_load_install()
108 * instead. Do not use this function in a global context since it requires
109 * Drupal to be fully bootstrapped, use require_once DRUPAL_ROOT . '/path/file'
113 * The include file's type (file extension).
115 * The module to which the include file belongs.
117 * (optional) The base file name (without the $type extension). If omitted,
118 * $module is used; i.e., resulting in "$module.$type" by default.
121 * The name of the included file, if successful; FALSE otherwise.
123 * @todo The module_handler service has a loadInclude() method which performs
124 * this same task but only for enabled modules. Figure out a way to move this
125 * functionality entirely into the module_handler while keeping the ability to
126 * load the files of disabled modules.
128 function module_load_include($type, $module, $name = NULL) {
133 if (function_exists('drupal_get_path')) {
134 $file = DRUPAL_ROOT . '/' . drupal_get_path('module', $module) . "/$name.$type";
135 if (is_file($file)) {
144 * Returns an array of modules required by core.
146 function drupal_required_modules() {
147 $listing = new ExtensionDiscovery(\Drupal::root());
148 $files = $listing->scan('module');
151 // Unless called by the installer, an installation profile is required and
152 // must always be loaded. drupal_get_profile() also returns the installation
153 // profile in the installer, but only after it has been selected.
154 if ($profile = drupal_get_profile()) {
155 $required[] = $profile;
158 foreach ($files as $name => $file) {
159 $info = \Drupal::service('info_parser')->parse($file->getPathname());
160 if (!empty($info) && !empty($info['required']) && $info['required']) {
169 * Sets weight of a particular module.
171 * The weight of uninstalled modules cannot be changed.
173 * @param string $module
174 * The name of the module (without the .module extension).
176 * An integer representing the weight of the module.
178 function module_set_weight($module, $weight) {
179 $extension_config = \Drupal::configFactory()->getEditable('core.extension');
180 if ($extension_config->get("module.$module") !== NULL) {
181 // Pre-cast the $weight to an integer so that we can save this without using
182 // schema. This is a performance improvement for module installation.
184 ->set("module.$module", (int) $weight)
185 ->set('module', module_config_sort($extension_config->get('module')))
188 // Prepare the new module list, sorted by weight, including filenames.
189 // @see \Drupal\Core\Extension\ModuleInstaller::install()
190 $module_handler = \Drupal::moduleHandler();
191 $current_module_filenames = $module_handler->getModuleList();
192 $current_modules = array_fill_keys(array_keys($current_module_filenames), 0);
193 $current_modules = module_config_sort(array_merge($current_modules, $extension_config->get('module')));
194 $module_filenames = [];
195 foreach ($current_modules as $name => $weight) {
196 $module_filenames[$name] = $current_module_filenames[$name];
198 // Update the module list in the extension handler.
199 $module_handler->setModuleList($module_filenames);
205 * Sorts the configured list of enabled modules.
207 * The list of enabled modules is expected to be ordered by weight and name.
208 * The list is always sorted on write to avoid the overhead on read.
211 * An array of module configuration data.
214 * An array of module configuration data sorted by weight and name.
216 function module_config_sort($data) {
217 // PHP array sorting functions such as uasort() do not work with both keys and
218 // values at the same time, so we achieve weight and name sorting by computing
219 // strings with both information concatenated (weight first, name second) and
220 // use that as a regular string sort reference list via array_multisort(),
221 // compound of "[sign-as-integer][padded-integer-weight][name]"; e.g., given
222 // two modules and weights (spaces added for clarity):
223 // - Block with weight -5: 0 0000000000000000005 block
224 // - Node with weight 0: 1 0000000000000000000 node
226 foreach ($data as $name => $weight) {
227 // Prefix negative weights with 0, positive weights with 1.
228 // +/- signs cannot be used, since + (ASCII 43) is before - (ASCII 45).
229 $prefix = (int) ($weight >= 0);
230 // The maximum weight is PHP_INT_MAX, so pad all weights to 19 digits.
231 $sort[] = $prefix . sprintf('%019d', abs($weight)) . $name;
233 array_multisort($sort, SORT_STRING, $data);