You are here

easy_install.module in Easy Install 8.9

Easy uninstall module file.

File

easy_install.module
View source
<?php

/**
 * @file
 * Easy uninstall module file.
 */
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Drupal\Core\Config\InstallStorage;

/**
 * Implements hook_form_alter().
 */
function easy_install_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  $form_ids = [
    'system_modules_uninstall_confirm_form',
  ];
  if (in_array($form_id, $form_ids)) {
    $modules = \Drupal::service('keyvalue.expirable')
      ->get('modules_uninstall')
      ->get(\Drupal::currentUser()
      ->id());
    foreach ($modules as $module) {
      $install_dir = drupal_get_path('module', $module) . '/' . InstallStorage::CONFIG_INSTALL_DIRECTORY;
      $optional_dir = drupal_get_path('module', $module) . '/' . InstallStorage::CONFIG_OPTIONAL_DIRECTORY;
      $install_details = file_scan_directory($install_dir, "/\\.(yml)\$/");
      if (!empty($install_details)) {
        $form['modules_config'][$module] = [
          '#type' => 'details',
          '#title' => t('@name', [
            '@name' => $module,
          ]),
          '#description' => t('We found that @description module have configurations with it, if you like to delete it Please select the checkbox', [
            '@description' => $module,
          ]),
          '#weight' => 0,
          '#validated' => TRUE,
          '#open' => TRUE,
        ];
        $install_details = file_scan_directory($install_dir, "/\\.(yml)\$/");
        $ins_options = [];
        foreach ($install_details as $config_value) {
          $ins_options[$config_value->name] = $config_value->name;
        }
        if (!empty($ins_options)) {
          $form['modules_config'][$module]['configs'] = [
            '#type' => 'checkboxes',
            '#label' => $config_value->name,
            '#title' => t('Select the configurations to be deleted'),
            '#options' => $ins_options,
            '#validated' => TRUE,
          ];
        }
        $optional_details = file_scan_directory($optional_dir, "/\\.(yml)\$/");
        $opt_options = [];
        foreach ($optional_details as $config_value) {
          $opt_options[$config_value->name] = $config_value->name;
        }
        if (!empty($opt_options)) {
          $form['modules_config'][$module]['opt_details'] = [
            '#type' => 'details',
            '#title' => "Optional Configurations",
            '#weight' => 0,
            '#validated' => TRUE,
            '#open' => TRUE,
          ];
          $form['modules_config'][$module]['opt_details']['opt_configs'] = [
            '#type' => 'checkboxes',
            '#label' => $config_value->name,
            '#options' => $opt_options,
            '#validated' => TRUE,
          ];
        }
      }
    }
    $label = 'Delete all the listed configurations except optional';
    if (empty($opt_options)) {
      $label = 'Delete all the listed configurations';
    }
    if (!empty($ins_options)) {
      foreach (array_keys($form['actions']) as $action) {
        if ($action != 'preview' && isset($form['actions'][$action]['#type']) && $form['actions'][$action]['#type'] === 'submit') {
          $form['actions'][$action]['#submit'][0] = 'easy_install_form_submit';
        }
      }
      $form['ins_all_configs'] = [
        '#type' => 'checkbox',
        '#label' => $label,
        '#title' => $label,
        '#validated' => TRUE,
      ];
    }
    if (!empty($opt_options)) {
      $form['opt_all_configs'] = [
        '#type' => 'checkbox',
        '#label' => 'Delete all the listed Optional configurations',
        '#title' => 'Delete all the listed Optional configurations',
        '#validated' => TRUE,
      ];
    }
  }
}

/**
 * Implements custom submit().
 */
function easy_install_form_submit(array $form, FormStateInterface $form_state) {
  $account = \Drupal::currentUser()
    ->id();
  $modules_list = \Drupal::service('keyvalue.expirable')
    ->get('modules_uninstall');
  $modules = $modules_list
    ->get($account);
  $module_handler = \Drupal::service('module_installer');
  $module_handler
    ->uninstall($modules);
  $modules_list
    ->delete($account);
  $msg = 'The selected modules have been uninstalled';
  $ins_configs = $form_state
    ->getValue('configs') ? $form_state
    ->getValue('configs') : [];
  if ($form_state
    ->getValue('ins_all_configs') != 0) {
    foreach ($ins_configs as $key => $value) {
      Drupal::configFactory()
        ->getEditable($key)
        ->delete();
    }
    $msg = 'The selected modules have been uninstalled and configurations
  deleted';
  }
  else {
    foreach ($ins_configs as $key => $values) {
      if ($values !== 0) {
        Drupal::configFactory()
          ->getEditable($key)
          ->delete();
        $msg = 'The selected modules have been uninstalled and configurations
         deleted';
      }
    }
  }
  $opt_configs = $form_state
    ->getValue('opt_configs') ? $form_state
    ->getValue('opt_configs') : [];
  if ($form_state
    ->getValue('opt_all_configs') != 0) {
    foreach ($opt_configs as $key => $value) {
      Drupal::configFactory()
        ->getEditable($key)
        ->delete();
    }
    $msg = 'The selected modules have been uninstalled and configurations
  deleted';
  }
  else {
    foreach ($opt_configs as $key => $values) {
      if ($values !== 0) {
        Drupal::configFactory()
          ->getEditable($key)
          ->delete();
        $msg = 'The selected modules have been uninstalled and configurations
         deleted';
      }
    }
  }
  drupal_set_message(t('@msg', [
    '@msg' => $msg,
  ]));
  $redirect = new Url('system.modules_uninstall');
  $form_state
    ->setRedirectUrl($redirect);
}

Functions

Namesort descending Description
easy_install_form_alter Implements hook_form_alter().
easy_install_form_submit Implements custom submit().