Repository navigation
Expand file tree
/
Copy pathclean_code.install
More file actions
49 lines (43 loc) · 1.53 KB
/
Copy pathclean_code.install
File metadata and controls
49 lines (43 loc) · 1.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<?php
/**
* @file
* Contains the uninstall hook and helper functions for the Clean Code module.
*/
declare(strict_types=1);
/**
* Implements hook_uninstall().
*/
function clean_code_uninstall() {
clean_code_remove_packages();
}
/**
* Helper function to remove selected packages on module uninstall.
*/
function clean_code_remove_packages() {
// Retrieve services only once.
$state = \Drupal::service('state');
$config_factory = \Drupal::service('config.factory');
$logger = \Drupal::service('logger.factory')->get('clean_code');
$package_manager = \Drupal::service('clean_code.package_manager');
// Check the removal status in the state service.
if ($state->get('clean_code.remove_packages')) {
// Get the selected packages from the config.
$config = $config_factory->getEditable('clean_code.settings');
$selected_packages = $config->get('selected_packages');
if (!empty($selected_packages)) {
// Remove the packages using the PackageManager service.
try {
$package_manager->removePackages($selected_packages);
// Clear the selected packages from the config and save it.
$config->clear('selected_packages')->save();
// Log success message.
$logger->notice('Selected packages have been successfully removed during the Clean Code module uninstall.');
}
catch (\Exception $e) {
$logger->error('Failed to remove selected packages during Clean Code module uninstall: @message', [
'@message' => $e->getMessage(),
]);
}
}
}
}