2

I'm creating a few custom classes that extend FieldItemList and they need some services injected.

I've had a look at core classes that extend that class and none of them do dependency injection, they all call \Drupal::service().

Is there a way to inject services here or should I follow the core pattern?

3

1 Answer 1

1

The field type plugin manager instantiates both, FieldItem and FieldItemList via the typed data manager:

Drupal\Core\Field\FieldTypePluginManager

  public function createFieldItemList(FieldableEntityInterface $entity, $field_name, $values = NULL) {
    // Leverage prototyping of the Typed Data API for fast instantiation.
    return $this->typedDataManager->getPropertyInstance($entity->getTypedData(), $field_name, $values);
  }

  public function createFieldItem(FieldItemListInterface $items, $index, $values = NULL) {
    // Leverage prototyping of the Typed Data API for fast instantiation.
    return $this->typedDataManager->getPropertyInstance($items, $index, $values);
  }

To be able to inject services you need to wait until this issue Allow typed data plugins to receive injected dependencies is resolved.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.