public function EventForm::validateForm in Booking and Availability Management Tools for Drupal 8
Button-level validation handlers are highly discouraged for entity forms, as they will prevent entity validation from running. If the entity is going to be saved during the form submission, this method should be manually invoked from the button-level validation handler, otherwise an exception will be thrown.
Overrides ContentEntityForm::validateForm
File
- modules/
bat_event/ src/ Entity/ Form/ EventForm.php, line 176 - Contains \Drupal\bat_event\Entity\Form\EventForm.
Class
- EventForm
- Form controller for Event edit forms.
Namespace
Drupal\bat_event\Entity\FormCode
public function validateForm(array &$form, FormStateInterface $form_state) {
parent::validateForm($form, $form_state);
$entity = $this->entity;
$event_type = bat_event_type_load($entity
->bundle());
$values = $form_state
->getValues();
$start_date = new \DateTime($values['event_dates'][0]['value']
->format('Y-m-d H:i:s'));
$end_date = new \DateTime($values['event_dates'][0]['end_value']
->format('Y-m-d H:i:s'));
// The end date must be greater or equal than start date.
if ($end_date < $start_date) {
$form_state
->setErrorByName('event_dates', t('End date must be on or after the start date.'));
}
$event_type = bat_event_type_load($this->entity
->bundle());
$target_field_name = 'event_' . $event_type
->getTargetEntityType() . '_reference';
if ($event_type
->getFixedEventStates()) {
if ($values[$target_field_name][0]['target_id'] != '') {
$database = Database::getConnectionInfo('default');
$prefix = isset($database['default']['prefix']['default']) ? $database['default']['prefix']['default'] : '';
$event_store = new DrupalDBStore($this->entity
->bundle(), DrupalDBStore::BAT_EVENT, $prefix);
$end_date
->sub(new \DateInterval('PT1M'));
$bat_units = [
new Unit($values[$target_field_name][0]['target_id'], 0),
];
$calendar = new Calendar($bat_units, $event_store);
$events = $calendar
->getEvents($start_date, $end_date);
foreach ($events[$values[$target_field_name][0]['target_id']] as $event) {
$event_id = $event
->getValue();
if ($event_id != $this->entity
->id()) {
if ($event = bat_event_load($event_id)) {
$state = $event
->get('event_state_reference')->entity;
if ($state
->getBlocking()) {
$form_state
->setErrorByName('', t('Cannot save this event as an event in a blocking state exists within the same timeframe.'));
break;
}
}
}
}
}
}
}