You are here

easy_install.module in Easy Install 8.3

Easy install module file.

File

easy_install.module
View source
<?php

/**
 * @file
 * Easy install 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 = array(
    'system_modules_uninstall_confirm_form',
  );
  if (in_array($form_id, $form_ids)) {
    $modules = \Drupal::service('keyvalue.expirable')
      ->get('modules_uninstall')
      ->get(\Drupal::currentUser()
      ->id());

    //$module_handler = \Drupal::service('module_handler');
    foreach ($modules as $module) {

      //$schema_dir = drupal_get_path('module', $module) . '/' . InstallStorage::CONFIG_SCHEMA_DIRECTORY;
      $install_dir = drupal_get_path('module', $module) . '/' . InstallStorage::CONFIG_INSTALL_DIRECTORY;

      //$optional_dir = drupal_get_path('module', $module) . '/' . InstallStorage::CONFIG_OPTIONAL_DIRECTORY;
      $details = file_scan_directory($install_dir, "/\\.(yml)\$/");
      if (!empty($details)) {
        $form['modules_config'][$module] = array(
          '#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,
        );
        $details = file_scan_directory($install_dir, "/\\.(yml)\$/");
        $options = [];
        foreach ($details as $config_value) {
          $options[$config_value->name] = $config_value->name;
        }
        $form['modules_config'][$module]['configs'] = array(
          '#type' => 'checkboxes',
          '#label' => $config_value->name,
          '#title' => t('Select the configurations to be deleted'),
          '#options' => $options,
          '#validated' => TRUE,
        );
      }
      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['all_configs'] = array(
      '#type' => 'checkbox',
      '#label' => t('Delete all the listed configurations'),
      '#title' => t('Delete all the listed 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);
  if ($form_state
    ->getValue('all_configs')) {
    foreach ($form_state
      ->getValues('configs') as $key => $value) {
      Drupal::configFactory()
        ->getEditable($key)
        ->delete();
    }
  }
  else {
    foreach ($form_state
      ->getValues('configs') as $key => $values) {
      if ($values) {
        Drupal::configFactory()
          ->getEditable($key)
          ->delete();
      }
    }
  }
  drupal_set_message(t('The selected modules have been uninstalled and configurations deleted'));
  $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().