module.inc 8.7 KB

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