RegistrantFactory.php in RNG - Events and Registrations 3.x
File
src/RegistrantFactory.php
View source
<?php
namespace Drupal\rng;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Entity\EntityInterface;
class RegistrantFactory implements RegistrantFactoryInterface {
protected $registrantStorage;
protected $eventManager;
public function __construct(EntityTypeManagerInterface $entity_type_manager, EventManagerInterface $event_manager) {
$this->registrantStorage = $entity_type_manager
->getStorage('registrant');
$this->eventManager = $event_manager;
}
public function createRegistrant(array $context) {
if (!isset($context['event']) || !$context['event'] instanceof EntityInterface) {
throw new \InvalidArgumentException('Registrant factory missing event context.');
}
$event = $context['event'];
$event_meta = $this->eventManager
->getMeta($event);
$event_type = $event_meta
->getEventType();
$values = [
'type' => $event_type
->getDefaultRegistrantType(),
];
$registrant = $this->registrantStorage
->create($values);
return $registrant;
}
}