id()); $roles = $user->getRoles(); roblib_bee_limits_check_reservations($user, $form, $form_state); $values = $form_state->getValues(); $node = Node::load($values['node']); $config_factory = \Drupal::configFactory(); $bee_settings = $config_factory->get('node.type.' . $node->bundle())->get('bee'); // we only want to limit hourly reservations if($bee_settings['bookable_type'] != 'hourly') { return; } $start_date = $values['start_date']; $end_date = $values['end_date']; $start_timestamp = new \DateTime($start_date->format('Y-m-d H:i')); $end_timestamp = new \DateTime($end_date->format('Y-m-d H:i')); $minutes_diff = abs($end_timestamp->getTimestamp() - $start_timestamp->getTimestamp()) / 60; if($minutes_diff > 120) { $form_state->setError($form, t('Reservation exceeded max booking time of 2 hours.')); } } function roblib_bee_limits_check_reservations($user, $form, &$form_state) { $uid = $user->id(); $results = \Drupal::entityQuery('bat_event') ->condition('uid', $uid, '=') ->condition('type', 'availability_hourly', '=') ->execute(); $active_bookings = 0; $now = new DateTime("now"); foreach($results as $result){ $event = bat_event_load($result); if($event->getEndDate() > $now){ $active_bookings++; } $event = NULL; } $max_bookings_allowed = roblib_bee_limits_get_max_bookings_allowed($user); if($max_bookings_allowed != -1 && $active_bookings > $max_bookings_allowed){ $form_state->setError($form, t('Reservation exceeded max number of active bookings allowed.')); } } function roblib_bee_limits_get_max_bookings_allowed($user) { $roles = $user->getRoles(); $is_admin = array_search('administrator', $roles); if($is_admin) { return -1; }else { return 3; } }