function ExtensionAvailableConstraintValidator::validate

File

core/lib/Drupal/Core/Extension/Plugin/Validation/Constraint/ExtensionAvailableConstraintValidator.php, line 79

Class

ExtensionAvailableConstraintValidator
Validates that a given extension exists.

Namespace

Drupal\Core\Extension\Plugin\Validation\Constraint

Code

public function validate(mixed $extension_name, Constraint $constraint) : void {
  assert($constraint instanceof ExtensionAvailableConstraint);
  // This constraint may be used to validate nullable (optional) values.
  if ($extension_name === NULL) {
    return;
  }
  $variables = [
    '@name' => $extension_name,
  ];
  $data = $this->context
    ->getObject();
  assert($data instanceof TypedDataInterface);
  $validation_profile = $this->resolveProfile($data);
  $extension_discovery = NULL;
  // If the profile is changing, the list of available extensions can change.
  // The ExtensionDiscovery object is used directly instead of ExtensionList
  // objects to avoid polluting caches and state.
  // @see \Drupal\Core\Extension\ExtensionDiscovery
  // @see \Drupal\Core\Extension\InstallProfileUninstallValidator
  $installProfile = $this->installProfile;
  if ($validation_profile !== $installProfile) {
    try {
      $extension_discovery = $this->getExtensionDiscovery($validation_profile);
    } catch (UnknownExtensionException) {
      // The new profile passed is not available. If we are validating a
      // profile, show the profile missing message.
      if ($constraint->type === 'profile') {
        $this->context
          ->addViolation($constraint->profileNotExistsMessage, $variables);
        return;
      }
      // If we are validating a module or theme, show we cannot load the
      // profile to check if the module or theme is available.
      $params = [
        '@profile' => $validation_profile,
        '@extension' => $extension_name,
      ];
      $this->context
        ->addViolation($constraint->couldNotLoadProfileToCheckExtension, $params);
      return;
    }
  }
  switch ($constraint->type) {
    case 'module':
      // 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 a profile is set, that profile is also available as a module.
      if ($extension_name === $validation_profile) {
        return;
      }
    // Intentionally fall through to the next cases.
    case 'theme':
    case 'profile':
      if (!$this->extensionExists($constraint->type, $extension_name, $extension_discovery)) {
        $message = $constraint->type . 'NotExistsMessage';
        assert(property_exists($constraint, $message));
        $this->context
          ->addViolation($constraint->{$message}, $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.