Created
July 6, 2017 17:52
-
-
Save heddn/119a439ead6189ddd6c835aa65c7348e to your computer and use it in GitHub Desktop.
RNG Commerce integration
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
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 DepositBuildRegistration. | |
* | |
* @package Drupal\custom_module | |
*/ | |
class PurchaseBuildRegistration 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 OrderReceiptSubscriber 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'], | |
]; | |
} | |
/** | |
* Build the registration entry. | |
* | |
* @param \Drupal\state_machine\Event\WorkflowTransitionEvent $event | |
*/ | |
public function buildRegistration(WorkflowTransitionEvent $event) { | |
$order = $event->getEntity(); | |
/** @var \Drupal\commerce_order\Entity\OrderInterface $order */ | |
foreach ($order->getItems() as $orderItem) { | |
/** @var \Drupal\commerce_product\Entity\ProductVariationInterface $variation */ | |
$variation = $orderItem->getPurchasedEntity(); | |
$context = ['event' => $variation->getProduct()]; | |
// Check if there is already a registration for the event with this | |
// user account. | |
if ($this->registrationAccessHandler->createAccess(NULL, $this->currentUser->getAccount(), $context)) { | |
$this->createRegistration($context); | |
} | |
} | |
} | |
/** | |
* Create an event registration. | |
* | |
* @param array $context | |
*/ | |
protected function createRegistration(array $context) { | |
/** @var \Drupal\user\UserInterface $user */ | |
$user = $this->userStorage->load($this->currentUser->getAccount()->id()); | |
/** @var \Drupal\rng\RegistrantInterface $registrant */ | |
$registrant = $this->registrantFactory->createRegistrant($context); | |
$registrant->setIdentity($user); | |
/** @var \Drupal\rng\RegistrationInterface $registration */ | |
$registration = $this->registrationStorage->create(['type' => 'registration']); | |
$registration->setEvent($context['event']); | |
if (!$registration->hasIdentity($registrant)) { | |
$registration->save(); | |
$registrant->setRegistration($registration); | |
$registrant->save(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment