class DefaultFactory

Default plugin factory.

Instantiates plugin instances by passing the full configuration array as a single constructor argument. Plugin types wanting to support plugin classes with more flexible constructor signatures can do so by using an alternate factory such as Drupal\Component\Plugin\Factory\ReflectionFactory.

Hierarchy

Expanded class hierarchy of DefaultFactory

10 files declare their use of DefaultFactory
DefaultsTestPluginManager.php in drupal/core/modules/system/tests/modules/plugin_test/lib/Drupal/plugin_test/Plugin/DefaultsTestPluginManager.php
Contains Drupal\plugin_test\Plugin\DefaultsTestPluginManager.
EntityManager.php in drupal/core/lib/Drupal/Core/Entity/EntityManager.php
Contains \Drupal\Core\Entity\EntityManager.
FetcherManager.php in drupal/core/modules/aggregator/lib/Drupal/aggregator/Plugin/FetcherManager.php
Definition of Drupal\aggregator\Plugin\FetcherManager.
FormatterFactory.php in drupal/core/modules/field/lib/Drupal/field/Plugin/Type/Formatter/FormatterFactory.php
Definition of Drupal\field\Plugin\Type\Formatter\FormatterFactory.
JoinManager.php in drupal/core/modules/views/lib/Drupal/views/Plugin/Type/JoinManager.php
Definition of Drupal\views\Plugin\Type\JoinManager.

... See full list

File

drupal/core/lib/Drupal/Component/Plugin/Factory/DefaultFactory.php, line 21
Definition of Drupal\Component\Plugin\Factory\DefaultFactory.

Namespace

Drupal\Component\Plugin\Factory
View source
class DefaultFactory implements FactoryInterface {

  /**
   * The object that retrieves the definitions of the plugins that this factory instantiates.
   *
   * The plugin definition includes the plugin class and possibly other
   * information necessary for proper instantiation.
   *
   * @var Drupal\Component\Plugin\Discovery\DiscoveryInterface
   */
  protected $discovery;

  /**
   * Constructs a Drupal\Component\Plugin\Factory\DefaultFactory object.
   */
  public function __construct(DiscoveryInterface $discovery) {
    $this->discovery = $discovery;
  }

  /**
   * Implements Drupal\Component\Plugin\Factory\FactoryInterface::createInstance().
   */
  public function createInstance($plugin_id, array $configuration) {
    $plugin_class = $this
      ->getPluginClass($plugin_id);
    return new $plugin_class($configuration, $plugin_id, $this->discovery);
  }

  /**
   * Finds the class relevant for a given plugin.
   *
   *  @param array $plugin_id
   *    The id of a plugin.
   *
   *  @return string
   *    The appropriate class name.
   */
  protected function getPluginClass($plugin_id) {
    $plugin_definition = $this->discovery
      ->getDefinition($plugin_id);
    if (empty($plugin_definition['class'])) {
      throw new PluginException(sprintf('The plugin (%s) did not specify an instance class.', $plugin_id));
    }
    $class = $plugin_definition['class'];
    if (!class_exists($class)) {
      throw new PluginException(sprintf('Plugin (%s) instance class "%s" does not exist.', $plugin_id, $class));
    }
    return $class;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DefaultFactory::$discovery protected property The object that retrieves the definitions of the plugins that this factory instantiates.
DefaultFactory::createInstance public function Implements Drupal\Component\Plugin\Factory\FactoryInterface::createInstance(). Overrides FactoryInterface::createInstance 4
DefaultFactory::getPluginClass protected function Finds the class relevant for a given plugin.
DefaultFactory::__construct public function Constructs a Drupal\Component\Plugin\Factory\DefaultFactory object.