class TypedDataFactory

A factory for typed data objects.

The factory incorporates list classes if the typed data is a list as well as class overrides that are specified in data definitions.

Hierarchy

Expanded class hierarchy of TypedDataFactory

File

drupal/core/lib/Drupal/Core/TypedData/TypedDataFactory.php, line 19
Definition of Drupal\Core\TypedData\TypedDataFactory.

Namespace

Drupal\Core\TypedData
View source
class TypedDataFactory extends DefaultFactory {

  /**
   * Implements Drupal\Component\Plugin\Factory\FactoryInterface::createInstance().
   *
   * @param string $plugin_id
   *   The id of a plugin, i.e. the data type.
   * @param array $configuration
   *   The plugin configuration, i.e. the data definition.
   *
   * @return Drupal\Core\TypedData\TypedDataInterface
   */
  public function createInstance($plugin_id, array $configuration) {
    $type_definition = $this->discovery
      ->getDefinition($plugin_id);

    // Allow per-data definition overrides of the used classes and generally
    // default to the data type definition.
    $definition = $configuration + $type_definition;
    if (empty($definition['list'])) {
      if (empty($definition['class'])) {
        throw new PluginException(sprintf('The plugin (%s) did not specify an instance class.', $plugin_id));
      }
      $plugin_class = $definition['class'];
    }
    else {
      if (empty($definition['list class'])) {
        throw new PluginException(sprintf('The plugin (%s) did not specify a list instance class.', $plugin_id));
      }
      $plugin_class = $definition['list class'];
    }
    return new $plugin_class($definition, $plugin_id, $this->discovery);
  }

}

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::getPluginClass protected function Finds the class relevant for a given plugin.
DefaultFactory::__construct public function Constructs a Drupal\Component\Plugin\Factory\DefaultFactory object.
TypedDataFactory::createInstance public function Implements Drupal\Component\Plugin\Factory\FactoryInterface::createInstance(). Overrides DefaultFactory::createInstance