diff --git a/core/core.services.yml b/core/core.services.yml index c6270f6..a5f0e7c 100644 --- a/core/core.services.yml +++ b/core/core.services.yml @@ -198,7 +198,7 @@ services: arguments: [slave] typed_data: class: Drupal\Core\TypedData\TypedDataManager - arguments: ['@container.namespaces'] + arguments: ['@container.namespaces', '@cache.cache', '@language_manager', '@module_handler'] calls: - [setValidationConstraintManager, ['@validation.constraint']] validation.constraint: diff --git a/core/lib/Drupal/Core/TypedData/Annotation/DataType.php b/core/lib/Drupal/Core/TypedData/Annotation/DataType.php index 946467f..646df4e 100644 --- a/core/lib/Drupal/Core/TypedData/Annotation/DataType.php +++ b/core/lib/Drupal/Core/TypedData/Annotation/DataType.php @@ -17,19 +17,34 @@ class DataType extends Plugin { /** - * The name of the module providing the type. + * The data type plugin ID. * * @var string */ - public $module; + public $id; /** - * The name of the data type class. + * The human-readable name of the data type. * - * This is not provided manually, it will be added by the discovery mechanism. + * @ingroup plugin_translatable * - * @var string + * @var \Drupal\Core\Annotation\Translation + */ + public $label; + + /** + * The human-readable name of the data type. + * + * @ingroup plugin_translatable + * + * @var \Drupal\Core\Annotation\Translation */ - public $class; + public $description; + /** + * The name of the list class for the data type. + * + * @var string + */ + public $list_class = '\Drupal\Core\TypedData\ItemList'; } diff --git a/core/lib/Drupal/Core/TypedData/TypedDataFactory.php b/core/lib/Drupal/Core/TypedData/TypedDataFactory.php deleted file mode 100644 index 7c37225..0000000 --- a/core/lib/Drupal/Core/TypedData/TypedDataFactory.php +++ /dev/null @@ -1,62 +0,0 @@ -discovery->getDefinition($plugin_id); - - if (!isset($type_definition)) { - throw new InvalidArgumentException(format_string('Invalid data type %plugin_id has been given.', array('%plugin_id' => $plugin_id))); - } - - // Allow per-data definition overrides of the used classes, i.e. take over - // classes specified in the data definition. - $key = empty($configuration['list']) ? 'class' : 'list_class'; - if (isset($configuration[$key])) { - $class = $configuration[$key]; - } - elseif (isset($type_definition[$key])) { - $class = $type_definition[$key]; - } - - if (!isset($class)) { - throw new PluginException(sprintf('The plugin (%s) did not specify an instance class.', $plugin_id)); - } - return new $class($configuration, $name, $parent); - } -} diff --git a/core/lib/Drupal/Core/TypedData/TypedDataManager.php b/core/lib/Drupal/Core/TypedData/TypedDataManager.php index 63c0240..512ca30 100644 --- a/core/lib/Drupal/Core/TypedData/TypedDataManager.php +++ b/core/lib/Drupal/Core/TypedData/TypedDataManager.php @@ -7,12 +7,11 @@ namespace Drupal\Core\TypedData; +use Drupal\Core\Cache\CacheBackendInterface; +use Drupal\Core\Extension\ModuleHandlerInterface; +use Drupal\Core\Language\LanguageManager; +use Drupal\Core\Plugin\DefaultPluginManager; use InvalidArgumentException; -use Drupal\Component\Plugin\Discovery\ProcessDecorator; -use Drupal\Component\Plugin\Discovery\DerivativeDiscoveryDecorator; -use Drupal\Component\Plugin\PluginManagerBase; -use Drupal\Core\Plugin\Discovery\AnnotatedClassDiscovery; -use Drupal\Core\Plugin\Discovery\CacheDecorator; use Drupal\Core\TypedData\Validation\MetadataFactory; use Drupal\Core\Validation\ConstraintManager; use Drupal\Core\Validation\DrupalTranslator; @@ -22,7 +21,7 @@ /** * Manages data type plugins. */ -class TypedDataManager extends PluginManagerBase { +class TypedDataManager extends DefaultPluginManager { /** * The validator used for validating typed data. @@ -39,34 +38,19 @@ class TypedDataManager extends PluginManagerBase { protected $constraintManager; /** - * Type definition defaults which are merged in by the ProcessDecorator. - * - * @see \Drupal\Component\Plugin\PluginManagerBase::processDefinition() - * - * @var array - */ - protected $defaults = array( - 'list_class' => '\Drupal\Core\TypedData\ItemList', - ); - - /** * An array of typed data property prototypes. * * @var array */ protected $prototypes = array(); - public function __construct(\Traversable $namespaces) { + public function __construct(\Traversable $namespaces, CacheBackendInterface $cache_backend, LanguageManager $language_manager, ModuleHandlerInterface $module_handler) { + $this->setCacheBackend($cache_backend, $language_manager, 'typed_data:types'); + $annotation_namespaces = array( 'Drupal\Core\TypedData\Annotation' => DRUPAL_ROOT . '/core/lib', ); - - $this->discovery = new AnnotatedClassDiscovery('DataType', $namespaces, $annotation_namespaces, 'Drupal\Core\TypedData\Annotation\DataType'); - $this->discovery = new DerivativeDiscoveryDecorator($this->discovery); - $this->discovery = new ProcessDecorator($this->discovery, array($this, 'processDefinition')); - $this->discovery = new CacheDecorator($this->discovery, 'typed_data:types'); - - $this->factory = new TypedDataFactory($this->discovery); + parent::__construct('DataType', $namespaces, $annotation_namespaces, 'Drupal\Core\TypedData\Annotation\DataType'); } /** @@ -88,7 +72,26 @@ public function __construct(\Traversable $namespaces) { * The instantiated typed data object. */ public function createInstance($plugin_id, array $configuration, $name = NULL, $parent = NULL) { - return $this->factory->createInstance($plugin_id, $configuration, $name, $parent); + $type_definition = $this->getDefinition($plugin_id); + + if (!isset($type_definition)) { + throw new InvalidArgumentException(format_string('Invalid data type %plugin_id has been given.', array('%plugin_id' => $plugin_id))); + } + + // Allow per-data definition overrides of the used classes, i.e. take over + // classes specified in the data definition. + $key = empty($configuration['list']) ? 'class' : 'list_class'; + if (isset($configuration[$key])) { + $class = $configuration[$key]; + } + elseif (isset($type_definition[$key])) { + $class = $type_definition[$key]; + } + + if (!isset($class)) { + throw new PluginException(sprintf('The plugin (%s) did not specify an instance class.', $plugin_id)); + } + return new $class($configuration, $name, $parent); } /** @@ -149,7 +152,7 @@ public function createInstance($plugin_id, array $configuration, $name = NULL, $ * @see \Drupal\Core\Entity\Field\EntityWrapper */ public function create(array $definition, $value = NULL, $name = NULL, $parent = NULL) { - $wrapper = $this->factory->createInstance($definition['type'], $definition, $name, $parent); + $wrapper = $this->createInstance($definition['type'], $definition, $name, $parent); if (isset($value)) { $wrapper->setValue($value, FALSE); }