modules_weight.helpers.inc in Modules weight 7
Helper functions.
File
modules_weight.helpers.incView source
<?php
/**
* @file
* Helper functions.
*/
/**
* Prepares the delta for the weight field on the administration form.
*
* If a module has a weight higher then 100 (or lower than 100), it will use
* that value as delta and the '#weight' field will turn into a textfield most
* likely.
*
* @param int $weight
* The weight.
*
* @return int
* The weight.
*/
function _modules_weight_prepare_delta($weight) {
$delta = 100;
// Typecasting to int.
$weight = (int) $weight;
if ($weight > $delta) {
return $weight;
}
if ($weight < -100) {
return $weight * -1;
}
return $delta;
}
/**
* Return the modules list.
*
* @param bool $show_core_modules
* Force to show the core modules.
*
* @return array
* The modules list.
*/
function _modules_weight_modules_list($show_core_modules = FALSE) {
$modules = [];
// Get current list of modules.
$installed_modules = system_get_info('module');
// Getting the modules weight.
$modules_weight = _modules_weight_get_modules_weight();
// Iterate through each of the modules.
foreach ($installed_modules as $module_name => $module_info) {
// We don't want to show the hidden modules, or the Core modules
// (if the configuration is set to FALSE).
if (!isset($module_info['hidden']) && ($show_core_modules || $module_info['package'] != 'Core')) {
$modules[$module_name]['name'] = $module_info['name'];
$modules[$module_name]['description'] = $module_info['description'];
$modules[$module_name]['weight'] = $modules_weight[$module_name];
$modules[$module_name]['package'] = $module_info['package'];
}
}
// Sorting all modules by their weight.
uasort($modules, 'sort_by_weight_and_name');
return $modules;
}
/**
* Sets the weight of a particular module.
*
* @param string $module
* The name of the module (without the .module extension).
* @param int $weight
* An integer representing the weight of the module.
*/
function _modules_weight_module_set_weight($module, $weight) {
// The query.
$query = "UPDATE {system}\n SET weight = :weight\n WHERE type = 'module'\n AND name = :name";
// Executing the query.
db_query($query, array(
':weight' => $weight,
':name' => $module,
));
}
/**
* Gets the weight of a particular module.
*
* @param string $module
* The name of the module (without the .module extension).
*
* @return int
* An integer representing the weight of the module.
*/
function _modules_weight_module_get_weight($module) {
// The query.
$query = "SELECT weight\n FROM {system}\n WHERE type = 'module'\n AND name = :name";
// Executing the query and getting the module weight.
$weight = db_query($query, array(
':name' => $module,
))
->fetchObject()->weight;
return $weight;
}
/**
* Return the modules weight.
*
* @return array
* An array with the modules weight keyed by module machine name.
*/
function _modules_weight_get_modules_weight() {
// The query.
$query = "SELECT name,\n weight\n FROM {system}\n WHERE type = 'module'\n AND status = 1";
// Executing the query and getting the modules weight.
$weight = db_query($query)
->fetchAllKeyed();
return $weight;
}
/**
* Sorts a structured array by the 'weight' and 'name element.
*
* Note that the sorting is by the 'weight' array element, not by the render
* element property '#weight'.
*
* Callback for uasort().
*
* @param array $a
* First item for comparison. The compared items should be associative
* arrays that optionally include a 'weight' element. For items without a
* 'weight' element, a default value of 0 will be used.
* @param array $b
* Second item for comparison.
*
* @return int
* The comparison result for uasort().
*/
function sort_by_weight_and_name(array $a, array $b) {
$a_weight = is_array($a) && isset($a['weight']) ? $a['weight'] : 0;
$b_weight = is_array($b) && isset($b['weight']) ? $b['weight'] : 0;
if ($a_weight == $b_weight) {
return $a['name'] < $b['name'] ? -1 : 1;
}
return $a_weight < $b_weight ? -1 : 1;
}
Functions
Name | Description |
---|---|
sort_by_weight_and_name | Sorts a structured array by the 'weight' and 'name element. |
_modules_weight_get_modules_weight | Return the modules weight. |
_modules_weight_modules_list | Return the modules list. |
_modules_weight_module_get_weight | Gets the weight of a particular module. |
_modules_weight_module_set_weight | Sets the weight of a particular module. |
_modules_weight_prepare_delta | Prepares the delta for the weight field on the administration form. |