class ModuleRequiredByThemesUninstallValidator

Same name in this branch
  1. 9 core/lib/Drupal/Core/ProxyClass/Extension/ModuleRequiredByThemesUninstallValidator.php \Drupal\Core\ProxyClass\Extension\ModuleRequiredByThemesUninstallValidator
Same name in other branches
  1. 8.9.x core/lib/Drupal/Core/ProxyClass/Extension/ModuleRequiredByThemesUninstallValidator.php \Drupal\Core\ProxyClass\Extension\ModuleRequiredByThemesUninstallValidator
  2. 8.9.x core/lib/Drupal/Core/Extension/ModuleRequiredByThemesUninstallValidator.php \Drupal\Core\Extension\ModuleRequiredByThemesUninstallValidator
  3. 10 core/lib/Drupal/Core/ProxyClass/Extension/ModuleRequiredByThemesUninstallValidator.php \Drupal\Core\ProxyClass\Extension\ModuleRequiredByThemesUninstallValidator
  4. 10 core/lib/Drupal/Core/Extension/ModuleRequiredByThemesUninstallValidator.php \Drupal\Core\Extension\ModuleRequiredByThemesUninstallValidator
  5. 11.x core/lib/Drupal/Core/ProxyClass/Extension/ModuleRequiredByThemesUninstallValidator.php \Drupal\Core\ProxyClass\Extension\ModuleRequiredByThemesUninstallValidator
  6. 11.x core/lib/Drupal/Core/Extension/ModuleRequiredByThemesUninstallValidator.php \Drupal\Core\Extension\ModuleRequiredByThemesUninstallValidator

Ensures modules cannot be uninstalled if enabled themes depend on them.

Hierarchy

Expanded class hierarchy of ModuleRequiredByThemesUninstallValidator

1 file declares its use of ModuleRequiredByThemesUninstallValidator
ModuleRequiredByThemesUninstallValidatorTest.php in core/tests/Drupal/Tests/Core/Extension/ModuleRequiredByThemesUninstallValidatorTest.php
1 string reference to 'ModuleRequiredByThemesUninstallValidator'
core.services.yml in core/core.services.yml
core/core.services.yml
1 service uses ModuleRequiredByThemesUninstallValidator
module_required_by_themes_uninstall_validator in core/core.services.yml
Drupal\Core\Extension\ModuleRequiredByThemesUninstallValidator

File

core/lib/Drupal/Core/Extension/ModuleRequiredByThemesUninstallValidator.php, line 12

Namespace

Drupal\Core\Extension
View source
class ModuleRequiredByThemesUninstallValidator implements ConfigImportModuleUninstallValidatorInterface {
    use StringTranslationTrait;
    
    /**
     * The module extension list.
     *
     * @var \Drupal\Core\Extension\ModuleExtensionList
     */
    protected $moduleExtensionList;
    
    /**
     * The theme extension list.
     *
     * @var \Drupal\Core\Extension\ThemeExtensionList
     */
    protected $themeExtensionList;
    
    /**
     * Constructs a new ModuleRequiredByThemesUninstallValidator.
     *
     * @param \Drupal\Core\StringTranslation\TranslationInterface $string_translation
     *   The string translation service.
     * @param \Drupal\Core\Extension\ModuleExtensionList $extension_list_module
     *   The module extension list.
     * @param \Drupal\Core\Extension\ThemeExtensionList $extension_list_theme
     *   The theme extension list.
     */
    public function __construct(TranslationInterface $string_translation, ModuleExtensionList $extension_list_module, ThemeExtensionList $extension_list_theme) {
        $this->stringTranslation = $string_translation;
        $this->moduleExtensionList = $extension_list_module;
        $this->themeExtensionList = $extension_list_theme;
    }
    
    /**
     * {@inheritdoc}
     */
    public function validate($module) {
        $reasons = [];
        $themes_depending_on_module = $this->getThemesDependingOnModule($module);
        if (!empty($themes_depending_on_module)) {
            $module_name = $this->moduleExtensionList
                ->get($module)->info['name'];
            $theme_names = implode(', ', $themes_depending_on_module);
            $reasons[] = $this->formatPlural(count($themes_depending_on_module), 'Required by the theme: @theme_names', 'Required by the themes: @theme_names', [
                '@module_name' => $module_name,
                '@theme_names' => $theme_names,
            ]);
        }
        return $reasons;
    }
    
    /**
     * {@inheritdoc}
     */
    public function validateConfigImport(string $module, StorageInterface $source_storage) : array {
        $reasons = [];
        $themes_depending_on_module = $this->getThemesDependingOnModule($module);
        if (!empty($themes_depending_on_module)) {
            $installed_themes_after_import = $source_storage->read('core.extension')['theme'];
            $themes_depending_on_module_still_installed = array_intersect_key($themes_depending_on_module, $installed_themes_after_import);
            // Ensure that any dependent themes will be uninstalled by the module.
            if (!empty($themes_depending_on_module_still_installed)) {
                $reasons[] = $this->formatPlural(count($themes_depending_on_module_still_installed), 'Required by the theme: @theme_names', 'Required by the themes: @theme_names', [
                    '@theme_names' => implode(', ', $themes_depending_on_module_still_installed),
                ]);
            }
        }
        return $reasons;
    }
    
    /**
     * Returns themes that depend on a module.
     *
     * @param string $module
     *   The module machine name.
     *
     * @return string[]
     *   An array of the names of themes that depend on $module keyed by the
     *   theme's machine name.
     */
    protected function getThemesDependingOnModule($module) {
        $installed_themes = $this->themeExtensionList
            ->getAllInstalledInfo();
        $themes_depending_on_module = array_map(function ($theme) use ($module) {
            if (in_array($module, $theme['dependencies'])) {
                return $theme['name'];
            }
        }, $installed_themes);
        return array_filter($themes_depending_on_module);
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title Overrides
ModuleRequiredByThemesUninstallValidator::$moduleExtensionList protected property The module extension list.
ModuleRequiredByThemesUninstallValidator::$themeExtensionList protected property The theme extension list.
ModuleRequiredByThemesUninstallValidator::getThemesDependingOnModule protected function Returns themes that depend on a module.
ModuleRequiredByThemesUninstallValidator::validate public function Determines the reasons a module can not be uninstalled. Overrides ModuleUninstallValidatorInterface::validate
ModuleRequiredByThemesUninstallValidator::validateConfigImport public function Determines reasons a module can not be uninstalled prior to config import. Overrides ConfigImportModuleUninstallValidatorInterface::validateConfigImport
ModuleRequiredByThemesUninstallValidator::__construct public function Constructs a new ModuleRequiredByThemesUninstallValidator.
StringTranslationTrait::$stringTranslation protected property The string translation service. 3
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.