Entities might want to use services, and it should be possible to provide them via dependency injection.
The way should be at least as easy or similar like classes implementing ContainerFactoryPluginInterface.

If there's already a standard way for this, please provide a documentation resource about it. Documentation might be lacking here and this issue might then turn into a documentation issue.

One possible way would be the definition of custom entity handlers via EntityHandlerInterface, but I think it's more convenient and sometimes does make sense that the entity uses a service directly. Thoughts?


mxh created an issue. See original summary.

mxh’s picture

Issue summary: View changes
mxh’s picture

Maybe this is something for contrib Entity API, not core. Not sure about it.

dawehner’s picture

I remember when we tried adding support for proper dependency injection for entities. This though resulted in a lot of really hard issues with forms and what not. Sadly I cannot find the issue right now.

vijaycs85’s picture

@dawehner just linked this issue on DIC documentation page(As a warning under Accessing services in objects using dependency injection section). Please update, if you manage to find the original issue on doc page as well.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.0-alpha1 will be released the week of January 17, 2018, which means new developments and disruptive changes should now be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.