2002-01-13 13:18:48 +00:00
|
|
|
<?php
|
|
|
|
|
2004-07-22 16:06:54 +00:00
|
|
|
/**
|
|
|
|
* @file
|
|
|
|
* API for loading and interacting with Drupal modules.
|
|
|
|
*/
|
|
|
|
|
2012-04-09 18:02:59 +00:00
|
|
|
|
2009-11-08 09:29:07 +00:00
|
|
|
/**
|
2012-08-31 15:56:36 +00:00
|
|
|
* Builds a list of bootstrap modules and enabled modules and themes.
|
2009-11-08 09:29:07 +00:00
|
|
|
*
|
|
|
|
* @param $type
|
2009-11-09 19:00:03 +00:00
|
|
|
* The type of list to return:
|
|
|
|
* - module_enabled: All enabled modules.
|
|
|
|
* - bootstrap: All enabled modules required for bootstrap.
|
|
|
|
* - theme: All themes.
|
2009-11-08 09:29:07 +00:00
|
|
|
*
|
|
|
|
* @return
|
2010-09-03 19:49:56 +00:00
|
|
|
* An associative array of modules or themes, keyed by name. For $type
|
2012-08-01 17:40:47 +00:00
|
|
|
* 'bootstrap' and 'module_enabled', the array values equal the keys.
|
|
|
|
* For $type 'theme', the array values are objects representing the
|
|
|
|
* respective database row, with the 'info' property already unserialized.
|
2009-11-08 09:29:07 +00:00
|
|
|
*
|
|
|
|
* @see list_themes()
|
2012-10-09 20:32:40 +00:00
|
|
|
*
|
|
|
|
* @todo There are too many layers/levels of caching involved for system_list()
|
|
|
|
* data. Consider to add a config($name, $cache = TRUE) argument to allow
|
|
|
|
* callers like system_list() to force-disable a possible configuration
|
|
|
|
* storage controller cache or some other way to circumvent it/take it over.
|
2009-11-08 09:29:07 +00:00
|
|
|
*/
|
|
|
|
function system_list($type) {
|
|
|
|
$lists = &drupal_static(__FUNCTION__);
|
2013-01-21 19:21:34 +00:00
|
|
|
if ($cached = cache('bootstrap')->get('system_list')) {
|
|
|
|
$lists = $cached->data;
|
2009-11-11 17:04:47 +00:00
|
|
|
}
|
2013-01-21 19:21:34 +00:00
|
|
|
else {
|
|
|
|
$lists = array(
|
|
|
|
'theme' => array(),
|
|
|
|
'filepaths' => array(),
|
|
|
|
);
|
|
|
|
// Build a list of themes.
|
|
|
|
$enabled_themes = (array) config('system.theme')->get('enabled');
|
|
|
|
// @todo Themes include all themes, including disabled/uninstalled. This
|
|
|
|
// system.theme.data state will go away entirely as soon as themes have
|
|
|
|
// a proper installation status.
|
|
|
|
// @see http://drupal.org/node/1067408
|
2013-06-05 14:24:40 +00:00
|
|
|
$theme_data = Drupal::state()->get('system.theme.data');
|
2013-01-21 19:21:34 +00:00
|
|
|
if (empty($theme_data)) {
|
|
|
|
// @todo: system_list() may be called from _drupal_bootstrap_code(), in
|
|
|
|
// which case system.module is not loaded yet.
|
|
|
|
// Prevent a filesystem scan in drupal_load() and include it directly.
|
|
|
|
// @see http://drupal.org/node/1067408
|
|
|
|
require_once DRUPAL_ROOT . '/core/modules/system/system.module';
|
|
|
|
$theme_data = system_rebuild_theme_data();
|
|
|
|
}
|
|
|
|
foreach ($theme_data as $name => $theme) {
|
|
|
|
$theme->status = (int) isset($enabled_themes[$name]);
|
|
|
|
$lists['theme'][$name] = $theme;
|
|
|
|
// Build a list of filenames so drupal_get_filename can use it.
|
|
|
|
if (isset($enabled_themes[$name])) {
|
2012-10-09 20:32:40 +00:00
|
|
|
$lists['filepaths'][] = array(
|
2013-01-21 19:21:34 +00:00
|
|
|
'type' => 'theme',
|
2012-10-09 20:32:40 +00:00
|
|
|
'name' => $name,
|
2013-01-21 19:21:34 +00:00
|
|
|
'filepath' => $theme->filename,
|
2012-10-09 20:32:40 +00:00
|
|
|
);
|
|
|
|
}
|
2013-01-21 19:21:34 +00:00
|
|
|
}
|
|
|
|
// @todo Move into list_themes(). Read info for a particular requested
|
|
|
|
// theme from state instead.
|
|
|
|
foreach ($lists['theme'] as $key => $theme) {
|
|
|
|
if (!empty($theme->info['base theme'])) {
|
|
|
|
// Make a list of the theme's base themes.
|
2013-05-09 09:25:10 +00:00
|
|
|
require_once __DIR__ . '/theme.inc';
|
2013-01-21 19:21:34 +00:00
|
|
|
$lists['theme'][$key]->base_themes = drupal_find_base_themes($lists['theme'], $key);
|
|
|
|
// Don't proceed if there was a problem with the root base theme.
|
|
|
|
if (!current($lists['theme'][$key]->base_themes)) {
|
|
|
|
continue;
|
2013-01-21 13:53:28 +00:00
|
|
|
}
|
2013-01-21 19:21:34 +00:00
|
|
|
// Determine the root base theme.
|
|
|
|
$base_key = key($lists['theme'][$key]->base_themes);
|
|
|
|
// Add to the list of sub-themes for each of the theme's base themes.
|
|
|
|
foreach (array_keys($lists['theme'][$key]->base_themes) as $base_theme) {
|
|
|
|
$lists['theme'][$base_theme]->sub_themes[$key] = $lists['theme'][$key]->info['name'];
|
2013-01-21 13:53:28 +00:00
|
|
|
}
|
2013-01-21 19:21:34 +00:00
|
|
|
// Add the base theme's theme engine info.
|
|
|
|
$lists['theme'][$key]->info['engine'] = $lists['theme'][$base_key]->info['engine'];
|
2013-01-21 11:02:01 +00:00
|
|
|
}
|
2013-01-21 19:21:34 +00:00
|
|
|
else {
|
|
|
|
// A plain theme is its own base theme.
|
|
|
|
$base_key = $key;
|
|
|
|
}
|
|
|
|
// Set the theme engine prefix.
|
|
|
|
$lists['theme'][$key]->prefix = ($lists['theme'][$key]->info['engine'] == 'theme') ? $base_key : $lists['theme'][$key]->info['engine'];
|
2009-11-08 09:29:07 +00:00
|
|
|
}
|
2013-01-21 19:21:34 +00:00
|
|
|
cache('bootstrap')->set('system_list', $lists);
|
|
|
|
}
|
|
|
|
// To avoid a separate database lookup for the filepath, prime the
|
|
|
|
// drupal_get_filename() static cache with all enabled modules and themes.
|
|
|
|
foreach ($lists['filepaths'] as $item) {
|
|
|
|
system_register($item['type'], $item['name'], $item['filepath']);
|
2009-11-08 09:29:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $lists[$type];
|
|
|
|
}
|
|
|
|
|
2009-11-11 17:04:47 +00:00
|
|
|
/**
|
2012-08-31 15:56:36 +00:00
|
|
|
* Resets all system_list() caches.
|
2009-11-11 17:04:47 +00:00
|
|
|
*/
|
|
|
|
function system_list_reset() {
|
|
|
|
drupal_static_reset('system_list');
|
2010-11-24 16:47:44 +00:00
|
|
|
drupal_static_reset('system_rebuild_module_data');
|
2010-06-24 17:32:01 +00:00
|
|
|
drupal_static_reset('list_themes');
|
2013-01-21 19:21:34 +00:00
|
|
|
cache('bootstrap')->delete('system_list');
|
2012-08-01 17:40:47 +00:00
|
|
|
cache()->delete('system_info');
|
2012-10-09 20:32:40 +00:00
|
|
|
// Remove last known theme data state.
|
|
|
|
// This causes system_list() to call system_rebuild_theme_data() on its next
|
|
|
|
// invocation. When enabling a module that implements hook_system_info_alter()
|
|
|
|
// to inject a new (testing) theme or manipulate an existing theme, then that
|
|
|
|
// will cause system_list_reset() to be called, but theme data is not
|
|
|
|
// necessarily rebuilt afterwards.
|
|
|
|
// @todo Obsolete with proper installation status for themes.
|
2013-06-05 14:24:40 +00:00
|
|
|
Drupal::state()->delete('system.theme.data');
|
2009-11-11 17:04:47 +00:00
|
|
|
}
|
|
|
|
|
2012-11-22 11:04:32 +00:00
|
|
|
/**
|
|
|
|
* 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'.
|
|
|
|
*/
|
|
|
|
function system_register($type, $name, $uri) {
|
|
|
|
drupal_get_filename($type, $name, $uri);
|
|
|
|
drupal_classloader_register($name, dirname($uri));
|
|
|
|
}
|
|
|
|
|
2006-08-03 01:02:51 +00:00
|
|
|
/**
|
2012-08-31 15:56:36 +00:00
|
|
|
* Loads a module's installation hooks.
|
2010-11-21 10:19:48 +00:00
|
|
|
*
|
|
|
|
* @param $module
|
|
|
|
* The name of the module (without the .module extension).
|
|
|
|
*
|
|
|
|
* @return
|
|
|
|
* The name of the module's install file, if successful; FALSE otherwise.
|
2006-08-03 01:02:51 +00:00
|
|
|
*/
|
|
|
|
function module_load_install($module) {
|
|
|
|
// Make sure the installation API is available
|
2013-05-09 09:25:10 +00:00
|
|
|
include_once __DIR__ . '/install.inc';
|
2006-08-03 01:02:51 +00:00
|
|
|
|
2010-11-21 10:19:48 +00:00
|
|
|
return module_load_include('install', $module);
|
2007-05-25 12:46:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-08-31 15:56:36 +00:00
|
|
|
* Loads a module include file.
|
2009-07-28 19:06:16 +00:00
|
|
|
*
|
2009-07-01 17:34:13 +00:00
|
|
|
* Examples:
|
|
|
|
* @code
|
2009-07-10 04:58:08 +00:00
|
|
|
* // Load node.admin.inc from the node module.
|
2009-07-01 17:34:13 +00:00
|
|
|
* module_load_include('inc', 'node', 'node.admin');
|
2009-07-10 04:58:08 +00:00
|
|
|
* // Load content_types.inc from the node module.
|
2009-07-28 19:06:16 +00:00
|
|
|
* module_load_include('inc', 'node', 'content_types');
|
2009-07-01 17:34:13 +00:00
|
|
|
* @endcode
|
2007-05-25 12:46:46 +00:00
|
|
|
*
|
2010-01-28 13:56:25 +00:00
|
|
|
* 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'
|
2009-07-10 04:58:08 +00:00
|
|
|
* instead.
|
|
|
|
*
|
2007-05-25 12:46:46 +00:00
|
|
|
* @param $type
|
|
|
|
* The include file's type (file extension).
|
|
|
|
* @param $module
|
|
|
|
* The module to which the include file belongs.
|
|
|
|
* @param $name
|
2010-11-27 19:12:56 +00:00
|
|
|
* (optional) The base file name (without the $type extension). If omitted,
|
|
|
|
* $module is used; i.e., resulting in "$module.$type" by default.
|
2010-11-21 10:19:48 +00:00
|
|
|
*
|
|
|
|
* @return
|
|
|
|
* The name of the included file, if successful; FALSE otherwise.
|
2013-01-21 19:21:34 +00:00
|
|
|
*
|
|
|
|
* @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.
|
2007-05-25 12:46:46 +00:00
|
|
|
*/
|
|
|
|
function module_load_include($type, $module, $name = NULL) {
|
2010-11-27 19:12:56 +00:00
|
|
|
if (!isset($name)) {
|
2007-05-25 12:46:46 +00:00
|
|
|
$name = $module;
|
|
|
|
}
|
|
|
|
|
2009-08-24 00:14:23 +00:00
|
|
|
if (function_exists('drupal_get_path')) {
|
2008-09-20 20:22:25 +00:00
|
|
|
$file = DRUPAL_ROOT . '/' . drupal_get_path('module', $module) . "/$name.$type";
|
|
|
|
if (is_file($file)) {
|
|
|
|
require_once $file;
|
|
|
|
return $file;
|
|
|
|
}
|
2007-05-25 12:46:46 +00:00
|
|
|
}
|
2008-09-20 20:22:25 +00:00
|
|
|
return FALSE;
|
2007-05-25 12:46:46 +00:00
|
|
|
}
|
|
|
|
|
2006-08-03 01:02:51 +00:00
|
|
|
|
|
|
|
/**
|
2010-06-26 12:34:44 +00:00
|
|
|
* Enables or installs a given list of modules.
|
2006-08-03 01:02:51 +00:00
|
|
|
*
|
2013-05-27 19:39:38 +00:00
|
|
|
* @deprecated as of Drupal 8.0. Use
|
|
|
|
* Drupal::moduleHandler()->enable($module_list, $enable_dependencies = TRUE).
|
2006-08-03 01:02:51 +00:00
|
|
|
*/
|
2010-03-02 09:07:09 +00:00
|
|
|
function module_enable($module_list, $enable_dependencies = TRUE) {
|
2013-05-27 19:39:38 +00:00
|
|
|
return Drupal::moduleHandler()->enable($module_list, $enable_dependencies);
|
2006-08-03 01:02:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-08-31 15:56:36 +00:00
|
|
|
* Disables a given set of modules.
|
2006-08-03 01:02:51 +00:00
|
|
|
*
|
2013-05-27 19:39:38 +00:00
|
|
|
* @deprecated as of Drupal 8.0. Use
|
|
|
|
* Drupal::moduleHandler()->disable($module_list, $disable_dependents = TRUE).
|
2006-08-03 01:02:51 +00:00
|
|
|
*/
|
2010-01-13 05:08:29 +00:00
|
|
|
function module_disable($module_list, $disable_dependents = TRUE) {
|
2013-05-27 19:39:38 +00:00
|
|
|
Drupal::moduleHandler()->disable($module_list, $disable_dependents);
|
2006-08-03 01:02:51 +00:00
|
|
|
}
|
|
|
|
|
2012-07-07 18:50:30 +00:00
|
|
|
/**
|
2013-05-03 15:58:40 +00:00
|
|
|
* Uninstalls a given list of disabled modules.
|
2012-07-07 18:50:30 +00:00
|
|
|
*
|
2013-05-27 19:39:38 +00:00
|
|
|
* @deprecated as of Drupal 8.0. Use
|
|
|
|
* Drupal::moduleHandler()->uninstall($module_list, $uninstall_dependents = TRUE).
|
2012-07-07 18:50:30 +00:00
|
|
|
*/
|
|
|
|
function module_uninstall($module_list = array(), $uninstall_dependents = TRUE) {
|
2013-05-27 19:39:38 +00:00
|
|
|
return Drupal::moduleHandler()->uninstall($module_list, $uninstall_dependents);
|
2012-07-07 18:50:30 +00:00
|
|
|
}
|
|
|
|
|
2004-07-14 20:42:20 +00:00
|
|
|
/**
|
|
|
|
* @defgroup hooks Hooks
|
2004-09-09 05:51:08 +00:00
|
|
|
* @{
|
|
|
|
* Allow modules to interact with the Drupal core.
|
2004-07-14 20:42:20 +00:00
|
|
|
*
|
|
|
|
* Drupal's module system is based on the concept of "hooks". A hook is a PHP
|
2009-11-08 09:31:10 +00:00
|
|
|
* function that is named foo_bar(), where "foo" is the name of the module
|
|
|
|
* (whose filename is thus foo.module) and "bar" is the name of the hook. Each
|
|
|
|
* hook has a defined set of parameters and a specified result type.
|
2004-07-14 20:42:20 +00:00
|
|
|
*
|
2009-11-08 09:31:10 +00:00
|
|
|
* To extend Drupal, a module need simply implement a hook. When Drupal wishes
|
|
|
|
* to allow intervention from modules, it determines which modules implement a
|
|
|
|
* hook and calls that hook in all enabled modules that implement it.
|
2004-07-14 20:42:20 +00:00
|
|
|
*
|
|
|
|
* The available hooks to implement are explained here in the Hooks section of
|
|
|
|
* the developer documentation. The string "hook" is used as a placeholder for
|
2009-11-08 09:31:10 +00:00
|
|
|
* the module name in the hook definitions. For example, if the module file is
|
|
|
|
* called example.module, then hook_help() as implemented by that module would
|
|
|
|
* be defined as example_help().
|
2010-04-20 18:50:36 +00:00
|
|
|
*
|
2010-04-21 07:53:45 +00:00
|
|
|
* The example functions included are not part of the Drupal core, they are
|
|
|
|
* just models that you can modify. Only the hooks implemented within modules
|
|
|
|
* are executed when running Drupal.
|
|
|
|
*
|
2013-04-04 22:49:28 +00:00
|
|
|
* @see themeable
|
|
|
|
* @see callbacks
|
2010-07-16 11:18:02 +00:00
|
|
|
*
|
2004-09-09 05:51:08 +00:00
|
|
|
* @} End of "defgroup hooks".
|
2004-07-14 20:42:20 +00:00
|
|
|
*/
|
|
|
|
|
2013-04-04 22:49:28 +00:00
|
|
|
/**
|
|
|
|
* @defgroup callbacks Callbacks
|
|
|
|
* @{
|
|
|
|
* Callback function signatures.
|
|
|
|
*
|
|
|
|
* Drupal's API sometimes uses callback functions to allow you to define how
|
|
|
|
* some type of processing happens. A callback is a function with a defined
|
|
|
|
* signature, which you define in a module. Then you pass the function name as
|
|
|
|
* a parameter to a Drupal API function or return it as part of a hook
|
|
|
|
* implementation return value, and your function is called at an appropriate
|
|
|
|
* time. For instance, when setting up batch processing you might need to
|
|
|
|
* provide a callback function for each processing step and/or a callback for
|
|
|
|
* when processing is finished; you would do that by defining these functions
|
|
|
|
* and passing their names into the batch setup function.
|
|
|
|
*
|
|
|
|
* Callback function signatures, like hook definitions, are described by
|
|
|
|
* creating and documenting dummy functions in a *.api.php file; normally, the
|
|
|
|
* dummy callback function's name should start with "callback_", and you should
|
|
|
|
* document the parameters and return value and provide a sample function body.
|
|
|
|
* Then your API documentation can refer to this callback function in its
|
|
|
|
* documentation. A user of your API can usually name their callback function
|
|
|
|
* anything they want, although a standard name would be to replace "callback_"
|
|
|
|
* with the module name.
|
|
|
|
*
|
|
|
|
* @see hooks
|
|
|
|
* @see themeable
|
|
|
|
*
|
2013-06-06 16:06:15 +00:00
|
|
|
* @}
|
2013-04-04 22:49:28 +00:00
|
|
|
*/
|
|
|
|
|
2007-02-04 21:20:50 +00:00
|
|
|
/**
|
2012-08-31 15:56:36 +00:00
|
|
|
* Returns an array of modules required by core.
|
2007-02-04 21:20:50 +00:00
|
|
|
*/
|
|
|
|
function drupal_required_modules() {
|
2013-03-06 22:51:39 +00:00
|
|
|
$files = drupal_system_listing('/^' . DRUPAL_PHP_FUNCTION_PATTERN . '\.info.yml$/', 'modules');
|
2008-10-12 01:23:07 +00:00
|
|
|
$required = array();
|
2009-08-21 07:50:08 +00:00
|
|
|
|
2012-10-05 16:11:15 +00:00
|
|
|
// An installation profile is required and one must always be loaded.
|
2009-08-21 07:50:08 +00:00
|
|
|
$required[] = drupal_get_profile();
|
|
|
|
|
2008-10-12 01:23:07 +00:00
|
|
|
foreach ($files as $name => $file) {
|
2009-08-17 19:14:42 +00:00
|
|
|
$info = drupal_parse_info_file($file->uri);
|
2008-10-12 01:23:07 +00:00
|
|
|
if (!empty($info) && !empty($info['required']) && $info['required']) {
|
|
|
|
$required[] = $name;
|
|
|
|
}
|
|
|
|
}
|
2009-08-21 07:50:08 +00:00
|
|
|
|
2008-10-12 01:23:07 +00:00
|
|
|
return $required;
|
2007-02-04 21:20:50 +00:00
|
|
|
}
|
2010-04-22 22:36:01 +00:00
|
|
|
|
2012-10-09 20:32:40 +00:00
|
|
|
/**
|
|
|
|
* 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) {
|
|
|
|
// Update the module weight in the config file that contains it.
|
|
|
|
$module_config = config('system.module');
|
|
|
|
if ($module_config->get("enabled.$module") !== NULL) {
|
|
|
|
$module_config
|
|
|
|
->set("enabled.$module", $weight)
|
|
|
|
->set('enabled', module_config_sort($module_config->get('enabled')))
|
|
|
|
->save();
|
2013-01-21 19:21:34 +00:00
|
|
|
|
|
|
|
// Prepare the new module list, sorted by weight, including filenames.
|
|
|
|
// @see module_enable()
|
2013-06-05 01:58:17 +00:00
|
|
|
$module_handler = Drupal::moduleHandler();
|
2013-01-21 19:21:34 +00:00
|
|
|
$current_module_filenames = $module_handler->getModuleList();
|
|
|
|
$current_modules = array_fill_keys(array_keys($current_module_filenames), 0);
|
|
|
|
$current_modules = module_config_sort(array_merge($current_modules, $module_config->get('enabled')));
|
|
|
|
$module_filenames = array();
|
|
|
|
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);
|
2012-10-09 20:32:40 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
$disabled_config = config('system.module.disabled');
|
|
|
|
if ($disabled_config->get($module) !== NULL) {
|
|
|
|
$disabled_config
|
|
|
|
->set($module, $weight)
|
|
|
|
->save();
|
|
|
|
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
|
|
|
|
$sort = array();
|
|
|
|
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;
|
|
|
|
}
|