0

I have a module that defines two services. Service A has service B as dependency.

Service B has some dependencies, one of which is entity type manager.

In the constructor of the service B an entity storage is being called for entity type that is defined by this module as well

public function __construct(AccountProxy $user, EntityTypeManager $entity_manager, QueryFactory $entity_query) {
    $this->user = $user;
    $this->storage = $entity_manager->getStorage('entity_type_name');
    $this->query = $entity_query->get('entity_type_name');
  }

During installation I get error about unknown entity and the installation breaks. I have tried to backtrace the cause of this but so far it looks like everything is in order. The container is only parsing the service definitions and instantiates them when needed. What I think might be the issue is the order in which the entity definitions are processed and service definitions parsed.

I do not want to add any condition into service B constructor to check if installation is in progress. I think this should not be happening.

What would be a proper workaround?

1

While it will require an additional call when you require it, you could just store a reference to the entity type manager only.

Especially for entity query, you must not do it like this anyway. A query object is not re-usable. You can't execute multiple queries from a single instance.

Additionally, you can simplify your dependencies if you call $storage->getQuery() instead of using the entity query for that (which does the same internally).

| improve this answer | |
  • Thanks, that sound like a good approach. I'll do that. I haven't used EQ from storage because I always forget which EQ will load the objects right away instead of only entity IDs. But this is one and the same query so it's fine. – user21641 Mar 14 '16 at 8:32
  • Yes, there is only one entity query API, just two different entry points. – Berdir Mar 14 '16 at 9:01

Your Answer

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