class EnabledExtensionsValidator

Validates no enabled Drupal extensions are removed from the stage directory.

@internal This is an internal part of Package Manager and may be changed or removed at any time without warning. External code should not interact with this class.

Hierarchy

Expanded class hierarchy of EnabledExtensionsValidator

File

core/modules/package_manager/src/Validator/EnabledExtensionsValidator.php, line 23

Namespace

Drupal\package_manager\Validator
View source
final class EnabledExtensionsValidator implements EventSubscriberInterface {
    use StringTranslationTrait;
    public function __construct(PathLocator $pathLocator, ModuleHandlerInterface $moduleHandler, ComposerInspector $composerInspector, ThemeHandlerInterface $themeHandler) {
    }
    
    /**
     * Validates that no enabled Drupal extensions have been removed.
     *
     * @param \Drupal\package_manager\Event\PreApplyEvent $event
     *   The event object.
     */
    public function validate(PreApplyEvent $event) : void {
        $active_packages_list = $this->composerInspector
            ->getInstalledPackagesList($this->pathLocator
            ->getProjectRoot());
        $stage_packages_list = $this->composerInspector
            ->getInstalledPackagesList($event->stage
            ->getStageDirectory());
        $extensions_list = $this->moduleHandler
            ->getModuleList() + $this->themeHandler
            ->listInfo();
        foreach ($extensions_list as $extension) {
            $extension_name = $extension->getName();
            $package = $active_packages_list->getPackageByDrupalProjectName($extension_name);
            if ($package && $stage_packages_list->getPackageByDrupalProjectName($extension_name) === NULL) {
                $removed_project_messages[] = t("'@name' @type (provided by <code>@package</code>)", [
                    '@name' => $extension_name,
                    '@type' => $extension->getType(),
                    '@package' => $package->name,
                ]);
            }
        }
        if (!empty($removed_project_messages)) {
            $removed_packages_summary = $this->formatPlural(count($removed_project_messages), 'The update cannot proceed because the following enabled Drupal extension was removed during the update.', 'The update cannot proceed because the following enabled Drupal extensions were removed during the update.');
            $event->addError($removed_project_messages, $removed_packages_summary);
        }
    }
    
    /**
     * {@inheritdoc}
     */
    public static function getSubscribedEvents() : array {
        return [
            PreApplyEvent::class => 'validate',
        ];
    }

}

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