Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\Core\Extension;
|
Chris@0
|
4
|
Chris@0
|
5 use Drupal\Core\Cache\Cache;
|
Chris@0
|
6 use Drupal\Core\Cache\CacheBackendInterface;
|
Chris@0
|
7 use Drupal\Core\DrupalKernelInterface;
|
Chris@0
|
8 use Drupal\Core\Entity\EntityStorageException;
|
Chris@0
|
9 use Drupal\Core\Entity\FieldableEntityInterface;
|
Chris@0
|
10 use Drupal\Core\Serialization\Yaml;
|
Chris@0
|
11
|
Chris@0
|
12 /**
|
Chris@0
|
13 * Default implementation of the module installer.
|
Chris@0
|
14 *
|
Chris@0
|
15 * It registers the module in config, installs its own configuration,
|
Chris@0
|
16 * installs the schema, updates the Drupal kernel and more.
|
Chris@0
|
17 */
|
Chris@0
|
18 class ModuleInstaller implements ModuleInstallerInterface {
|
Chris@0
|
19
|
Chris@0
|
20 /**
|
Chris@0
|
21 * The module handler.
|
Chris@0
|
22 *
|
Chris@0
|
23 * @var \Drupal\Core\Extension\ModuleHandlerInterface
|
Chris@0
|
24 */
|
Chris@0
|
25 protected $moduleHandler;
|
Chris@0
|
26
|
Chris@0
|
27 /**
|
Chris@0
|
28 * The drupal kernel.
|
Chris@0
|
29 *
|
Chris@0
|
30 * @var \Drupal\Core\DrupalKernelInterface
|
Chris@0
|
31 */
|
Chris@0
|
32 protected $kernel;
|
Chris@0
|
33
|
Chris@0
|
34 /**
|
Chris@0
|
35 * The app root.
|
Chris@0
|
36 *
|
Chris@0
|
37 * @var string
|
Chris@0
|
38 */
|
Chris@0
|
39 protected $root;
|
Chris@0
|
40
|
Chris@0
|
41 /**
|
Chris@0
|
42 * The uninstall validators.
|
Chris@0
|
43 *
|
Chris@0
|
44 * @var \Drupal\Core\Extension\ModuleUninstallValidatorInterface[]
|
Chris@0
|
45 */
|
Chris@0
|
46 protected $uninstallValidators;
|
Chris@0
|
47
|
Chris@0
|
48 /**
|
Chris@0
|
49 * Constructs a new ModuleInstaller instance.
|
Chris@0
|
50 *
|
Chris@0
|
51 * @param string $root
|
Chris@0
|
52 * The app root.
|
Chris@0
|
53 * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
|
Chris@0
|
54 * The module handler.
|
Chris@0
|
55 * @param \Drupal\Core\DrupalKernelInterface $kernel
|
Chris@0
|
56 * The drupal kernel.
|
Chris@0
|
57 *
|
Chris@0
|
58 * @see \Drupal\Core\DrupalKernel
|
Chris@0
|
59 * @see \Drupal\Core\CoreServiceProvider
|
Chris@0
|
60 */
|
Chris@0
|
61 public function __construct($root, ModuleHandlerInterface $module_handler, DrupalKernelInterface $kernel) {
|
Chris@0
|
62 $this->root = $root;
|
Chris@0
|
63 $this->moduleHandler = $module_handler;
|
Chris@0
|
64 $this->kernel = $kernel;
|
Chris@0
|
65 }
|
Chris@0
|
66
|
Chris@0
|
67 /**
|
Chris@0
|
68 * {@inheritdoc}
|
Chris@0
|
69 */
|
Chris@0
|
70 public function addUninstallValidator(ModuleUninstallValidatorInterface $uninstall_validator) {
|
Chris@0
|
71 $this->uninstallValidators[] = $uninstall_validator;
|
Chris@0
|
72 }
|
Chris@0
|
73
|
Chris@0
|
74 /**
|
Chris@0
|
75 * {@inheritdoc}
|
Chris@0
|
76 */
|
Chris@0
|
77 public function install(array $module_list, $enable_dependencies = TRUE) {
|
Chris@0
|
78 $extension_config = \Drupal::configFactory()->getEditable('core.extension');
|
Chris@0
|
79 if ($enable_dependencies) {
|
Chris@0
|
80 // Get all module data so we can find dependencies and sort.
|
Chris@0
|
81 $module_data = system_rebuild_module_data();
|
Chris@0
|
82 $module_list = $module_list ? array_combine($module_list, $module_list) : [];
|
Chris@0
|
83 if ($missing_modules = array_diff_key($module_list, $module_data)) {
|
Chris@0
|
84 // One or more of the given modules doesn't exist.
|
Chris@0
|
85 throw new MissingDependencyException(sprintf('Unable to install modules %s due to missing modules %s.', implode(', ', $module_list), implode(', ', $missing_modules)));
|
Chris@0
|
86 }
|
Chris@0
|
87
|
Chris@0
|
88 // Only process currently uninstalled modules.
|
Chris@0
|
89 $installed_modules = $extension_config->get('module') ?: [];
|
Chris@0
|
90 if (!$module_list = array_diff_key($module_list, $installed_modules)) {
|
Chris@0
|
91 // Nothing to do. All modules already installed.
|
Chris@0
|
92 return TRUE;
|
Chris@0
|
93 }
|
Chris@0
|
94
|
Chris@0
|
95 // Add dependencies to the list. The new modules will be processed as
|
Chris@0
|
96 // the while loop continues.
|
Chris@0
|
97 while (list($module) = each($module_list)) {
|
Chris@0
|
98 foreach (array_keys($module_data[$module]->requires) as $dependency) {
|
Chris@0
|
99 if (!isset($module_data[$dependency])) {
|
Chris@0
|
100 // The dependency does not exist.
|
Chris@0
|
101 throw new MissingDependencyException("Unable to install modules: module '$module' is missing its dependency module $dependency.");
|
Chris@0
|
102 }
|
Chris@0
|
103
|
Chris@0
|
104 // Skip already installed modules.
|
Chris@0
|
105 if (!isset($module_list[$dependency]) && !isset($installed_modules[$dependency])) {
|
Chris@0
|
106 $module_list[$dependency] = $dependency;
|
Chris@0
|
107 }
|
Chris@0
|
108 }
|
Chris@0
|
109 }
|
Chris@0
|
110
|
Chris@0
|
111 // Set the actual module weights.
|
Chris@0
|
112 $module_list = array_map(function ($module) use ($module_data) {
|
Chris@0
|
113 return $module_data[$module]->sort;
|
Chris@0
|
114 }, $module_list);
|
Chris@0
|
115
|
Chris@0
|
116 // Sort the module list by their weights (reverse).
|
Chris@0
|
117 arsort($module_list);
|
Chris@0
|
118 $module_list = array_keys($module_list);
|
Chris@0
|
119 }
|
Chris@0
|
120
|
Chris@0
|
121 // Required for module installation checks.
|
Chris@0
|
122 include_once $this->root . '/core/includes/install.inc';
|
Chris@0
|
123
|
Chris@0
|
124 /** @var \Drupal\Core\Config\ConfigInstaller $config_installer */
|
Chris@0
|
125 $config_installer = \Drupal::service('config.installer');
|
Chris@0
|
126 $sync_status = $config_installer->isSyncing();
|
Chris@0
|
127 if ($sync_status) {
|
Chris@0
|
128 $source_storage = $config_installer->getSourceStorage();
|
Chris@0
|
129 }
|
Chris@0
|
130 $modules_installed = [];
|
Chris@0
|
131 foreach ($module_list as $module) {
|
Chris@0
|
132 $enabled = $extension_config->get("module.$module") !== NULL;
|
Chris@0
|
133 if (!$enabled) {
|
Chris@0
|
134 // Throw an exception if the module name is too long.
|
Chris@0
|
135 if (strlen($module) > DRUPAL_EXTENSION_NAME_MAX_LENGTH) {
|
Chris@0
|
136 throw new ExtensionNameLengthException("Module name '$module' is over the maximum allowed length of " . DRUPAL_EXTENSION_NAME_MAX_LENGTH . ' characters');
|
Chris@0
|
137 }
|
Chris@0
|
138
|
Chris@0
|
139 // Load a new config object for each iteration, otherwise changes made
|
Chris@0
|
140 // in hook_install() are not reflected in $extension_config.
|
Chris@0
|
141 $extension_config = \Drupal::configFactory()->getEditable('core.extension');
|
Chris@0
|
142
|
Chris@0
|
143 // Check the validity of the default configuration. This will throw
|
Chris@0
|
144 // exceptions if the configuration is not valid.
|
Chris@0
|
145 $config_installer->checkConfigurationToInstall('module', $module);
|
Chris@0
|
146
|
Chris@0
|
147 // Save this data without checking schema. This is a performance
|
Chris@0
|
148 // improvement for module installation.
|
Chris@0
|
149 $extension_config
|
Chris@0
|
150 ->set("module.$module", 0)
|
Chris@0
|
151 ->set('module', module_config_sort($extension_config->get('module')))
|
Chris@0
|
152 ->save(TRUE);
|
Chris@0
|
153
|
Chris@0
|
154 // Prepare the new module list, sorted by weight, including filenames.
|
Chris@0
|
155 // This list is used for both the ModuleHandler and DrupalKernel. It
|
Chris@0
|
156 // needs to be kept in sync between both. A DrupalKernel reboot or
|
Chris@0
|
157 // rebuild will automatically re-instantiate a new ModuleHandler that
|
Chris@0
|
158 // uses the new module list of the kernel. However, DrupalKernel does
|
Chris@0
|
159 // not cause any modules to be loaded.
|
Chris@0
|
160 // Furthermore, the currently active (fixed) module list can be
|
Chris@0
|
161 // different from the configured list of enabled modules. For all active
|
Chris@0
|
162 // modules not contained in the configured enabled modules, we assume a
|
Chris@0
|
163 // weight of 0.
|
Chris@0
|
164 $current_module_filenames = $this->moduleHandler->getModuleList();
|
Chris@0
|
165 $current_modules = array_fill_keys(array_keys($current_module_filenames), 0);
|
Chris@0
|
166 $current_modules = module_config_sort(array_merge($current_modules, $extension_config->get('module')));
|
Chris@0
|
167 $module_filenames = [];
|
Chris@0
|
168 foreach ($current_modules as $name => $weight) {
|
Chris@0
|
169 if (isset($current_module_filenames[$name])) {
|
Chris@0
|
170 $module_filenames[$name] = $current_module_filenames[$name];
|
Chris@0
|
171 }
|
Chris@0
|
172 else {
|
Chris@0
|
173 $module_path = drupal_get_path('module', $name);
|
Chris@0
|
174 $pathname = "$module_path/$name.info.yml";
|
Chris@0
|
175 $filename = file_exists($module_path . "/$name.module") ? "$name.module" : NULL;
|
Chris@0
|
176 $module_filenames[$name] = new Extension($this->root, 'module', $pathname, $filename);
|
Chris@0
|
177 }
|
Chris@0
|
178 }
|
Chris@0
|
179
|
Chris@0
|
180 // Update the module handler in order to load the module's code.
|
Chris@0
|
181 // This allows the module to participate in hooks and its existence to
|
Chris@0
|
182 // be discovered by other modules.
|
Chris@0
|
183 // The current ModuleHandler instance is obsolete with the kernel
|
Chris@0
|
184 // rebuild below.
|
Chris@0
|
185 $this->moduleHandler->setModuleList($module_filenames);
|
Chris@0
|
186 $this->moduleHandler->load($module);
|
Chris@0
|
187 module_load_install($module);
|
Chris@0
|
188
|
Chris@0
|
189 // Clear the static cache of system_rebuild_module_data() to pick up the
|
Chris@0
|
190 // new module, since it merges the installation status of modules into
|
Chris@0
|
191 // its statically cached list.
|
Chris@0
|
192 drupal_static_reset('system_rebuild_module_data');
|
Chris@0
|
193
|
Chris@0
|
194 // Update the kernel to include it.
|
Chris@0
|
195 $this->updateKernel($module_filenames);
|
Chris@0
|
196
|
Chris@12
|
197 // Replace the route provider service with a version that will rebuild
|
Chris@12
|
198 // if routes used during installation. This ensures that a module's
|
Chris@12
|
199 // routes are available during installation. This has to occur before
|
Chris@12
|
200 // any services that depend on it are instantiated otherwise those
|
Chris@12
|
201 // services will have the old route provider injected. Note that, since
|
Chris@12
|
202 // the container is rebuilt by updating the kernel, the route provider
|
Chris@12
|
203 // service is the regular one even though we are in a loop and might
|
Chris@12
|
204 // have replaced it before.
|
Chris@12
|
205 \Drupal::getContainer()->set('router.route_provider.old', \Drupal::service('router.route_provider'));
|
Chris@12
|
206 \Drupal::getContainer()->set('router.route_provider', \Drupal::service('router.route_provider.lazy_builder'));
|
Chris@12
|
207
|
Chris@0
|
208 // Allow modules to react prior to the installation of a module.
|
Chris@0
|
209 $this->moduleHandler->invokeAll('module_preinstall', [$module]);
|
Chris@0
|
210
|
Chris@0
|
211 // Now install the module's schema if necessary.
|
Chris@0
|
212 drupal_install_schema($module);
|
Chris@0
|
213
|
Chris@0
|
214 // Clear plugin manager caches.
|
Chris@0
|
215 \Drupal::getContainer()->get('plugin.cache_clearer')->clearCachedDefinitions();
|
Chris@0
|
216
|
Chris@0
|
217 // Set the schema version to the number of the last update provided by
|
Chris@0
|
218 // the module, or the minimum core schema version.
|
Chris@0
|
219 $version = \Drupal::CORE_MINIMUM_SCHEMA_VERSION;
|
Chris@0
|
220 $versions = drupal_get_schema_versions($module);
|
Chris@0
|
221 if ($versions) {
|
Chris@0
|
222 $version = max(max($versions), $version);
|
Chris@0
|
223 }
|
Chris@0
|
224
|
Chris@0
|
225 // Notify interested components that this module's entity types and
|
Chris@0
|
226 // field storage definitions are new. For example, a SQL-based storage
|
Chris@0
|
227 // handler can use this as an opportunity to create the necessary
|
Chris@0
|
228 // database tables.
|
Chris@0
|
229 // @todo Clean this up in https://www.drupal.org/node/2350111.
|
Chris@0
|
230 $entity_manager = \Drupal::entityManager();
|
Chris@0
|
231 $update_manager = \Drupal::entityDefinitionUpdateManager();
|
Chris@0
|
232 foreach ($entity_manager->getDefinitions() as $entity_type) {
|
Chris@0
|
233 if ($entity_type->getProvider() == $module) {
|
Chris@0
|
234 $update_manager->installEntityType($entity_type);
|
Chris@0
|
235 }
|
Chris@0
|
236 elseif ($entity_type->entityClassImplements(FieldableEntityInterface::CLASS)) {
|
Chris@0
|
237 // The module being installed may be adding new fields to existing
|
Chris@0
|
238 // entity types. Field definitions for any entity type defined by
|
Chris@0
|
239 // the module are handled in the if branch.
|
Chris@0
|
240 foreach ($entity_manager->getFieldStorageDefinitions($entity_type->id()) as $storage_definition) {
|
Chris@0
|
241 if ($storage_definition->getProvider() == $module) {
|
Chris@0
|
242 // If the module being installed is also defining a storage key
|
Chris@0
|
243 // for the entity type, the entity schema may not exist yet. It
|
Chris@0
|
244 // will be created later in that case.
|
Chris@0
|
245 try {
|
Chris@0
|
246 $update_manager->installFieldStorageDefinition($storage_definition->getName(), $entity_type->id(), $module, $storage_definition);
|
Chris@0
|
247 }
|
Chris@0
|
248 catch (EntityStorageException $e) {
|
Chris@0
|
249 watchdog_exception('system', $e, 'An error occurred while notifying the creation of the @name field storage definition: "!message" in %function (line %line of %file).', ['@name' => $storage_definition->getName()]);
|
Chris@0
|
250 }
|
Chris@0
|
251 }
|
Chris@0
|
252 }
|
Chris@0
|
253 }
|
Chris@0
|
254 }
|
Chris@0
|
255
|
Chris@0
|
256 // Install default configuration of the module.
|
Chris@0
|
257 $config_installer = \Drupal::service('config.installer');
|
Chris@0
|
258 if ($sync_status) {
|
Chris@0
|
259 $config_installer
|
Chris@0
|
260 ->setSyncing(TRUE)
|
Chris@0
|
261 ->setSourceStorage($source_storage);
|
Chris@0
|
262 }
|
Chris@0
|
263 \Drupal::service('config.installer')->installDefaultConfig('module', $module);
|
Chris@0
|
264
|
Chris@0
|
265 // If the module has no current updates, but has some that were
|
Chris@0
|
266 // previously removed, set the version to the value of
|
Chris@0
|
267 // hook_update_last_removed().
|
Chris@0
|
268 if ($last_removed = $this->moduleHandler->invoke($module, 'update_last_removed')) {
|
Chris@0
|
269 $version = max($version, $last_removed);
|
Chris@0
|
270 }
|
Chris@0
|
271 drupal_set_installed_schema_version($module, $version);
|
Chris@0
|
272
|
Chris@0
|
273 // Ensure that all post_update functions are registered already.
|
Chris@0
|
274 /** @var \Drupal\Core\Update\UpdateRegistry $post_update_registry */
|
Chris@0
|
275 $post_update_registry = \Drupal::service('update.post_update_registry');
|
Chris@0
|
276 $post_update_registry->registerInvokedUpdates($post_update_registry->getModuleUpdateFunctions($module));
|
Chris@0
|
277
|
Chris@0
|
278 // Record the fact that it was installed.
|
Chris@0
|
279 $modules_installed[] = $module;
|
Chris@0
|
280
|
Chris@0
|
281 // Drupal's stream wrappers needs to be re-registered in case a
|
Chris@0
|
282 // module-provided stream wrapper is used later in the same request. In
|
Chris@0
|
283 // particular, this happens when installing Drupal via Drush, as the
|
Chris@0
|
284 // 'translations' stream wrapper is provided by Interface Translation
|
Chris@0
|
285 // module and is later used to import translations.
|
Chris@0
|
286 \Drupal::service('stream_wrapper_manager')->register();
|
Chris@0
|
287
|
Chris@0
|
288 // Update the theme registry to include it.
|
Chris@0
|
289 drupal_theme_rebuild();
|
Chris@0
|
290
|
Chris@0
|
291 // Modules can alter theme info, so refresh theme data.
|
Chris@0
|
292 // @todo ThemeHandler cannot be injected into ModuleHandler, since that
|
Chris@0
|
293 // causes a circular service dependency.
|
Chris@0
|
294 // @see https://www.drupal.org/node/2208429
|
Chris@0
|
295 \Drupal::service('theme_handler')->refreshInfo();
|
Chris@0
|
296
|
Chris@0
|
297 // Allow the module to perform install tasks.
|
Chris@0
|
298 $this->moduleHandler->invoke($module, 'install');
|
Chris@0
|
299
|
Chris@0
|
300 // Record the fact that it was installed.
|
Chris@0
|
301 \Drupal::logger('system')->info('%module module installed.', ['%module' => $module]);
|
Chris@0
|
302 }
|
Chris@0
|
303 }
|
Chris@0
|
304
|
Chris@0
|
305 // If any modules were newly installed, invoke hook_modules_installed().
|
Chris@0
|
306 if (!empty($modules_installed)) {
|
Chris@0
|
307 \Drupal::getContainer()->set('router.route_provider', \Drupal::service('router.route_provider.old'));
|
Chris@0
|
308 if (!\Drupal::service('router.route_provider.lazy_builder')->hasRebuilt()) {
|
Chris@0
|
309 // Rebuild routes after installing module. This is done here on top of
|
Chris@0
|
310 // \Drupal\Core\Routing\RouteBuilder::destruct to not run into errors on
|
Chris@0
|
311 // fastCGI which executes ::destruct() after the module installation
|
Chris@0
|
312 // page was sent already.
|
Chris@0
|
313 \Drupal::service('router.builder')->rebuild();
|
Chris@0
|
314 }
|
Chris@0
|
315
|
Chris@0
|
316 $this->moduleHandler->invokeAll('modules_installed', [$modules_installed]);
|
Chris@0
|
317 }
|
Chris@0
|
318
|
Chris@0
|
319 return TRUE;
|
Chris@0
|
320 }
|
Chris@0
|
321
|
Chris@0
|
322 /**
|
Chris@0
|
323 * {@inheritdoc}
|
Chris@0
|
324 */
|
Chris@0
|
325 public function uninstall(array $module_list, $uninstall_dependents = TRUE) {
|
Chris@0
|
326 // Get all module data so we can find dependencies and sort.
|
Chris@0
|
327 $module_data = system_rebuild_module_data();
|
Chris@0
|
328 $module_list = $module_list ? array_combine($module_list, $module_list) : [];
|
Chris@0
|
329 if (array_diff_key($module_list, $module_data)) {
|
Chris@0
|
330 // One or more of the given modules doesn't exist.
|
Chris@0
|
331 return FALSE;
|
Chris@0
|
332 }
|
Chris@0
|
333
|
Chris@0
|
334 $extension_config = \Drupal::configFactory()->getEditable('core.extension');
|
Chris@0
|
335 $installed_modules = $extension_config->get('module') ?: [];
|
Chris@0
|
336 if (!$module_list = array_intersect_key($module_list, $installed_modules)) {
|
Chris@0
|
337 // Nothing to do. All modules already uninstalled.
|
Chris@0
|
338 return TRUE;
|
Chris@0
|
339 }
|
Chris@0
|
340
|
Chris@0
|
341 if ($uninstall_dependents) {
|
Chris@0
|
342 // Add dependent modules to the list. The new modules will be processed as
|
Chris@0
|
343 // the while loop continues.
|
Chris@0
|
344 $profile = drupal_get_profile();
|
Chris@0
|
345 while (list($module) = each($module_list)) {
|
Chris@0
|
346 foreach (array_keys($module_data[$module]->required_by) as $dependent) {
|
Chris@0
|
347 if (!isset($module_data[$dependent])) {
|
Chris@0
|
348 // The dependent module does not exist.
|
Chris@0
|
349 return FALSE;
|
Chris@0
|
350 }
|
Chris@0
|
351
|
Chris@0
|
352 // Skip already uninstalled modules.
|
Chris@0
|
353 if (isset($installed_modules[$dependent]) && !isset($module_list[$dependent]) && $dependent != $profile) {
|
Chris@0
|
354 $module_list[$dependent] = $dependent;
|
Chris@0
|
355 }
|
Chris@0
|
356 }
|
Chris@0
|
357 }
|
Chris@0
|
358 }
|
Chris@0
|
359
|
Chris@0
|
360 // Use the validators and throw an exception with the reasons.
|
Chris@0
|
361 if ($reasons = $this->validateUninstall($module_list)) {
|
Chris@0
|
362 foreach ($reasons as $reason) {
|
Chris@0
|
363 $reason_message[] = implode(', ', $reason);
|
Chris@0
|
364 }
|
Chris@0
|
365 throw new ModuleUninstallValidatorException('The following reasons prevent the modules from being uninstalled: ' . implode('; ', $reason_message));
|
Chris@0
|
366 }
|
Chris@0
|
367 // Set the actual module weights.
|
Chris@0
|
368 $module_list = array_map(function ($module) use ($module_data) {
|
Chris@0
|
369 return $module_data[$module]->sort;
|
Chris@0
|
370 }, $module_list);
|
Chris@0
|
371
|
Chris@0
|
372 // Sort the module list by their weights.
|
Chris@0
|
373 asort($module_list);
|
Chris@0
|
374 $module_list = array_keys($module_list);
|
Chris@0
|
375
|
Chris@0
|
376 // Only process modules that are enabled. A module is only enabled if it is
|
Chris@0
|
377 // configured as enabled. Custom or overridden module handlers might contain
|
Chris@0
|
378 // the module already, which means that it might be loaded, but not
|
Chris@0
|
379 // necessarily installed.
|
Chris@0
|
380 foreach ($module_list as $module) {
|
Chris@0
|
381
|
Chris@0
|
382 // Clean up all entity bundles (including fields) of every entity type
|
Chris@0
|
383 // provided by the module that is being uninstalled.
|
Chris@0
|
384 // @todo Clean this up in https://www.drupal.org/node/2350111.
|
Chris@0
|
385 $entity_manager = \Drupal::entityManager();
|
Chris@0
|
386 foreach ($entity_manager->getDefinitions() as $entity_type_id => $entity_type) {
|
Chris@0
|
387 if ($entity_type->getProvider() == $module) {
|
Chris@0
|
388 foreach (array_keys($entity_manager->getBundleInfo($entity_type_id)) as $bundle) {
|
Chris@0
|
389 $entity_manager->onBundleDelete($bundle, $entity_type_id);
|
Chris@0
|
390 }
|
Chris@0
|
391 }
|
Chris@0
|
392 }
|
Chris@0
|
393
|
Chris@0
|
394 // Allow modules to react prior to the uninstallation of a module.
|
Chris@0
|
395 $this->moduleHandler->invokeAll('module_preuninstall', [$module]);
|
Chris@0
|
396
|
Chris@0
|
397 // Uninstall the module.
|
Chris@0
|
398 module_load_install($module);
|
Chris@0
|
399 $this->moduleHandler->invoke($module, 'uninstall');
|
Chris@0
|
400
|
Chris@0
|
401 // Remove all configuration belonging to the module.
|
Chris@0
|
402 \Drupal::service('config.manager')->uninstall('module', $module);
|
Chris@0
|
403
|
Chris@0
|
404 // In order to make uninstalling transactional if anything uses routes.
|
Chris@0
|
405 \Drupal::getContainer()->set('router.route_provider.old', \Drupal::service('router.route_provider'));
|
Chris@0
|
406 \Drupal::getContainer()->set('router.route_provider', \Drupal::service('router.route_provider.lazy_builder'));
|
Chris@0
|
407
|
Chris@0
|
408 // Notify interested components that this module's entity types are being
|
Chris@0
|
409 // deleted. For example, a SQL-based storage handler can use this as an
|
Chris@0
|
410 // opportunity to drop the corresponding database tables.
|
Chris@0
|
411 // @todo Clean this up in https://www.drupal.org/node/2350111.
|
Chris@0
|
412 $update_manager = \Drupal::entityDefinitionUpdateManager();
|
Chris@0
|
413 foreach ($entity_manager->getDefinitions() as $entity_type) {
|
Chris@0
|
414 if ($entity_type->getProvider() == $module) {
|
Chris@0
|
415 $update_manager->uninstallEntityType($entity_type);
|
Chris@0
|
416 }
|
Chris@0
|
417 elseif ($entity_type->entityClassImplements(FieldableEntityInterface::CLASS)) {
|
Chris@0
|
418 // The module being installed may be adding new fields to existing
|
Chris@0
|
419 // entity types. Field definitions for any entity type defined by
|
Chris@0
|
420 // the module are handled in the if branch.
|
Chris@0
|
421 $entity_type_id = $entity_type->id();
|
Chris@0
|
422 /** @var \Drupal\Core\Entity\FieldableEntityStorageInterface $storage */
|
Chris@0
|
423 $storage = $entity_manager->getStorage($entity_type_id);
|
Chris@0
|
424 foreach ($entity_manager->getFieldStorageDefinitions($entity_type_id) as $storage_definition) {
|
Chris@0
|
425 // @todo We need to trigger field purging here.
|
Chris@0
|
426 // See https://www.drupal.org/node/2282119.
|
Chris@0
|
427 if ($storage_definition->getProvider() == $module && !$storage->countFieldData($storage_definition, TRUE)) {
|
Chris@0
|
428 $update_manager->uninstallFieldStorageDefinition($storage_definition);
|
Chris@0
|
429 }
|
Chris@0
|
430 }
|
Chris@0
|
431 }
|
Chris@0
|
432 }
|
Chris@0
|
433
|
Chris@0
|
434 // Remove the schema.
|
Chris@0
|
435 drupal_uninstall_schema($module);
|
Chris@0
|
436
|
Chris@0
|
437 // Remove the module's entry from the config. Don't check schema when
|
Chris@0
|
438 // uninstalling a module since we are only clearing a key.
|
Chris@0
|
439 \Drupal::configFactory()->getEditable('core.extension')->clear("module.$module")->save(TRUE);
|
Chris@0
|
440
|
Chris@0
|
441 // Update the module handler to remove the module.
|
Chris@0
|
442 // The current ModuleHandler instance is obsolete with the kernel rebuild
|
Chris@0
|
443 // below.
|
Chris@0
|
444 $module_filenames = $this->moduleHandler->getModuleList();
|
Chris@0
|
445 unset($module_filenames[$module]);
|
Chris@0
|
446 $this->moduleHandler->setModuleList($module_filenames);
|
Chris@0
|
447
|
Chris@0
|
448 // Remove any potential cache bins provided by the module.
|
Chris@0
|
449 $this->removeCacheBins($module);
|
Chris@0
|
450
|
Chris@0
|
451 // Clear the static cache of system_rebuild_module_data() to pick up the
|
Chris@0
|
452 // new module, since it merges the installation status of modules into
|
Chris@0
|
453 // its statically cached list.
|
Chris@0
|
454 drupal_static_reset('system_rebuild_module_data');
|
Chris@0
|
455
|
Chris@0
|
456 // Clear plugin manager caches.
|
Chris@0
|
457 \Drupal::getContainer()->get('plugin.cache_clearer')->clearCachedDefinitions();
|
Chris@0
|
458
|
Chris@0
|
459 // Update the kernel to exclude the uninstalled modules.
|
Chris@0
|
460 $this->updateKernel($module_filenames);
|
Chris@0
|
461
|
Chris@0
|
462 // Update the theme registry to remove the newly uninstalled module.
|
Chris@0
|
463 drupal_theme_rebuild();
|
Chris@0
|
464
|
Chris@0
|
465 // Modules can alter theme info, so refresh theme data.
|
Chris@0
|
466 // @todo ThemeHandler cannot be injected into ModuleHandler, since that
|
Chris@0
|
467 // causes a circular service dependency.
|
Chris@0
|
468 // @see https://www.drupal.org/node/2208429
|
Chris@0
|
469 \Drupal::service('theme_handler')->refreshInfo();
|
Chris@0
|
470
|
Chris@0
|
471 \Drupal::logger('system')->info('%module module uninstalled.', ['%module' => $module]);
|
Chris@0
|
472
|
Chris@0
|
473 $schema_store = \Drupal::keyValue('system.schema');
|
Chris@0
|
474 $schema_store->delete($module);
|
Chris@0
|
475
|
Chris@0
|
476 /** @var \Drupal\Core\Update\UpdateRegistry $post_update_registry */
|
Chris@0
|
477 $post_update_registry = \Drupal::service('update.post_update_registry');
|
Chris@0
|
478 $post_update_registry->filterOutInvokedUpdatesByModule($module);
|
Chris@0
|
479 }
|
Chris@0
|
480 // Rebuild routes after installing module. This is done here on top of
|
Chris@0
|
481 // \Drupal\Core\Routing\RouteBuilder::destruct to not run into errors on
|
Chris@0
|
482 // fastCGI which executes ::destruct() after the Module uninstallation page
|
Chris@0
|
483 // was sent already.
|
Chris@0
|
484 \Drupal::service('router.builder')->rebuild();
|
Chris@0
|
485 drupal_get_installed_schema_version(NULL, TRUE);
|
Chris@0
|
486
|
Chris@0
|
487 // Let other modules react.
|
Chris@0
|
488 $this->moduleHandler->invokeAll('modules_uninstalled', [$module_list]);
|
Chris@0
|
489
|
Chris@0
|
490 // Flush all persistent caches.
|
Chris@0
|
491 // Any cache entry might implicitly depend on the uninstalled modules,
|
Chris@0
|
492 // so clear all of them explicitly.
|
Chris@0
|
493 $this->moduleHandler->invokeAll('cache_flush');
|
Chris@0
|
494 foreach (Cache::getBins() as $service_id => $cache_backend) {
|
Chris@0
|
495 $cache_backend->deleteAll();
|
Chris@0
|
496 }
|
Chris@0
|
497
|
Chris@0
|
498 return TRUE;
|
Chris@0
|
499 }
|
Chris@0
|
500
|
Chris@0
|
501 /**
|
Chris@0
|
502 * Helper method for removing all cache bins registered by a given module.
|
Chris@0
|
503 *
|
Chris@0
|
504 * @param string $module
|
Chris@0
|
505 * The name of the module for which to remove all registered cache bins.
|
Chris@0
|
506 */
|
Chris@0
|
507 protected function removeCacheBins($module) {
|
Chris@0
|
508 $service_yaml_file = drupal_get_path('module', $module) . "/$module.services.yml";
|
Chris@12
|
509 if (!file_exists($service_yaml_file)) {
|
Chris@12
|
510 return;
|
Chris@12
|
511 }
|
Chris@12
|
512
|
Chris@12
|
513 $definitions = Yaml::decode(file_get_contents($service_yaml_file));
|
Chris@12
|
514
|
Chris@12
|
515 $cache_bin_services = array_filter(
|
Chris@12
|
516 isset($definitions['services']) ? $definitions['services'] : [],
|
Chris@12
|
517 function ($definition) {
|
Chris@12
|
518 $tags = isset($definition['tags']) ? $definition['tags'] : [];
|
Chris@12
|
519 foreach ($tags as $tag) {
|
Chris@12
|
520 if (isset($tag['name']) && ($tag['name'] == 'cache.bin')) {
|
Chris@12
|
521 return TRUE;
|
Chris@0
|
522 }
|
Chris@0
|
523 }
|
Chris@12
|
524 return FALSE;
|
Chris@12
|
525 }
|
Chris@12
|
526 );
|
Chris@12
|
527
|
Chris@12
|
528 foreach (array_keys($cache_bin_services) as $service_id) {
|
Chris@12
|
529 $backend = $this->kernel->getContainer()->get($service_id);
|
Chris@12
|
530 if ($backend instanceof CacheBackendInterface) {
|
Chris@12
|
531 $backend->removeBin();
|
Chris@0
|
532 }
|
Chris@0
|
533 }
|
Chris@0
|
534 }
|
Chris@0
|
535
|
Chris@0
|
536 /**
|
Chris@0
|
537 * Updates the kernel module list.
|
Chris@0
|
538 *
|
Chris@0
|
539 * @param string $module_filenames
|
Chris@0
|
540 * The list of installed modules.
|
Chris@0
|
541 */
|
Chris@0
|
542 protected function updateKernel($module_filenames) {
|
Chris@0
|
543 // This reboots the kernel to register the module's bundle and its services
|
Chris@0
|
544 // in the service container. The $module_filenames argument is taken over as
|
Chris@0
|
545 // %container.modules% parameter, which is passed to a fresh ModuleHandler
|
Chris@0
|
546 // instance upon first retrieval.
|
Chris@0
|
547 $this->kernel->updateModules($module_filenames, $module_filenames);
|
Chris@0
|
548 // After rebuilding the container we need to update the injected
|
Chris@0
|
549 // dependencies.
|
Chris@0
|
550 $container = $this->kernel->getContainer();
|
Chris@0
|
551 $this->moduleHandler = $container->get('module_handler');
|
Chris@0
|
552 }
|
Chris@0
|
553
|
Chris@0
|
554 /**
|
Chris@0
|
555 * {@inheritdoc}
|
Chris@0
|
556 */
|
Chris@0
|
557 public function validateUninstall(array $module_list) {
|
Chris@0
|
558 $reasons = [];
|
Chris@0
|
559 foreach ($module_list as $module) {
|
Chris@0
|
560 foreach ($this->uninstallValidators as $validator) {
|
Chris@0
|
561 $validation_reasons = $validator->validate($module);
|
Chris@0
|
562 if (!empty($validation_reasons)) {
|
Chris@0
|
563 if (!isset($reasons[$module])) {
|
Chris@0
|
564 $reasons[$module] = [];
|
Chris@0
|
565 }
|
Chris@0
|
566 $reasons[$module] = array_merge($reasons[$module], $validation_reasons);
|
Chris@0
|
567 }
|
Chris@0
|
568 }
|
Chris@0
|
569 }
|
Chris@0
|
570 return $reasons;
|
Chris@0
|
571 }
|
Chris@0
|
572
|
Chris@0
|
573 }
|