You are here

public function ModulesListForm::submitForm in Modules weight 8

Same name and namespace in other branches
  1. 8.2 src/Form/ModulesListForm.php \Drupal\modules_weight\Form\ModulesListForm::submitForm()

Form submission handler.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Overrides FormInterface::submitForm

File

src/Form/ModulesListForm.php, line 138

Class

ModulesListForm
Builds the form to configure the Modules weight.

Namespace

Drupal\modules_weight\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {

  // The modules information.
  $modules_info = $form_state
    ->getValue('modules');

  // Doing the array unidimensional.
  $new_weight_value = array_combine(array_keys($modules_info), array_column($modules_info, 'weight'));

  // Getting the config to know if we should show or not the core modules.
  $show_core_modules = $this->configFactory
    ->get('modules_weight.settings')
    ->get('show_system_modules');

  // The old values information.
  $old_modules_info = $this->modulesWeight
    ->getModulesList($show_core_modules);

  // Doing the array unidimensional.
  $old_weight_value = array_combine(array_keys($old_modules_info), array_column($old_modules_info, 'weight'));
  if ($new_weight_value == $old_weight_value) {

    // Printing message if there are not module that has changed.
    $this
      ->messenger()
      ->addWarning($this
      ->t("You don't have changed the weight for any module."));
  }
  else {

    // Getting the changed modules.
    $changed = array_diff_assoc($new_weight_value, $old_weight_value);

    // Printing message because we have changes in the weights.
    $this
      ->messenger()
      ->addMessage($this
      ->t('The modules weight was updated.'));

    // Updating weights.
    foreach ($changed as $module_name => $weight) {

      // Setting the new weight.
      module_set_weight($module_name, $weight);
      $variables = [
        '@module_name' => $this->moduleExtensionList
          ->getExtensionInfo($module_name)['name'],
        '@weight' => $weight,
      ];

      // Printing information about the modules weight.
      $this
        ->messenger()
        ->addMessage($this
        ->t('@module_name have now as weight: @weight', $variables));
    }
  }
}