Registrant.php in RNG - Events and Registrations 8
File
src/Entity/Registrant.php
View source
<?php
namespace Drupal\rng\Entity;
use Drupal\Core\Entity\ContentEntityBase;
use Drupal\Core\Entity\EntityInterface;
use Drupal\rng\RegistrantInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\rng\RegistrationInterface;
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 array(
'entity_type' => $this
->get('identity')->target_type,
'entity_id' => $this
->get('identity')->target_id,
);
}
public function setIdentity(EntityInterface $entity) {
$this
->set('identity', array(
'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 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() {
return $this
->id() ? t('Registrant @id', [
'@id' => $this
->id(),
]) : 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', array(
'registrant',
'registration',
))
->setCardinality(1)
->setReadOnly(TRUE);
return $fields;
}
}