function ExtensionExistsConstraintValidator::validate

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::validate()

File

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

Class

ExtensionExistsConstraintValidator
Validates that a given extension exists.

Namespace

Drupal\Core\Extension\Plugin\Validation\Constraint

Code

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}'");
    }
}

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