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.
Comment | File | Size | Author |
---|---|---|---|
#8 | inject_typed_data-8.patch | 8.81 KB | smiletrl |
#8 | intderdiff-3-8.txt | 1.43 KB | smiletrl |
#3 | inject_typed_data-3.patch | 7.84 KB | smiletrl |
#3 | interdiff.txt | 1.67 KB | smiletrl |
inject_typed_data.patch | 7.84 KB | smiletrl | |
Comments
Comment #1
smiletrl CreditAttribution: smiletrl commentedTaging
Comment #3
smiletrl CreditAttribution: smiletrl commentedfix
Comment #5
smiletrl CreditAttribution: 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 CreditAttribution: 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 CreditAttribution: smiletrl commentedSee #2053415-33: Allow typed data plugins to receive injected dependencies.