1

I have class like this:

<?php

namespace Drupal\mymodule;

use Drupal\comment\CommentInterface;
use Drupal\comment\CommentManagerInterface;
use Drupal\comment\CommentStorage;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Database\Connection;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityManagerInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\Session\AccountInterface;

class MyCommentStorage extends CommentStorage { 

  /**
   * {@inheritdoc}
   */
  public function __construct(EntityTypeInterface $entity_info, Connection $database, EntityManagerInterface $entity_manager, AccountInterface $current_user, CacheBackendInterface $cache, LanguageManagerInterface $language_manager) {
    parent::__construct($entity_info, $database, $entity_manager, $current_user, $cache, $language_manager);
  }

}

and my file service:

services:
  comment.storage:
    class: Drupal\mymodule\MyCommentStorage
    arguments: ['@xxx','@database', '@entity.manager', '@current_user', '@cache.entity', '@language_manager']

I don't know add @xxx service to pass first argument in my method construct.I don't find any service implement interface EntityTypeInterface. So what's service I need add to first argument?

2

An entity handler class is instantiated via EntityHandlerInterface::createInstance static method, which takes the container as the first argument and the EntityTypeInterface as the second argument.

So you would not need the service and have to use the injected container to instantiate your class.

class MyCommentStorage extends CommentStorage implements EntityHandlerInterface { 

  /**
   * {@inheritdoc}
   */
  public function __construct(EntityTypeInterface $entity_info, Connection $database, EntityManagerInterface $entity_manager, AccountInterface $current_user, CacheBackendInterface $cache, LanguageManagerInterface $language_manager) {
    parent::__construct($entity_info, $database, $entity_manager, $current_user, $cache, $language_manager);
  }

  static public function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
    return new static(
      $entity_type,
      $container->get('database'),
      // etc...
    );
  }

}
| improve this answer | |
  • thank you. But it not my answer i need. I create service because i need injection in other class. With suggest of you i still can't create my service. – Jonh Jan 6 '17 at 16:38
  • Entity handlers are not services. Entity handlers are invoked by the entity type manager via that createInstance method on the entity handler. Could you describe how you are going to use your entity storage handler? – mradcliffe Jan 6 '17 at 17:06
  • 1
    As @mradcliffe said, a entity handler is not a service. You can't define it as a service. Instead, you implement hook_entity_type_alter() and specify it there – Berdir Jan 6 '17 at 18:19
  • 3
    That said, this part of the answer is indeed missing, maybe it can be extended? See hook_entity_type_alter(), this is actually documented there – Berdir Jan 6 '17 at 18:21

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.