Commit fbca3630 authored by Dries's avatar Dries

- Patch #211439 by David_Rothstein, cwgordon7: correct sorting of modules.

parent 8c3efea3
......@@ -34,8 +34,8 @@ function module_load_all() {
* Whether to force the module list to be regenerated (such as after the
* administrator has changed the system settings).
* @param $sort
* By default, modules are ordered by weight and filename. Set this option to
* TRUE to return a module list ordered only by module name.
* By default, modules are ordered by weight and module name. Set this option
* to TRUE to return a module list ordered only by module name.
* @param $fixed_list
* (Optional) Override the module list with the given modules. Stays until the
* next call with $refresh = TRUE.
......@@ -56,7 +56,12 @@ function module_list($refresh = FALSE, $sort = FALSE, $fixed_list = NULL) {
}
}
else {
$result = db_query("SELECT name, filename FROM {system} WHERE type = 'module' AND status = 1 ORDER BY weight ASC, filename ASC");
// The module name (rather than the filename) is used as the fallback
// weighting in order to guarantee consistent behavior across different
// Drupal installations, which might have modules installed in different
// locations in the file system. The ordering here must also be
// consistent with the one used in module_implements().
$result = db_query("SELECT name, filename FROM {system} WHERE type = 'module' AND status = 1 ORDER BY weight ASC, name ASC");
foreach ($result as $module) {
if (file_exists($module->filename)) {
drupal_get_filename('module', $module->name, $module->filename);
......@@ -315,7 +320,7 @@ function module_hook($module, $hook) {
* MODULE_IMPLEMENTS_WRITE_CACHE: Write the stored list of hook
* implementations into the cache_registry table.
* @param $sort
* By default, modules are ordered by weight and filename. By setting this
* By default, modules are ordered by weight and module name. By setting this
* option to TRUE, modules will be ordered by module name.
* @return
* An array with the names of the modules which are implementing this hook.
......@@ -350,6 +355,11 @@ function module_implements($hook, $sort = FALSE) {
$cached_hooks = count($implementations);
}
if (!isset($implementations[$hook])) {
// The module name (rather than the filename) is used as the fallback
// weighting in order to guarantee consistent behavior across different
// Drupal installations, which might have modules installed in different
// locations in the file system. The ordering here must also be
// consistent with the one used in module_list().
$implementations[$hook] = db_query("SELECT module FROM {registry} WHERE type = 'function' AND suffix = :hook ORDER BY weight, module", array(':hook' => $hook))->fetchCol();
}
foreach ($implementations[$hook] as $module) {
......
......@@ -22,37 +22,32 @@ class ModuleUnitTest extends DrupalWebTestCase {
* The basic functionality of module_list().
*/
function testModuleList() {
// Build a list of modules filenames.
$base_module_list = array();
foreach (drupal_get_profile_modules('default', 'en') as $module) {
$base_module_list[$module] = drupal_get_path('module', $module);
}
asort($base_module_list);
// Build a list of module names based on that order. Since all default
// profile modules have a weight equal to 0, the default sort order is
// simply alphabetical.
$module_list = array_keys($base_module_list);
// Build a list of modules, sorted alphabetically.
$module_list = drupal_get_profile_modules('default', 'en');
sort($module_list);
// Compare this list to the one returned by module_list(). We expect them
// to match, since all default profile modules have a weight equal to 0
// (except for block.module, which has a lower weight but comes first in
// the alphabet anyway).
$this->assertModuleList($module_list, t('Default profile'));
// Try to install a new module.
drupal_install_modules(array('path'));
$base_module_list['path'] = drupal_get_path('module', 'path');
asort($base_module_list);
$module_list = array_keys($base_module_list);
drupal_install_modules(array('contact'));
$module_list[] = 'contact';
sort($module_list);
$this->assertModuleList($module_list, t('After adding a module'));
// Try to mess with the module weights.
db_update('system')
->fields(array('weight' => 20))
->condition('name', 'path')
->condition('name', 'contact')
->condition('type', 'module')
->execute();
// Reset the module list.
module_list(TRUE);
// Move path at the end of the array.
unset($base_module_list['path']);
$base_module_list['path'] = drupal_get_path('module', 'path');
$module_list = array_keys($base_module_list);
// Move contact to the end of the array.
unset($module_list[array_search('contact', $module_list)]);
$module_list[] = 'contact';
$this->assertModuleList($module_list, t('After changing weights'));
// Test the fixed list feature.
......@@ -73,7 +68,7 @@ class ModuleUnitTest extends DrupalWebTestCase {
* Assert that module_list() return the expected values.
*
* @param $expected_values
* The expected values, sorted by weight and file name.
* The expected values, sorted by weight and module name.
*/
protected function assertModuleList(Array $expected_values, $condition) {
$expected_values = array_combine($expected_values, $expected_values);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment