I have an entity with a JSON field, and I do some complicated processing when the entity is saved.

Right now, I do this processing in hook_ENTITY_presave(), which calls a service I have created in a custom module.

However, since Drupal 9.3, we can create bundle classes to extend entities cleanly.

So I made one for my entity, and I wrote some new getters and setters so I can do $entity->getMyValue() instead of $entity->field_abc->value.

Now, I want to take the next step and inject my service into the entity and move the processing directly into the entity on save. How can I do that?


1 Answer 1


Alas, this is not yet possible. There is a discussion in the Core issue queue on how to proceed with this.

As noted in the discussion linked above, an alternative to do this now is to use the Typed Entity module, which wraps entities into objects and allows you to do dependency injection.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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