module.inc 8.88 KB
Newer Older
1 2
<?php

3 4 5 6 7
/**
 * @file
 * API for loading and interacting with Drupal modules.
 */

8
use Drupal\Core\Extension\ExtensionDiscovery;
9

10
/**
11
 * Builds a list of installed themes.
12 13
 *
 * @param $type
14
 *   The type of list to return:
15
 *   - theme: All installed themes.
16
 *
17
 * @return array
18
 *   An associative array of themes, keyed by name.
19 20
 *   For $type 'theme', the array values are objects representing the
 *   respective database row, with the 'info' property already unserialized.
21
 *
22 23 24 25
 * @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
26
 * @see \Drupal\Core\Extension\ThemeHandler::listInfo()
27 28
 */
function system_list($type) {
29 30 31 32 33 34 35 36 37 38 39
  @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);

  $lists = [
    'theme' => \Drupal::service('theme_handler')->listInfo(),
    'filepaths' => [],
  ];
  foreach ($lists['theme'] as $name => $theme) {
    $lists['filepaths'][] = [
      'type' => 'theme',
      'name' => $name,
      'filepath' => $theme->getPathname(),
40
    ];
41 42 43 44
  }
  return $lists[$type];
}

45
/**
46
 * Resets all system_list() caches.
47 48 49 50 51 52
 *
 * @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
53 54
 */
function system_list_reset() {
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);
56
  \Drupal::service('extension.list.profile')->reset();
57
  \Drupal::service('extension.list.module')->reset();
58 59
  \Drupal::service('extension.list.theme_engine')->reset();
  \Drupal::service('extension.list.theme')->reset();
60 61
}

62 63 64 65 66 67 68 69 70 71
/**
 * Registers an extension in runtime registries for execution.
 *
 * @param string $type
 *   The extension type; e.g., 'module' or 'theme'.
 * @param string $name
 *   The internal name of the extension; e.g., 'node'.
 * @param string $uri
 *   The relative URI of the primary extension file; e.g.,
 *   'core/modules/node/node.module'.
72 73 74 75 76 77 78 79
 *
 * @deprecated in Drupal 8.8.0 and will be removed before Drupal 9.0.0. There is
 *   no replacement for this function. Use the following sequence of code to
 *   achieve the same functionality:
 *   @code
 *   $path = \Drupal::service("extension.list.$type")->getPath($name);
 *   \Drupal::service('class_loader')->addPsr4('Drupal\\' . $name . '\\', \Drupal::root() . '/' . $path . '/src');
 *   @endcode
80 81
 */
function system_register($type, $name, $uri) {
82 83
  @trigger_error('system_register() is deprecated in Drupal 8.8.0 and will be removed before Drupal 9.0.0. There is no replacement for this function. To achieve the same functionality use this snippet: $path = \Drupal::service("extension.list.$type")->getPath($name); ' . "\\Drupal::service('class_loader')->addPsr4('Drupal\\\\' . \$name . '\\\\', \\Drupal::root() . '/' . \$path . '/src'); See https://www.drupal.org/node/3035275.", E_USER_DEPRECATED);
  \Drupal::service('class_loader')->addPsr4('Drupal\\' . $name . '\\', \Drupal::root() . '/' . dirname($uri) . '/src');
84 85
}

Steven Wittens's avatar
Steven Wittens committed
86
/**
87
 * Loads a module's installation hooks.
88 89 90 91 92 93
 *
 * @param $module
 *   The name of the module (without the .module extension).
 *
 * @return
 *   The name of the module's install file, if successful; FALSE otherwise.
Steven Wittens's avatar
Steven Wittens committed
94 95 96
 */
function module_load_install($module) {
  // Make sure the installation API is available
97
  include_once __DIR__ . '/install.inc';
Steven Wittens's avatar
Steven Wittens committed
98

99
  return module_load_include('install', $module);
100 101 102
}

/**
103
 * Loads a module include file.
104
 *
105 106
 * Examples:
 * @code
107
 *   // Load node.admin.inc from the node module.
108
 *   module_load_include('inc', 'node', 'node.admin');
109
 *   // Load content_types.inc from the node module.
110
 *   module_load_include('inc', 'node', 'content_types');
111
 * @endcode
112
 *
113 114 115
 * Do not use this function to load an install file, use module_load_install()
 * instead. Do not use this function in a global context since it requires
 * Drupal to be fully bootstrapped, use require_once DRUPAL_ROOT . '/path/file'
116 117
 * instead.
 *
118 119 120 121 122
 * @param $type
 *   The include file's type (file extension).
 * @param $module
 *   The module to which the include file belongs.
 * @param $name
123 124
 *   (optional) The base file name (without the $type extension). If omitted,
 *   $module is used; i.e., resulting in "$module.$type" by default.
125 126 127
 *
 * @return
 *   The name of the included file, if successful; FALSE otherwise.
128 129 130 131 132
 *
 * @todo The module_handler service has a loadInclude() method which performs
 *   this same task but only for enabled modules. Figure out a way to move this
 *   functionality entirely into the module_handler while keeping the ability to
 *   load the files of disabled modules.
133 134
 */
function module_load_include($type, $module, $name = NULL) {
135
  if (!isset($name)) {
136 137 138
    $name = $module;
  }

139
  if (function_exists('drupal_get_path')) {
140 141 142 143 144
    $file = DRUPAL_ROOT . '/' . drupal_get_path('module', $module) . "/$name.$type";
    if (is_file($file)) {
      require_once $file;
      return $file;
    }
145
  }
146
  return FALSE;
147 148
}

149
/**
150
 * Returns an array of modules required by core.
151 152
 */
function drupal_required_modules() {
153
  $listing = new ExtensionDiscovery(\Drupal::root());
154
  $files = $listing->scan('module');
155
  $required = [];
156

157 158 159 160 161 162
  // Unless called by the installer, an installation profile is required and
  // must always be loaded. drupal_get_profile() also returns the installation
  // profile in the installer, but only after it has been selected.
  if ($profile = drupal_get_profile()) {
    $required[] = $profile;
  }
163

164
  foreach ($files as $name => $file) {
165
    $info = \Drupal::service('info_parser')->parse($file->getPathname());
166 167 168 169
    if (!empty($info) && !empty($info['required']) && $info['required']) {
      $required[] = $name;
    }
  }
170

171
  return $required;
172
}
173

174 175 176 177 178 179 180 181 182 183 184
/**
 * Sets weight of a particular module.
 *
 * The weight of uninstalled modules cannot be changed.
 *
 * @param string $module
 *   The name of the module (without the .module extension).
 * @param int $weight
 *   An integer representing the weight of the module.
 */
function module_set_weight($module, $weight) {
185
  $extension_config = \Drupal::configFactory()->getEditable('core.extension');
186
  if ($extension_config->get("module.$module") !== NULL) {
187 188
    // Pre-cast the $weight to an integer so that we can save this without using
    // schema. This is a performance improvement for module installation.
189
    $extension_config
190
      ->set("module.$module", (int) $weight)
191
      ->set('module', module_config_sort($extension_config->get('module')))
192
      ->save(TRUE);
193 194

    // Prepare the new module list, sorted by weight, including filenames.
195
    // @see \Drupal\Core\Extension\ModuleInstaller::install()
196
    $module_handler = \Drupal::moduleHandler();
197 198
    $current_module_filenames = $module_handler->getModuleList();
    $current_modules = array_fill_keys(array_keys($current_module_filenames), 0);
199
    $current_modules = module_config_sort(array_merge($current_modules, $extension_config->get('module')));
200
    $module_filenames = [];
201 202 203 204 205
    foreach ($current_modules as $name => $weight) {
      $module_filenames[$name] = $current_module_filenames[$name];
    }
    // Update the module list in the extension handler.
    $module_handler->setModuleList($module_filenames);
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
    return;
  }
}

/**
 * Sorts the configured list of enabled modules.
 *
 * The list of enabled modules is expected to be ordered by weight and name.
 * The list is always sorted on write to avoid the overhead on read.
 *
 * @param array $data
 *   An array of module configuration data.
 *
 * @return array
 *   An array of module configuration data sorted by weight and name.
 */
function module_config_sort($data) {
  // PHP array sorting functions such as uasort() do not work with both keys and
  // values at the same time, so we achieve weight and name sorting by computing
  // strings with both information concatenated (weight first, name second) and
  // use that as a regular string sort reference list via array_multisort(),
  // compound of "[sign-as-integer][padded-integer-weight][name]"; e.g., given
  // two modules and weights (spaces added for clarity):
  // - Block with weight -5: 0 0000000000000000005 block
  // - Node  with weight  0: 1 0000000000000000000 node
231
  $sort = [];
232 233 234 235 236 237 238 239 240 241
  foreach ($data as $name => $weight) {
    // Prefix negative weights with 0, positive weights with 1.
    // +/- signs cannot be used, since + (ASCII 43) is before - (ASCII 45).
    $prefix = (int) ($weight >= 0);
    // The maximum weight is PHP_INT_MAX, so pad all weights to 19 digits.
    $sort[] = $prefix . sprintf('%019d', abs($weight)) . $name;
  }
  array_multisort($sort, SORT_STRING, $data);
  return $data;
}