View source
<?php
define('MODULES_WEIGHT_GREEN_OUTPUT', "\33[1;32;40m\33[1m%s\33[0m");
define('MODULES_WEIGHT_RED_OUTPUT', "\33[31;40m\33[1m%s\33[0m");
function modules_weight_drush_command() {
$items['mw-show-system-modules'] = array(
'description' => dt('Configures if we can reorder the core modules.'),
'arguments' => array(
'status' => dt('The status option (on, off).'),
),
'aliases' => array(
'mw-ssm',
),
'examples' => array(
'mw-show-system-modules' => dt('Shows if we can reorder the core modules or not.'),
'mw-show-system-modules on' => dt('Allows reorder the core modules.'),
'mw-show-system-modules off' => dt("Don't allows reorder the core modules."),
),
);
$items['mw-reorder'] = array(
'description' => dt('Configures the modules weight.'),
'arguments' => array(
'module' => dt('The module machine name.'),
'weight' => dt('The module weight.'),
),
'options' => array(
'minus' => array(
'description' => dt('If the option is present the weight will be consider as a negative value. Read for more information https://drupal.stackexchange.com/q/246298/28275 .'),
),
),
'aliases' => array(
'mw-r',
),
'examples' => array(
'mw-reorder node_revision_delete' => dt('Show the node_revision_delete module weight.'),
'mw-reorder onlyone -5 --minus' => dt('Set the onlyone module weight to -5.'),
'mw-reorder no_autocomplete 15' => dt("Set the no_autocomplete module weight to 15."),
),
);
$field_labels = array(
'name' => dt('Name'),
'machine_name' => dt('Machine Name'),
'weight' => dt('Weight'),
'package' => dt('Package'),
);
$items['mw-list'] = array(
'description' => dt('Shows the modules weight list.'),
'options' => array(
'force' => array(
'description' => dt('If the option is present the core modules will be shown even if the option to allow it is disabled.'),
),
),
'outputformat' => array(
'default' => 'table',
'pipe-format' => 'list',
'field-labels' => $field_labels,
'output-data-type' => 'format-table',
),
'aliases' => array(
'mw-l',
),
'examples' => array(
'mw-list' => dt('Shows the modules weight list.'),
'mw-list --force' => dt('Shows the modules weight list with the core modules even if the option to allow it is disabled.'),
),
);
return $items;
}
function modules_weight_drush_help($section) {
switch ($section) {
case 'meta:modules_weight:title':
return dt("Modules weight commands");
case 'meta:modules_weight:summary':
return dt("Interacts with the Modules weight module's functionalities.");
case 'drush:mw-show-system-modules':
return dt('Configures if we can reorder the core modules.');
case 'drush:mw-reorder':
return dt('Configures the module weight.');
case 'drush:mw-list':
return dt('Shows the modules weight list.');
}
}
function drush_modules_weight_mw_show_system_modules_validate() {
$args = func_get_args();
if (count($args) > 1) {
return drush_set_error('MODULES_WEIGHT_INVALID_ARGUMENT', dt('This command use only one argument.'));
}
$available_options = array(
'on',
'off',
);
if (isset($args[0]) && !in_array($args[0], $available_options)) {
return drush_set_error('MODULES_WEIGHT_INVALID_ARGUMENT', dt("You must specify as argument 'on' or 'off'"));
}
}
function drush_modules_weight_mw_show_system_modules() {
$args = func_get_args();
$show_system_modules = variable_get('modules_weight_show_system_modules');
$activated = sprintf(MODULES_WEIGHT_GREEN_OUTPUT, dt('Activated'));
$disabled = sprintf(MODULES_WEIGHT_RED_OUTPUT, dt('Disabled'));
if (isset($args[0])) {
list($value, $status) = $args[0] == 'on' ? array(
1,
strtolower($activated),
) : array(
0,
strtolower($disabled),
);
if ($show_system_modules == $value) {
$message = dt('The core modules reorder option is already @status.', array(
'@status' => $status,
));
drush_log($message, 'warning');
return;
}
variable_set('modules_weight_show_system_modules', $value);
$message = dt('You have @status the core modules reorder option.', array(
'@status' => $status,
));
drush_log($message, 'success');
}
else {
$status = $show_system_modules ? $activated : $disabled;
$message = dt('The core modules reorder option is: @status', array(
'@status' => $status,
));
drush_print($message);
}
}
function drush_modules_weight_mw_reorder_validate() {
$args = func_get_args();
if (count($args) < 1 || count($args) > 2) {
return drush_set_error('MODULES_WEIGHT_INVALID_ARGUMENT', dt('This command use one or two arguments.'));
}
if (!module_exists($args[0])) {
return drush_set_error('MODULES_WEIGHT_INVALID_MODULE_NAME', dt('@module_name module machine name is invalid or is not installed.', array(
'@module_name' => $args[0],
)));
}
if (isset($args[1]) && !ctype_digit($args[1])) {
return drush_set_error('MODULES_WEIGHT_INVALID_ARGUMENT', dt('You must enter digits for the modules-weight.'));
}
$force = drush_get_option('force');
$module = system_get_info('module', $args[0]);
$show_system_modules = variable_get('modules_weight_show_system_modules');
if (!$force && $module['package'] == 'Core' && !$show_system_modules) {
if (!drush_confirm(dt("You're trying to reorder a Core module but Modules Weight is not configured to allow it. Do you want to continue?"))) {
return drush_user_abort();
}
}
}
function drush_modules_weight_mw_reorder() {
$args = func_get_args();
module_load_include('inc', 'modules_weight', 'modules_weight.helpers');
if (count($args) == 2) {
$minus = drush_get_option('minus');
$weight = $minus ? -1 * $args[1] : $args[1];
_modules_weight_module_set_weight($args[0], $weight);
$message = dt('The module weight for @module_name was updated to @weight.', array(
'@module_name' => $args[0],
'@weight' => $weight,
));
drush_log($message, 'success');
}
else {
$weight = _modules_weight_module_get_weight($args[0]);
$module = system_get_info('module', $args[0]);
$values = array(
'@module_name' => $module['name'],
'@machine_name' => $args[0],
'@weight' => $weight,
);
$message = dt('The weight of the @module_name [@machine_name] module is: @weight', $values);
drush_print($message);
}
}
function drush_modules_weight_mw_list() {
$result = array();
module_load_include('inc', 'modules_weight', 'modules_weight.helpers');
$force = (bool) drush_get_option('force');
$show_core_modules = $force ?: variable_get('modules_weight_show_system_modules');
$modules = _modules_weight_modules_list($show_core_modules);
foreach ($modules as $filename => $module) {
$row = array();
$row['name'] = $module['name'];
$row['machine_name'] = $filename;
$row['weight'] = $module['weight'];
$row['package'] = $module['package'];
$result[] = $row;
}
return $result;
}