class ExtensionExistsConstraintValidator

Same name in other branches
  1. 11.x core/lib/Drupal/Core/Extension/Plugin/Validation/Constraint/ExtensionExistsConstraintValidator.php \Drupal\Core\Extension\Plugin\Validation\Constraint\ExtensionExistsConstraintValidator

Validates that a given extension exists.

Hierarchy

Expanded class hierarchy of ExtensionExistsConstraintValidator

File

core/lib/Drupal/Core/Extension/Plugin/Validation/Constraint/ExtensionExistsConstraintValidator.php, line 17

Namespace

Drupal\Core\Extension\Plugin\Validation\Constraint
View source
class ExtensionExistsConstraintValidator extends ConstraintValidator implements ContainerInjectionInterface {
    
    /**
     * The module handler service.
     *
     * @var \Drupal\Core\Extension\ModuleHandlerInterface
     */
    protected ModuleHandlerInterface $moduleHandler;
    
    /**
     * The theme handler service.
     *
     * @var \Drupal\Core\Extension\ThemeHandlerInterface
     */
    protected ThemeHandlerInterface $themeHandler;
    
    /**
     * Constructs a ExtensionExistsConstraintValidator object.
     *
     * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
     *   The module handler service.
     * @param \Drupal\Core\Extension\ThemeHandlerInterface $theme_handler
     *   The theme handler service.
     */
    public function __construct(ModuleHandlerInterface $module_handler, ThemeHandlerInterface $theme_handler) {
        $this->moduleHandler = $module_handler;
        $this->themeHandler = $theme_handler;
    }
    
    /**
     * {@inheritdoc}
     */
    public static function create(ContainerInterface $container) {
        return new static($container->get('module_handler'), $container->get('theme_handler'));
    }
    
    /**
     * {@inheritdoc}
     */
    public function validate(mixed $extension_name, Constraint $constraint) {
        $variables = [
            '@name' => $extension_name,
        ];
        switch ($constraint->type) {
            case 'module':
                // This constraint may be used to validate nullable (optional) values.
                if ($extension_name === NULL) {
                    return;
                }
                // Some plugins are shipped in `core/lib`, which corresponds to the
                // special `core` extension name.
                // For example: \Drupal\Core\Menu\Plugin\Block\LocalActionsBlock.
                if ($extension_name === 'core') {
                    return;
                }
                if (!$this->moduleHandler
                    ->moduleExists($extension_name)) {
                    $this->context
                        ->addViolation($constraint->moduleMessage, $variables);
                }
                break;
            case 'theme':
                // This constraint may be used to validate nullable (optional) values.
                if ($extension_name === NULL) {
                    return;
                }
                if (!$this->themeHandler
                    ->themeExists($extension_name)) {
                    $this->context
                        ->addViolation($constraint->themeMessage, $variables);
                }
                break;
            default:
                throw new \InvalidArgumentException("Unknown extension type: '{$constraint->type}'");
        }
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title
ExtensionExistsConstraintValidator::$moduleHandler protected property The module handler service.
ExtensionExistsConstraintValidator::$themeHandler protected property The theme handler service.
ExtensionExistsConstraintValidator::create public static function Instantiates a new instance of this class. Overrides ContainerInjectionInterface::create
ExtensionExistsConstraintValidator::validate public function
ExtensionExistsConstraintValidator::__construct public function Constructs a ExtensionExistsConstraintValidator object.

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