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 EntityTypeManagerInterface
as 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' }
services.yml
file uparguments: ['@entity_type.manager', '@...', '@...']
EntityTypeManagerInterface
issue. Thanks! Where did you come up withentity_type.manager
instead ofentity_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 theservices.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.core.services.yml
. The second service is from a module, so you find this inrng.services.yml
of that module.