| 1 |
<?php
|
| 2 |
|
| 3 |
|
| 4 |
/**
|
| 5 |
* Implementation of hook_help().
|
| 6 |
*/
|
| 7 |
function module_weights_help($path, $arg) {
|
| 8 |
// drupal_set_message('<pre>'. print_r($path, 1) .'</pre>');
|
| 9 |
// drupal_set_message('<pre>'. print_r($arg, 1) .'</pre>');
|
| 10 |
$output = '';
|
| 11 |
switch ($path) {
|
| 12 |
case 'admin/help#module_weights':
|
| 13 |
$output .= '<p>'. t('Drupal assigns each module a weight. For most operations involving any module that defines a particular hook, the modules are invoked in order first by weight, then by name.') .'</p>';
|
| 14 |
$output .= '<p>'. t('This module adds a weight column to the modules table at !modules, allowing weights to be viewed and edited. Once activated, a weight column appears on the modules table. To change a module weight, edit its value and press "Save configuration". Any user who can submit the !modules form will be able to change module weights.', array('!modules' => l('admin/build/modules', 'admin/build/modules'))) .'</p>';
|
| 15 |
break;
|
| 16 |
}
|
| 17 |
return $output;
|
| 18 |
}
|
| 19 |
|
| 20 |
function module_weights_system_module_headers_alter(&$header) {
|
| 21 |
//add weight header
|
| 22 |
array_unshift($header, 'Weight');
|
| 23 |
}
|
| 24 |
function module_weights_system_module_weights_alter(&$row, $module, &$form) {
|
| 25 |
array_unshift($row, drupal_render($form['weights'][$module]));
|
| 26 |
//CLEANUP what we added in hook_form_alter()
|
| 27 |
unset($form['weights'][$module]);
|
| 28 |
}
|
| 29 |
|
| 30 |
/**
|
| 31 |
* Helper function to fetch and cache module weights
|
| 32 |
*
|
| 33 |
*/
|
| 34 |
function fetch_module_weights($name = NULL) {
|
| 35 |
static $module_weights = array();
|
| 36 |
if (empty($module_weights)) {
|
| 37 |
$query = "SELECT filename, name, type, owner, status, throttle, bootstrap, schema_version, weight FROM {system} WHERE type = 'module' AND status = 1 ORDER BY name";
|
| 38 |
$result = db_query($query);
|
| 39 |
while( $row = db_fetch_object($result)) {
|
| 40 |
$module_weights[$row->name] = $row->weight;
|
| 41 |
}
|
| 42 |
}
|
| 43 |
if ($name === NULL) {
|
| 44 |
return $module_weights;
|
| 45 |
}
|
| 46 |
else if (isset($module_weights[$name])) {
|
| 47 |
return $module_weights[$name];
|
| 48 |
}
|
| 49 |
else {
|
| 50 |
return NULL;
|
| 51 |
}
|
| 52 |
}
|
| 53 |
|
| 54 |
function module_weights_form_alter(&$form, $form_state, $form_id) {
|
| 55 |
switch ($form_id) {
|
| 56 |
case 'system_modules':
|
| 57 |
//do not alter system module form when in 'confirm' state
|
| 58 |
if (isset($form['confirm'])) {
|
| 59 |
return ;
|
| 60 |
}
|
| 61 |
$weights = fetch_module_weights();
|
| 62 |
$form['weights'] = array(
|
| 63 |
'#tree' => TRUE,
|
| 64 |
);
|
| 65 |
foreach ($weights as $name => $weight) {
|
| 66 |
$form['weights'][$name] = array(
|
| 67 |
'#type' => 'textfield',
|
| 68 |
'#default_value' => $weight,
|
| 69 |
'#size' => 4,
|
| 70 |
);
|
| 71 |
}
|
| 72 |
// Do my #submit before system.module's so all the rebuilding
|
| 73 |
// operations in system_module_submit use the new weights.
|
| 74 |
array_unshift($form['#submit'],'module_weights_system_module_submit');
|
| 75 |
$form['#validate'][] = 'module_weights_system_module_validate';
|
| 76 |
break;
|
| 77 |
}
|
| 78 |
}
|
| 79 |
function module_weights_system_module_validate($form, &$form_state) {
|
| 80 |
$weights = fetch_module_weights();
|
| 81 |
foreach ($weights as $name => $weight) {
|
| 82 |
//submitted weights must be numeric
|
| 83 |
if (!is_numeric($form_state['values']['weights'][$name])) {
|
| 84 |
form_set_error("weights][{$name}", t(
|
| 85 |
'The !module module weight must be a number.',
|
| 86 |
array('!module' => $name)
|
| 87 |
));
|
| 88 |
}
|
| 89 |
}
|
| 90 |
}
|
| 91 |
|
| 92 |
function module_weights_system_module_submit($form, &$form_state) {
|
| 93 |
foreach ($form_state['values']['weights'] as $name => $weight) {
|
| 94 |
//extra step of optimization, update only weights that changed
|
| 95 |
//also skip on module names that dont match our record in fetch_module_weights()
|
| 96 |
$module_weight = fetch_module_weights($name);
|
| 97 |
if ($module_weight !== NULL && $module_weight != $weight) {
|
| 98 |
$query = "UPDATE {system} SET weight = %d WHERE name LIKE '%s'";
|
| 99 |
db_query($query, $weight, $name);
|
| 100 |
}
|
| 101 |
}
|
| 102 |
}
|