public function Event::save in Booking and Availability Management Tools for Drupal 8
Saves an entity permanently.
When saving existing entities, the entity is assumed to be complete, partial updates of entities are not supported.
Return value
int Either SAVED_NEW or SAVED_UPDATED, depending on the operation performed.
Throws
\Drupal\Core\Entity\EntityStorageException In case of failures an exception is thrown.
Overrides EntityBase::save
File
- modules/
bat_event/ src/ Entity/ Event.php, line 189 - Contains \Drupal\bat_event\Entity\Event.
Class
- Event
- Defines the Event entity.
Namespace
Drupal\bat_event\EntityCode
public function save() {
$langcode = $this->defaultLangcode;
$event_type = bat_event_type_load($this
->bundle());
// Construct target entity reference field name using this event type's target entity type.
$target_field_name = 'event_' . $event_type
->getTargetEntityType() . '_reference';
// We are going to be updating the event - so the first step is to remove
// the old event.
if (!$this
->isNew()) {
$entity_original = \Drupal::entityTypeManager()
->getStorage('bat_event')
->loadUnchanged($this
->id());
if ($entity_original
->getStartDate() != '' && $entity_original
->getEndDate() != '' && $entity_original
->getTranslation($langcode)
->get($target_field_name) !== FALSE) {
// Get the referenced entity ID.
$event_target_entity_reference = $entity_original
->getTranslation($langcode)
->get($target_field_name)
->getValue();
$target_entity_id = 0;
if (isset($event_target_entity_reference[0]['target_id'])) {
$target_entity_id = $event_target_entity_reference[0]['target_id'];
}
// Load the referenced entity.
if ($target_entity = \Drupal::entityTypeManager()
->getStorage($event_type
->getTargetEntityType())
->load($target_entity_id)) {
$unit = new Unit($target_entity_id, $target_entity
->getEventDefaultValue($event_type
->id()));
$this
->batStoreSave($unit, $entity_original
->getStartDate(), $entity_original
->getEndDate()
->sub(new \DateInterval('PT1M')), $event_type
->id(), $event_type
->getEventGranularity(), $unit
->getDefaultValue(), $this
->get('id')->value, TRUE);
}
}
}
parent::save();
// Now we store the new event.
if ($this
->getTranslation($langcode)
->get($target_field_name) !== FALSE) {
$event_value = '';
if (isset($event_type->default_event_value_field_ids)) {
$field = $event_type->default_event_value_field_ids;
$field_info = FieldStorageConfig::loadByName('bat_event', $field);
$values = $this
->getTranslation($langcode)
->get($field)
->getValue();
if (!empty($values)) {
if ($field_info
->getType() == 'entity_reference') {
$event_value = $values[0]['target_id'];
}
elseif ($field_info
->getType() == 'commerce_price') {
$event_value = $values[0]['number'];
}
elseif ($field_info
->getType() == 'text' || $field_info
->getType() == 'string' || $field_info
->getType() == 'number_integer') {
$event_value = $values[0]['value'];
}
}
}
else {
$event_state_reference = $this
->getTranslation($langcode)
->get('event_state_reference')
->getValue();
$event_value = $event_state_reference[0]['target_id'];
}
$event_target_entity_reference = $this
->getTranslation($langcode)
->get($target_field_name);
$target_entity_id = $event_target_entity_reference
->referencedEntities()[0]
->id();
if ($target_entity = \Drupal::entityTypeManager()
->getStorage($event_type
->getTargetEntityType())
->load($target_entity_id)) {
$unit = new Unit($target_entity_id, $target_entity
->getEventDefaultValue($event_type
->id()));
$this
->batStoreSave($unit, $this
->getStartDate(), $this
->getEndDate()
->sub(new \DateInterval('PT1M')), $event_type
->id(), $event_type
->getEventGranularity(), $event_value, $this
->get('id')->value);
}
}
}