function ReflectionFactory::createInstance

Same name in other branches
  1. 9 core/lib/Drupal/Component/Plugin/Factory/ReflectionFactory.php \Drupal\Component\Plugin\Factory\ReflectionFactory::createInstance()
  2. 8.9.x core/lib/Drupal/Component/Plugin/Factory/ReflectionFactory.php \Drupal\Component\Plugin\Factory\ReflectionFactory::createInstance()
  3. 11.x core/lib/Drupal/Component/Plugin/Factory/ReflectionFactory.php \Drupal\Component\Plugin\Factory\ReflectionFactory::createInstance()

Overrides DefaultFactory::createInstance

File

core/lib/Drupal/Component/Plugin/Factory/ReflectionFactory.php, line 16

Class

ReflectionFactory
A plugin factory that maps instance configuration to constructor arguments.

Namespace

Drupal\Component\Plugin\Factory

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);
    // Lets figure out of there's a constructor for this class and pull
    // arguments from the $options array if so to populate it.
    $reflector = new \ReflectionClass($plugin_class);
    if ($reflector->hasMethod('__construct')) {
        $arguments = $this->getInstanceArguments($reflector, $plugin_id, $plugin_definition, $configuration);
        $instance = $reflector->newInstanceArgs($arguments);
    }
    else {
        $instance = new $plugin_class();
    }
    return $instance;
}

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