Commit 745f2829 authored by droces's avatar droces

General functionality fixes, and season validation

parent b4e0ccb0
......@@ -171,19 +171,21 @@ function abookings_node_form_validate($form, FormStateInterface &$form_state) {
// kint($form, '$form');
// kint($form_state, '$form_state');
$booking = $form_state->getFormObject()->getEntity();
// kint($booking, '$booking');
// Validate promotion
$promo_code_provided = $form_state->getValue('field_promo_code_provided')[0]['value'];
$checkin_date = $form_state->getValue('field_checkin_date')[0]['value'];
$nights = $form_state->getValue('field_num_nights')[0]['value'];
if ($promo_code_provided) {
$current_promo = $booking->get('field_promo_code_provided')->getValue()[0];
if ($promo_code_provided && empty($current_promo)) {
validate_promo($form_state, $promo_code_provided, $checkin_date, $nights);
}
$booking = $form_state->getFormObject()->getEntity();
// kint($booking, '$booking');
$bookable_nid_val = $form_state->getValue('field_bookable_unit');
// kint($bookable_nid_val, '$bookable_nid_val');
if (! $bookable_nid_val) {
......@@ -250,15 +252,18 @@ function validate_promo($form_state, $promo_code_provided, $checkin_date, $night
function save_promo_line_item($code, $amount, $booking, $date) {
// kint($code, '$code');
// kint($booking, '$booking');
// kint($date, '$date');
// Create node object with attached file.
$promo = Node::create([
'type' => 'line_item',
'title' => 'Promo ' . $code . '-' . $booking,
'title' => 'Promo ' . $code . '-' . $booking->getTitle(),
'field_type' => 'promotion',
'field_amount' => $amount,
'field_booking' => $booking,
'field_date' => $date,
'field_date' => $date->format('Y-m-d H:i:s'),
]);
// kint($promo, '$promo');
$promo->save();
......@@ -686,3 +691,27 @@ function get_bookings_data() {
// kint($content['#attached']['drupalSettings']['seasons'], 'seasons_data settings');
return $bookings_renderable;
}
/**
* Implements hook_form_FORM_ID_alter().
*/
function abookings_form_node_season_edit_form_alter(&$form,
\Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
$form['#validate'][] = 'validate_season';
}
function validate_season(array &$form, FormStateInterface $form_state) {
// kint($form, '$form');
// kint($form_state, '$form_state');
// Get dates as unix timestamps
$start_date = $form_state->getValue('field_start_date')[0]['value']->format('U');
$end_date = $form_state->getValue('field_end_date')[0]['value']->format('U');
if ($start_date > $end_date) {
$msg = 'End date must be after start date';
$form_state->setErrorByName('field_start_date');
$form_state->setErrorByName('field_end_date', t($msg));
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment