Newer
Older
Dries Buytaert
committed
<?php
/**
* @file
* Contains \Drupal\system\Form\ModulesUninstallConfirmForm.
*/
namespace Drupal\system\Form;
use Drupal\Core\Config\ConfigManagerInterface;
use Drupal\Core\Config\Entity\ConfigDependencyDeleteFormTrait;
use Drupal\Core\Entity\EntityManagerInterface;
Alex Pott
committed
use Drupal\Core\Extension\ModuleInstallerInterface;
Dries Buytaert
committed
use Drupal\Core\Form\ConfirmFormBase;
Dries Buytaert
committed
use Drupal\Core\Form\FormStateInterface;
Angie Byron
committed
use Drupal\Core\Url;
catch
committed
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\KeyValueStore\KeyValueStoreExpirableInterface;
Dries Buytaert
committed
/**
* Builds a confirmation form to uninstall selected modules.
*/
Angie Byron
committed
class ModulesUninstallConfirmForm extends ConfirmFormBase {
use ConfigDependencyDeleteFormTrait;
catch
committed
/**
Alex Pott
committed
* The module installer service.
catch
committed
*
Alex Pott
committed
* @var \Drupal\Core\Extension\ModuleInstallerInterface
catch
committed
*/
Alex Pott
committed
protected $moduleInstaller;
catch
committed
/**
* The expirable key value store.
*
* @var \Drupal\Core\KeyValueStore\KeyValueStoreExpirableInterface
*/
protected $keyValueExpirable;
/**
* The configuration manager.
*
* @var \Drupal\Core\Config\ConfigManagerInterface
*/
protected $configManager;
/**
* The entity manager.
*
* @var \Drupal\Core\Entity\EntityManagerInterface
*/
protected $entityManager;
catch
committed
/**
* An array of modules to uninstall.
*
* @var array
*/
protected $modules = array();
/**
* Constructs a ModulesUninstallConfirmForm object.
*
Alex Pott
committed
* @param \Drupal\Core\Extension\ModuleInstallerInterface $module_installer
* The module installer.
catch
committed
* @param \Drupal\Core\KeyValueStore\KeyValueStoreExpirableInterface $key_value_expirable
* The key value expirable factory.
* @param \Drupal\Core\Config\ConfigManagerInterface $config_manager
* The configuration manager.
* @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
* The entity manager.
catch
committed
*/
Alex Pott
committed
public function __construct(ModuleInstallerInterface $module_installer, KeyValueStoreExpirableInterface $key_value_expirable, ConfigManagerInterface $config_manager, EntityManagerInterface $entity_manager) {
$this->moduleInstaller = $module_installer;
catch
committed
$this->keyValueExpirable = $key_value_expirable;
$this->configManager = $config_manager;
$this->entityManager = $entity_manager;
Alex Pott
committed
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
Alex Pott
committed
$container->get('module_installer'),
$container->get('keyvalue.expirable')->get('modules_uninstall'),
$container->get('config.manager'),
$container->get('entity.manager')
Alex Pott
committed
);
catch
committed
}
Dries Buytaert
committed
/**
* {@inheritdoc}
*/
public function getQuestion() {
Alex Pott
committed
return $this->t('Confirm uninstall');
Dries Buytaert
committed
}
/**
* {@inheritdoc}
*/
public function getConfirmText() {
Alex Pott
committed
return $this->t('Uninstall');
Dries Buytaert
committed
}
/**
* {@inheritdoc}
*/
public function getCancelUrl() {
Angie Byron
committed
return new Url('system.modules_uninstall');
Dries Buytaert
committed
}
/**
* {@inheritdoc}
*/
public function getDescription() {
Alex Pott
committed
return $this->t('Would you like to continue with uninstalling the above?');
Dries Buytaert
committed
}
/**
* {@inheritdoc}
*/
Alex Pott
committed
public function getFormId() {
Dries Buytaert
committed
return 'system_modules_uninstall_confirm_form';
}
/**
* {@inheritdoc}
*/
Dries Buytaert
committed
public function buildForm(array $form, FormStateInterface $form_state) {
catch
committed
// Retrieve the list of modules from the key value store.
$account = $this->currentUser()->id();
catch
committed
$this->modules = $this->keyValueExpirable->get($account);
// Prevent this page from showing when the module list is empty.
if (empty($this->modules)) {
drupal_set_message($this->t('The selected modules could not be uninstalled, either due to a website problem or due to the uninstall confirmation form timing out. Please try again.'), 'error');
return $this->redirect('system.modules_uninstall');
Dries Buytaert
committed
}
catch
committed
$data = system_rebuild_module_data();
Alex Pott
committed
$form['text']['#markup'] = '<p>' . $this->t('The following modules will be completely uninstalled from your site, and <em>all data from these modules will be lost</em>!') . '</p>';
catch
committed
$form['modules'] = array(
'#theme' => 'item_list',
'#items' => array_map(function ($module) use ($data) {
return $data[$module]->info['name'];
}, $this->modules),
);
Dries Buytaert
committed
// List the dependent entities.
$this->addDependencyListsToForm($form, 'module', $this->modules ,$this->configManager, $this->entityManager);
Alex Pott
committed
return parent::buildForm($form, $form_state);
Dries Buytaert
committed
}
/**
* {@inheritdoc}
*/
Dries Buytaert
committed
public function submitForm(array &$form, FormStateInterface $form_state) {
catch
committed
// Clear the key value store entry.
$account = $this->currentUser()->id();
catch
committed
$this->keyValueExpirable->delete($account);
// Uninstall the modules.
Alex Pott
committed
$this->moduleInstaller->uninstall($this->modules);
catch
committed
Alex Pott
committed
drupal_set_message($this->t('The selected modules have been uninstalled.'));
Alex Pott
committed
$form_state->setRedirectUrl($this->getCancelUrl());