public function Registrant::preSave in RNG - Events and Registrations 8.2
Same name and namespace in other branches
- 3.x src/Entity/Registrant.php \Drupal\rng\Entity\Registrant::preSave()
If a value is set on the identity and blank on the registrant, copy values from identity to registrant, and vice-versa.
Overrides ContentEntityBase::preSave
File
- src/
Entity/ Registrant.php, line 113
Class
- Registrant
- Defines the registrant entity class.
Namespace
Drupal\rng\EntityCode
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,
]);
}
/** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
$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();
}
}
}