ModuleRequiredByThemesUninstallValidator.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. namespace Drupal\Core\Extension;
  3. use Drupal\Core\StringTranslation\StringTranslationTrait;
  4. use Drupal\Core\StringTranslation\TranslationInterface;
  5. /**
  6. * Ensures modules cannot be uninstalled if enabled themes depend on them.
  7. */
  8. class ModuleRequiredByThemesUninstallValidator implements ModuleUninstallValidatorInterface {
  9. use StringTranslationTrait;
  10. /**
  11. * The module extension list.
  12. *
  13. * @var \Drupal\Core\Extension\ModuleExtensionList
  14. */
  15. protected $moduleExtensionList;
  16. /**
  17. * The theme extension list.
  18. *
  19. * @var \Drupal\Core\Extension\ThemeExtensionList
  20. */
  21. protected $themeExtensionList;
  22. /**
  23. * Constructs a new ModuleRequiredByThemesUninstallValidator.
  24. *
  25. * @param \Drupal\Core\StringTranslation\TranslationInterface $string_translation
  26. * The string translation service.
  27. * @param \Drupal\Core\Extension\ModuleExtensionList $extension_list_module
  28. * The module extension list.
  29. * @param \Drupal\Core\Extension\ThemeExtensionList $extension_list_theme
  30. * The theme extension list.
  31. */
  32. public function __construct(TranslationInterface $string_translation, ModuleExtensionList $extension_list_module, ThemeExtensionList $extension_list_theme) {
  33. $this->stringTranslation = $string_translation;
  34. $this->moduleExtensionList = $extension_list_module;
  35. $this->themeExtensionList = $extension_list_theme;
  36. }
  37. /**
  38. * {@inheritdoc}
  39. */
  40. public function validate($module) {
  41. $reasons = [];
  42. $themes_depending_on_module = $this->getThemesDependingOnModule($module);
  43. if (!empty($themes_depending_on_module)) {
  44. $module_name = $this->moduleExtensionList->get($module)->info['name'];
  45. $theme_names = implode(', ', $themes_depending_on_module);
  46. $reasons[] = $this->formatPlural(count($themes_depending_on_module),
  47. 'Required by the theme: @theme_names',
  48. 'Required by the themes: @theme_names',
  49. ['@module_name' => $module_name, '@theme_names' => $theme_names]);
  50. }
  51. return $reasons;
  52. }
  53. /**
  54. * Returns themes that depend on a module.
  55. *
  56. * @param string $module
  57. * The module machine name.
  58. *
  59. * @return string[]
  60. * An array of the names of themes that depend on $module.
  61. */
  62. protected function getThemesDependingOnModule($module) {
  63. $installed_themes = $this->themeExtensionList->getAllInstalledInfo();
  64. $themes_depending_on_module = array_map(function ($theme) use ($module) {
  65. if (in_array($module, $theme['dependencies'])) {
  66. return $theme['name'];
  67. }
  68. }, $installed_themes);
  69. return array_filter($themes_depending_on_module);
  70. }
  71. }