function bat_date_range_fields in Booking and Availability Management Tools for Drupal 8
Same name and namespace in other branches
- 7 bat.module \bat_date_range_fields()
Utility function to create two related datepickers.
We have a few forms that need a start and end date field and we need to apply the same javascript to these forms in order to enforce specific consistent behaviours and group the form elements and javascript injection in one place.
Parameters
int $year:
int $month:
Return value
array The array holding the field definitions
2 calls to bat_date_range_fields()
- BatEventUiBulkUpdateForm::buildForm in modules/
bat_event_ui/ src/ Form/ BatEventUiBulkUpdateForm.php - Form constructor.
- UnitSetStateAction::buildForm in modules/
bat_unit/ src/ Form/ UnitSetStateAction.php - Form constructor.
File
- ./
bat.module, line 409 - Contains bat.module..
Code
function bat_date_range_fields($year = NULL, $month = NULL, $granularity = 'bat_hourly') {
$date_range_fields = [];
$config = \Drupal::config('bat.settings');
$date = new DateTime();
if ($year && $month) {
// Calculate min and max dates of the specified year/month.
$date
->setDate($year, $month, 01);
$min_date = $date
->format($config
->get('bat_daily_date_format'));
$date
->modify('last day of this month');
$max_date = $date
->format($config
->get('bat_daily_date_format'));
$extra_attributes = [
'min' => $min_date,
'max' => $max_date,
'bat-min' => $min_date,
'bat-max' => $max_date,
];
}
else {
$date
->modify('+' . $config
->get('bat_event_start_date') . ' days');
$extra_attributes = [
'min' => $date
->format($config
->get('bat_daily_date_format')),
'bat-min' => $date
->format($config
->get('bat_daily_date_format')),
];
}
$date_range_fields['bat_start_date'] = [
'#prefix' => '<div class="form-wrapper bat-date-range"><div class="start-date">',
'#suffix' => '</div>',
'#type' => 'date',
'#title' => t('Event Start'),
'#date_date_format' => $config
->get('bat_daily_date_format'),
'#attributes' => $extra_attributes + [
'type' => 'date',
'class' => [
'bat_start_date',
],
],
'#required' => TRUE,
];
$date_range_fields['bat_end_date'] = [
'#prefix' => '<div class="end-date">',
'#suffix' => '</div></div>',
'#type' => 'date',
'#title' => t('Event End'),
'#date_date_format' => $config
->get('bat_daily_date_format'),
'#attributes' => $extra_attributes + [
'type' => 'date',
'class' => [
'bat_end_date',
],
],
'#required' => TRUE,
'#attached' => [
'library' => [
'bat/bat_date_range',
],
],
];
return $date_range_fields;
}