3

On Drupal 8, in a custom entity class, I'm trying to load a repository in the constructor to be able to use custom functions in the preSave().

class MyEntity extends ContentEntityBase implements MyModuleInterface {

private $customStuff;

public function __construct(array $values, $entity_type, &$bundle = FALSE, $translations = []) {
        $this->customStuff = \Drupal::service('mymodule.mymodule_repository');
  }

(The service is defined in .services.yml in the correct module)

But I have the error :

The website encountered an unexpected error. Please try again later.Drupal\Component\Plugin\Exception\PluginNotFoundException: The "" entity type does not exist. in Drupal\Core\Entity\EntityTypeManager->getDefinition() (line 133 of core/lib/Drupal/Core/Entity/EntityTypeManager.php). Drupal\Core\Entity\EntityManager->getDefinition(NULL) (Line: 410)

How to inject custom dependence in custom entity ?

2
  • Drupal core doesn't set an error that matches the one in your question, so either that's not accurate, or a contrib/custom module is causing the problem
    – Clive
    Apr 13, 2018 at 9:56
  • I added the full stack error because yes it's an error provided by Drupal core....
    – Promo
    Apr 13, 2018 at 10:34

3 Answers 3

2

How to inject custom dependence in custom entity?

You cannot; per the Services and dependency injection documentation:

Note: It's not possible to inject services to entity object. See this issue for more details.

You'll have to get the service from \Drupal directly.

1

I forgot the take in account the inheritance of the parent __construct().

Here is the solution :

  public function __construct(array $values, $entity_type, $bundle = FALSE, $translations = []) {
    parent::__construct($values, $entity_type, $bundle, $translations);
    $this->customStuff = \Drupal::service('mymodule.mymodule_repository');
  }
0
0

Have you added the Entity annotations? E.g:

/**
 * Defines MyEntity entity.
 *
 * @ingroup my_entity
 *
 * @ContentEntityType(
 *   id = "my_entity",
 *   label = @Translation("My Entity"),
...
class MyEntity extends ContentEntityBase implements MyModuleInterface {
...

https://www.drupal.org/docs/8/api/entity-api/structure-of-an-entity-annotation

7
  • Yes annotation are there and the entity is working fine (it's a production code that I trying to improve)
    – Promo
    Apr 13, 2018 at 9:50
  • Ah ok is it the $entity_type parameter in your constructor that doesn't exist? Apr 13, 2018 at 9:53
  • Also, if your dependency can be required by composer, initialise a composer project in your module and include the autoload.php, I found that the easiest way of loading dependencies. Apr 13, 2018 at 9:54
  • I updated the error. The problem is not composer you should never load autoload in your module, i have to find the right way to load it with Drupal method.
    – Promo
    Apr 13, 2018 at 10:04
  • I've not heard of problems with autoloading in a module, have you got an article to hand explaining? If you have a mismatch of arguments, try using xdebug and trace back to the function that instantiates your class and see what is currently being passed there. I can't imagine you can change what's passed to your constructor, so you may need to amend your code to work with what Drupal gives you. Apr 13, 2018 at 10:24

Your Answer

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

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