0

I am trying to write a custom eventsubscriber for commerce 2.x for an event after a successful checkout. Everything works and now I want to create a __construct function to initialize an object, but then I get the following error:

TypeError: Argument 1 passed to Drupal\custom_module\EventSubscriber\OrderCompleteEventSubscriber::__construct() must implement interface Drupal\Core\Entity\EntityTypeManagerInterface, none given, called in /home/user/websiteName/docroot/core/lib/Drupal/Component/DependencyInjection/Container.php on line 264 in Drupal\custom_module\EventSubscriber\OrderCompleteEventSubscriber->__construct()

I've tried explicitly defining the EntityTypeManagerInterface within the class, but everything is resulting in this error. Not finding anyone with any similar issues, and this particular block of code works for other people, just wondering what I am missing or If I am doing something incorrect. This code runs great if I don't construct an object with EntityTypeManagerInterfaceas the argument ... any ideas?

namespace Drupal\custom_module\EventSubscriber;

use Drupal\commerce_order\Entity\OrderInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Session\AccountProxyInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\rng\RegistrantFactoryInterface;
use Drupal\state_machine\Event\WorkflowTransitionEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;


class OrderCompleteEventSubscriber implements EventSubscriberInterface {

  use StringTranslationTrait;

  /**
   * The registration entity storage.
   *
   * @var \Drupal\Core\Entity\EntityStorageInterface
   */
  protected $registrationStorage;

  /**
   * The user entity storage.
   *
   * @var \Drupal\user\UserStorage
   */
  protected $userStorage;

  /**
   * The registration access handler.
   *
   * @var \Drupal\Core\Entity\EntityAccessControlHandlerInterface
   */
  protected $registrationAccessHandler;

  /**
   * The registrant factory.
   *
   * @var \Drupal\rng\RegistrantFactoryInterface
   */
  protected $registrantFactory;

  /**
   * Drupal\Core\Session\AccountProxyInterface definition.
   *
   * @var \Drupal\Core\Session\AccountProxyInterface
   */
  protected $currentUser;

  /**
   * Constructs a new OrderCompleteEventSubscriber object.
   *
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   * @param \Drupal\rng\RegistrantFactoryInterface $registrantFactory
   * @param \Drupal\Core\Session\AccountProxyInterface $current_user
   */
  public function __construct(EntityTypeManagerInterface $entity_type_manager, RegistrantFactoryInterface $registrantFactory, AccountProxyInterface $current_user) {
    $this->registrationStorage = $entity_type_manager->getStorage('registration');
    $this->registrationAccessHandler = $entity_type_manager->getAccessControlHandler('registration');
    $this->userStorage = $entity_type_manager->getStorage('user');
    $this->registrantFactory = $registrantFactory;
    $this->currentUser = $current_user;
  }

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {
    return [
      'commerce_order.place.post_transition' => ['buildRegistration'],
    ];
  }

  /**
   *
   * @param \Drupal\state_machine\Event\WorkflowTransitionEvent $event
   */
  public function buildRegistration(WorkflowTransitionEvent $event) {
    $order = $event->getEntity();
    // do stuff
      }
    }
  }

}

Services.yml File

services:
   custom_module_event_subscriber:
      class: '\Drupal\custom_module\EventSubscriber\OrderCompleteEventSubscriber'
      tags:
        - { name: 'event_subscriber' }
5
  • 1
    Probably the arguments in the service definition are missing, can you show the content of the *.services.yml file?
    – 4uk4
    Sep 19, 2017 at 16:10
  • @4k4 that might be it, I did not provide arguments in my services file :P I have put my services.yml file up
    – DMAC
    Sep 19, 2017 at 16:16
  • 1
    yes, you need this line: arguments: ['@entity_type.manager', '@...', '@...']
    – 4uk4
    Sep 19, 2017 at 16:21
  • @4k4 That worked and cleared up the issue with the EntityTypeManagerInterface issue. Thanks! Where did you come up with entity_type.manager instead of entity_type_manager? I am going through the docs and am not finding too many useful things on arguments. My second argument is now throwing an error saying the services has a dependency on a non-existent service, but when i take it out of the services.yml, the program white screens with the error saying I need my second argument to be of that type which symphony says it doesn't exist so I can't clear caches.
    – DMAC
    Sep 19, 2017 at 16:34
  • 1
    The services are named arbitrarily, you have to look them up. You find the core services in core.services.yml. The second service is from a module, so you find this in rng.services.yml of that module.
    – 4uk4
    Sep 19, 2017 at 16:44

1 Answer 1

1

A bit late but the code from EventSubscriber has three arguments so your services.yml file should read:

services:
  custom_module.order_complete:
    class: Drupal\custom_module\EventSubscriber\PurchaseBuildRegistration
    arguments: ['@entity_type.manager', '@rng.registrant.factory', '@current_user']
tags:
  - { name: event_subscriber }

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.