View source
<?php
namespace Drupal\rng\Entity;
use Drupal\Core\Entity\ContentEntityBase;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\rng\Exception\InvalidRegistrant;
class Registrant extends ContentEntityBase implements RegistrantInterface {
public function getRegistration() {
return $this
->get('registration')->entity;
}
public function setRegistration(RegistrationInterface $registration) {
$this
->set('registration', [
'entity' => $registration,
]);
return $this;
}
public function getIdentity() {
return $this
->get('identity')->entity;
}
public function getIdentityId() {
return [
'entity_type' => $this
->get('identity')->target_type,
'entity_id' => $this
->get('identity')->target_id,
];
}
public function setIdentity(EntityInterface $entity) {
$this
->set('identity', [
'entity' => $entity,
]);
return $this;
}
public function clearIdentity() {
$this->identity
->setValue(NULL);
return $this;
}
public function hasIdentity(EntityInterface $entity) {
$keys = $this
->getIdentityId();
return $entity
->getEntityTypeId() == $keys['entity_type'] && $entity
->id() == $keys['entity_id'];
}
public function preSave(EntityStorageInterface $storage) {
if (!$this
->getRegistration()) {
throw new InvalidRegistrant('Registrant created with no registration.');
}
$event_type = $this
->getRegistration()
->getEventMeta()
->getEventType();
if ($this->event
->isEmpty()) {
$registration = $this
->getRegistration();
$this->event
->setValue([
'target_id' => $registration->event->target_id,
'target_type' => $registration->event->target_type,
]);
}
$entity = $this
->getIdentity();
if (!$entity && !$event_type
->getAllowAnonRegistrants()) {
throw new InvalidRegistrant('Registrant created with no identity, and anonymous registrants are not allowed.');
}
if (!$entity && $event_type
->getAutoAttachUsers()) {
$email_field = $event_type
->getRegistrantEmailField();
if (!$this
->get($email_field)
->isEmpty()) {
$email = $this
->get($email_field)->value;
$entity = user_load_by_mail($email);
if ($entity) {
$this
->setIdentity($entity);
}
}
}
if ($entity && $event_type
->getAutoSyncRegistrants()) {
$fields = $this
->getFields(FALSE);
$entity_fields = $entity
->getFields(FALSE);
$entity_changed = FALSE;
foreach ($fields as $name => $field) {
if (isset($entity_fields[$name])) {
if (empty($field) && !$entity_fields[$name]) {
$this
->set($name, $entity_fields[$name]);
}
elseif (empty($entity_fields[$name]) && !empty($field)) {
$entity
->set($name, $field);
$entity_changed = TRUE;
}
}
}
if ($entity_changed) {
$entity
->save();
}
}
}
public static function getRegistrantsIdsForIdentity(EntityInterface $identity) {
return \Drupal::entityQuery('registrant')
->condition('identity__target_type', $identity
->getEntityTypeId(), '=')
->condition('identity__target_id', $identity
->id(), '=')
->execute();
}
public function label() {
$identity = $this
->getIdentity();
if ($identity) {
return t('@type @id', [
'@type' => $identity
->getEntityTypeId(),
'@id' => $identity
->label(),
]);
}
$registration = $this
->getRegistration();
$pattern = $this->type->entity->label_pattern;
if (!empty($pattern)) {
$label = \Drupal::token()
->replace($pattern, [
'registrant' => $this,
'registration' => $registration,
]);
if (!empty(trim($label))) {
if (strstr($label, '[') != FALSE) {
return t(' ');
}
return $label;
}
}
if ($registration) {
return $registration
->label();
}
return t('New registrant');
}
public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
$fields = parent::baseFieldDefinitions($entity_type);
$fields['registration'] = BaseFieldDefinition::create('entity_reference')
->setLabel(t('Registration'))
->setDescription(t('The registration associated with this registrant.'))
->setSetting('target_type', 'registration')
->setCardinality(1)
->setReadOnly(TRUE);
$fields['identity'] = BaseFieldDefinition::create('dynamic_entity_reference')
->setLabel(t('Identity'))
->setDescription(t('The person associated with this registrant.'))
->setSetting('exclude_entity_types', 'true')
->setSetting('entity_type_ids', [
'registrant',
'registration',
])
->setCardinality(1)
->setReadOnly(TRUE);
$fields['event'] = BaseFieldDefinition::create('dynamic_entity_reference')
->setLabel(t('Event'))
->setDescription(t('The event for the registrant.'))
->setSetting('exclude_entity_types', 'true')
->setSetting('entity_type_ids', [
'registrant',
'registration',
])
->setDescription(t('The relationship between this registrant and an event.'))
->setRevisionable(TRUE)
->setReadOnly(TRUE);
$fields['status'] = BaseFieldDefinition::create('boolean')
->setLabel(new TranslatableMarkup('Confirmed'))
->setRevisionable(TRUE)
->setTranslatable(TRUE)
->setDefaultValue(TRUE)
->setDisplayConfigurable('form', TRUE);
return $fields;
}
public function getEvent() {
return $this
->get('event')->entity;
}
public function setEvent(ContentEntityInterface $event) {
$this
->set('event', [
'entity' => $event,
]);
return $this;
}
}