annotate core/includes/module.inc @ 19:fa3358dc1485 tip

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