View source
<?php
namespace Drupal\modules_weight\Form;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Form\FormBase;
use Drupal\modules_weight\Utility\FormElement;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\modules_weight\ModulesWeightInterface;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Extension\ModuleExtensionList;
class ModulesListForm extends FormBase {
protected $modulesWeight;
protected $configFactory;
protected $moduleExtensionList;
public function __construct(ModulesWeightInterface $modules_weight, ConfigFactoryInterface $config_factory, ModuleExtensionList $module_extension_list) {
$this->modulesWeight = $modules_weight;
$this->configFactory = $config_factory;
$this->moduleExtensionList = $module_extension_list;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('modules_weight'), $container
->get('config.factory'), $container
->get('extension.list.module'));
}
public function getFormId() {
return 'modules_weight_modules_list';
}
public function buildForm(array $form, FormStateInterface $form_state) {
$header = [
$this
->t('Name'),
[
'data' => $this
->t('Description'),
'class' => [
RESPONSIVE_PRIORITY_MEDIUM,
],
],
$this
->t('Weight'),
[
'data' => $this
->t('Package'),
'class' => [
RESPONSIVE_PRIORITY_MEDIUM,
],
],
];
$form['modules'] = [
'#type' => 'table',
'#header' => $header,
'#sticky' => TRUE,
];
$show_core_modules = $this->configFactory
->get('modules_weight.settings')
->get('show_system_modules');
$modules = $this->modulesWeight
->getModulesList($show_core_modules);
foreach ($modules as $filename => $module) {
$form['modules'][$filename]['name'] = [
'#markup' => $module['name'],
];
$form['modules'][$filename]['description'] = [
'#markup' => $module['description'],
];
$form['modules'][$filename]['weight'] = [
'#type' => 'weight',
'#default_value' => $module['weight'],
'#delta' => FormElement::getMaxDelta($module['weight']),
];
$form['modules'][$filename]['package'] = [
'#markup' => $module['package'],
];
}
$form['submit'] = [
'#type' => 'submit',
'#value' => $this
->t('Save Changes'),
];
return $form;
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$modules_info = $form_state
->getValue('modules');
$new_weight_value = array_combine(array_keys($modules_info), array_column($modules_info, 'weight'));
$show_core_modules = $this->configFactory
->get('modules_weight.settings')
->get('show_system_modules');
$old_modules_info = $this->modulesWeight
->getModulesList($show_core_modules);
$old_weight_value = array_combine(array_keys($old_modules_info), array_column($old_modules_info, 'weight'));
if ($new_weight_value == $old_weight_value) {
$this
->messenger()
->addWarning($this
->t("You don't have changed the weight for any module."));
}
else {
$changed = array_diff_assoc($new_weight_value, $old_weight_value);
$this
->messenger()
->addMessage($this
->t('The modules weight was updated.'));
foreach ($changed as $module_name => $weight) {
module_set_weight($module_name, $weight);
$variables = [
'@module_name' => $this->moduleExtensionList
->getExtensionInfo($module)['name'],
'@weight' => $values['weight'],
];
$this
->messenger()
->addMessage($this
->t('@module_name have now as weight: @weight', $variables));
}
}
}
}