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'] = [
'description' => dt('Configures if we can reorder the core modules.'),
'arguments' => [
'status' => dt('The status option (on, off).'),
],
'examples' => [
'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."),
],
'aliases' => [
'mw-ssm',
],
];
$items['mw-reorder'] = [
'description' => dt('Configures the modules weight.'),
'arguments' => [
'module' => dt('The module machine name.'),
'weight' => dt('The module weight.'),
],
'options' => [
'minus' => [
'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 .'),
],
],
'examples' => [
'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."),
],
'aliases' => [
'mw-r',
],
];
$field_labels = [
'name' => dt('Name'),
'machine_name' => dt('Machine Name'),
'weight' => dt('Weight'),
'package' => dt('Package'),
];
$items['mw-list'] = [
'description' => dt('Shows the modules weight list.'),
'options' => [
'force' => [
'description' => dt('If the option is present the core modules will be shown even if the option to allow it is disabled.'),
],
],
'outputformat' => [
'default' => 'table',
'pipe-format' => 'list',
'field-labels' => $field_labels,
'output-data-type' => 'format-table',
],
'examples' => [
'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.'),
],
'aliases' => [
'mw-l',
],
];
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 = [
'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();
$config = \Drupal::service('config.factory')
->getEditable('modules_weight.settings');
$show_system_modules = $config
->get('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' ? [
1,
strtolower($activated),
] : [
0,
strtolower($disabled),
];
if ($show_system_modules == $value) {
$message = dt('The core modules reorder option is already @status.', [
'@status' => $status,
]);
drush_log($message, 'warning');
return;
}
$config
->set('show_system_modules', $value);
$config
->save();
$message = dt('You have @status the core modules reorder option.', [
'@status' => $status,
]);
drush_log($message, 'success');
}
else {
$status = $show_system_modules ? $activated : $disabled;
$message = dt('The core modules reorder option is: @status', [
'@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 (!\Drupal::service('module_handler')
->moduleExists($args[0])) {
return drush_set_error('MODULES_WEIGHT_INVALID_MODULE_NAME', dt('@module_name module machine name is invalid or is not installed.', [
'@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 = \Drupal::service('module_handler')
->getModule($args[0]);
$module = \Drupal::service('info_parser')
->parse($module
->getPathname());
$show_system_modules = \Drupal::service('config.factory')
->get('modules_weight.settings')
->get('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();
if (count($args) == 2) {
$minus = drush_get_option('minus');
$weight = $minus ? -1 * $args[1] : $args[1];
module_set_weight($args[0], $weight);
$message = dt('The module weight for @module_name was updated to @weight.', [
'@module_name' => $args[0],
'@weight' => $weight,
]);
drush_log($message, 'success');
}
else {
$installed_modules = \Drupal::service('config.factory')
->get('core.extension')
->get('module') ?: [];
$weight = $installed_modules[$args[0]];
$module_name = \Drupal::service('extension.list.module')
->getName($args[0]);
$values = [
'@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 = [];
$force = (bool) drush_get_option('force');
$show_core_modules = $force ?: \Drupal::service('config.factory')
->get('modules_weight.settings')
->get('show_system_modules');
$modules = \Drupal::service('modules_weight')
->getModulesList($show_core_modules);
foreach ($modules as $filename => $module) {
$row = [];
$row['name'] = $module['name'];
$row['machine_name'] = $filename;
$row['weight'] = $module['weight'];
$row['package'] = $module['package'];
$result[] = $row;
}
return $result;
}