Motivation
Now all typed data class could be instantiated via TypedDataManager::createInstance
if (!isset($class)) {
throw new PluginException(sprintf('The plugin (%s) did not specify an instance class.', $plugin_id));
}
return new $class($configuration, $name, $parent);
It would be better if these typed data could have option to inject their dependencies. For example, FieldItem class, we want to inject TypedDataManager service, since it's been used a lot, especially for FieldItem::getConstraints().
if (!empty($this->definition['property_constraints'])) {
$constraints[] = \Drupal::typedData()->getValidationConstraintManager()->create('ComplexData', $this->definition['property_constraints']);
}
Solution
Introduce a new interface TypedDataInjectionInterface, which contains only one factory method, i.e., create() to inject typed data's dependencies.
Comments
Comment #1
smiletrl commentedTaging
Comment #3
smiletrl commentedfix
Comment #5
smiletrl commented#3: inject_typed_data-3.patch queued for re-testing.
Comment #7
larowlanThis is a duplicate, on my phone so can't find the other one but will link to it on Monday. We got the other one green but it was doing too much serialization but then it got stuck
Comment #8
smiletrl commentedIndeed, it's the serialization issue. This time it should be fine:)
This patch provides a solution to this kind of issue.
Ideally, class which injects its dependencies should extend abstract class DependencySerialization. However, FieldItemBase has already extended Map. Php doesn't allow mulitple inheritances, so I simply copy code from that class here, with minor change.
Comment #9
smiletrl commentedSee #2053415-33: Allow typed data plugins to receive injected dependencies.