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.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

smiletrl’s picture

Issue tags: +Field API, +typed data

Taging

Status: Needs review » Needs work

The last submitted patch, inject_typed_data.patch, failed testing.

smiletrl’s picture

Status: Needs work » Needs review
FileSize
1.67 KB
7.84 KB

fix

Status: Needs review » Needs work
Issue tags: -Field API, -typed data

The last submitted patch, inject_typed_data-3.patch, failed testing.

smiletrl’s picture

Status: Needs work » Needs review

#3: inject_typed_data-3.patch queued for re-testing.

Status: Needs review » Needs work
Issue tags: +Field API, +typed data

The last submitted patch, inject_typed_data-3.patch, failed testing.

larowlan’s picture

This 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

smiletrl’s picture

Status: Needs work » Needs review
Issue tags: +API change
FileSize
1.43 KB
8.81 KB

Indeed, 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.

smiletrl’s picture

Status: Needs review » Closed (duplicate)