Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
109 lines (94 sloc) 3.21 KB
<?php
/**
* Implementation of hook_menu().
*/
function module_order_menu() {
// This is the minimum information you can provide for a menu item.
$items['admin/settings/module_order'] = array(
'title' => 'Module Order',
'page callback' => 'drupal_get_form',
'page arguments' => array('module_order_admin'),
'access arguments' => array('administer module order'),
'description' => 'Rebuild module weights.',
);
return $items;
}
function module_order_form_alter(&$form, $form_state, $form_id) {
switch ($form_id) {
case 'system_modules':
$form['#submit'][] = 'module_order_rebuild_weights';
break;
}
}
function module_order_admin(&$form_state) {
$form = array();
$form['rebuild_order'] = array(
'#type' => 'submit',
'#value' => t('Rebuild Module Weights'),
);
$results = db_query('SELECT name, weight FROM system ORDER BY weight DESC, name');
while ($module = db_fetch_array($results)) {
$modules[] = $module;
}
$form['rebuild_order']['#suffix'] = theme('table', array('Module', 'Weight'), $modules);
return $form;
}
function module_order_admin_submit($form, &$form_state) {
$values = $form_state['values'];
$op = $form_state['clicked_button']['#value'];
if ($values['op'] == t('Rebuild Module Weights')) {
module_order_rebuild_weights();
}
}
function module_order_rebuild_weights() {
module_load_include('inc', 'module_order', 'module_order.graph');
// get module info from .info files
$modules = module_order_get_module_info();
$graph = array();
foreach ($modules as $filename => $file) {
// get the modules that should come before the current module
if (isset($file->info['before'])) {
foreach ((array)$file->info['before'] as $dependency) {
$graph[$file->name]['edges'][$dependency] = 1;
$graph[$dependency]['edges'] = array(); # if this key doesn't exist, it won't get a weight
}
}
// get the modules that should come after the current module
if (isset($file->info['after'])) {
foreach ((array)$file->info['after'] as $dependency) {
$graph[$dependency]['edges'][$file->name] = 1;
}
}
}
module_order_depth_first_search($graph);
// update module weights in the system table with weights returned by the sort function
foreach($graph as $module => $info) {
db_query("UPDATE {system} SET weight = %d WHERE name = '%s'", $info['weight'], $module);
}
// TODO: Make this conditional
drupal_set_message("Module weights rebuilt!");
}
function module_order_get_module_info() {
// Get current list of modules
$files = drupal_system_listing('\.module$', 'modules', 'name', 0);
// Extract current files from database.
system_get_files_database($files, 'module');
ksort($files);
// Set defaults for module info
$defaults = array(
'before' => '',
'after' => '',
);
foreach ($files as $filename => $file) {
// Look for the info file.
$file->info = drupal_parse_info_file(dirname($file->filename) .'/'. $file->name .'.info');
// Skip modules that don't provide info.
if (empty($file->info)) {
unset($files[$filename]);
continue;
}
// Merge in defaults and save.
$files[$filename]->info = $file->info + $defaults;
}
return $files;
}