class InstallProfileUninstallValidator

Same name in other branches
  1. 10 core/lib/Drupal/Core/ProxyClass/Extension/InstallProfileUninstallValidator.php \Drupal\Core\ProxyClass\Extension\InstallProfileUninstallValidator
  2. 10 core/lib/Drupal/Core/Extension/InstallProfileUninstallValidator.php \Drupal\Core\Extension\InstallProfileUninstallValidator

Ensures install profile can only be uninstalled if the modules are available.

Hierarchy

Expanded class hierarchy of InstallProfileUninstallValidator

File

core/lib/Drupal/Core/Extension/InstallProfileUninstallValidator.php, line 13

Namespace

Drupal\Core\Extension
View source
class InstallProfileUninstallValidator implements ModuleUninstallValidatorInterface {
    use StringTranslationTrait;
    
    /**
     * Extension discovery that scans all folders except profiles.
     *
     * @var \Drupal\Core\Extension\ExtensionDiscovery
     */
    protected ExtensionDiscovery $noProfileExtensionDiscovery;
    public function __construct(TranslationInterface $string_translation, ModuleExtensionList $moduleExtensionList, ThemeExtensionList $themeExtensionList, string|false|null $installProfile, string $root, string $sitePath) {
        $this->setStringTranslation($string_translation);
    }
    
    /**
     * {@inheritdoc}
     */
    public function validate($module) : array {
        $reasons = [];
        // When there are modules installed that only exist in the install profile's
        // directory an install profile can not be uninstalled.
        if ($module === $this->installProfile) {
            $profile_name = $this->moduleExtensionList
                ->get($module)->info['name'];
            $profile_only_modules = array_diff_key($this->moduleExtensionList
                ->getAllInstalledInfo(), $this->getExtensionDiscovery()
                ->scan('module'));
            // Remove the install profile as we're uninstalling it.
            unset($profile_only_modules[$module]);
            if (!empty($profile_only_modules)) {
                $reasons[] = $this->t("The install profile '@profile_name' is providing the following module(s): @profile_modules", [
                    '@profile_name' => $profile_name,
                    '@profile_modules' => implode(', ', array_keys($profile_only_modules)),
                ]);
            }
            $profile_only_themes = array_diff_key($this->themeExtensionList
                ->getAllInstalledInfo(), $this->getExtensionDiscovery()
                ->scan('theme'));
            if (!empty($profile_only_themes)) {
                $reasons[] = $this->t("The install profile '@profile_name' is providing the following theme(s): @profile_themes", [
                    '@profile_name' => $profile_name,
                    '@profile_themes' => implode(', ', array_keys($profile_only_themes)),
                ]);
            }
        }
        elseif (!empty($this->installProfile)) {
            $extension = $this->moduleExtensionList
                ->get($module);
            // Ensure that the install profile does not depend on the module being
            // uninstalled.
            if (isset($extension->required_by[$this->installProfile])) {
                $profile_name = $this->moduleExtensionList
                    ->get($this->installProfile)->info['name'];
                $reasons[] = $this->t("The '@profile_name' install profile requires '@module_name'", [
                    '@profile_name' => $profile_name,
                    '@module_name' => $extension->info['name'],
                ]);
            }
        }
        return $reasons;
    }
    
    /**
     * Gets an extension discovery object that ignores the install profile.
     *
     * @return \Drupal\Core\Extension\ExtensionDiscovery
     *   An extension discovery object to look for extensions not in a profile
     *   directory.
     */
    protected function getExtensionDiscovery() : ExtensionDiscovery {
        if (!isset($this->noProfileExtensionDiscovery)) {
            // cspell:ignore CNKDSIUSYFUISEFCB
            $this->noProfileExtensionDiscovery = new ExtensionDiscovery($this->root, TRUE, [
                '_does_not_exist_profile_CNKDSIUSYFUISEFCB',
            ], $this->sitePath);
        }
        return $this->noProfileExtensionDiscovery;
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title Overrides
InstallProfileUninstallValidator::$noProfileExtensionDiscovery protected property Extension discovery that scans all folders except profiles.
InstallProfileUninstallValidator::getExtensionDiscovery protected function Gets an extension discovery object that ignores the install profile.
InstallProfileUninstallValidator::validate public function Determines the reasons a module can not be uninstalled. Overrides ModuleUninstallValidatorInterface::validate
InstallProfileUninstallValidator::__construct public function
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.