You are here

public function ConstraintFactory::createInstance in Drupal 9

Creates a pre-configured instance of a plugin.

Parameters

string $plugin_id: The ID of the plugin being instantiated.

array $configuration: An array of configuration relevant to the plugin instance.

Return value

object A fully configured plugin instance.

Throws

\Drupal\Component\Plugin\Exception\PluginException If the instance cannot be created, such as if the ID is invalid.

Overrides ContainerFactory::createInstance

File

core/lib/Drupal/Core/Validation/ConstraintFactory.php, line 20

Class

ConstraintFactory
Constraint plugin factory.

Namespace

Drupal\Core\Validation

Code

public function createInstance($plugin_id, array $configuration = []) {
  $plugin_definition = $this->discovery
    ->getDefinition($plugin_id);
  $plugin_class = static::getPluginClass($plugin_id, $plugin_definition, $this->interface);

  // If the plugin provides a factory method, pass the container to it.
  if (is_subclass_of($plugin_class, ContainerFactoryPluginInterface::class)) {
    return $plugin_class::create(\Drupal::getContainer(), $configuration, $plugin_id, $plugin_definition);
  }

  // If the plugin is a Symfony Constraint, use the correct constructor.
  if (is_subclass_of($plugin_class, Constraint::class)) {
    return new $plugin_class($configuration);
  }

  // Otherwise, create the plugin as normal.
  return new $plugin_class($configuration, $plugin_id, $plugin_definition);
}