I’m encountering an issue while attempting to implement a time picker within a Bricks form. Since there doesn’t appear to be a dedicated time field, I’ve used the date field twice: one for the date and the second to function as a time picker.
Following the Bricks documentation on customizing form options, I tried to apply specific settings to the second date field (intended as the time picker). However, the custom options I applied are affecting both date fields, which is not the desired behavior.
To isolate the time picker, I added a name
attribute, timePicker
, to the second date field. I then examined the output of the bricks/element/form/datepicker_options
filter to confirm that the timePicker
field was recognized. The filter output is as follows:
array(8) {
["type"]=> string(10) "datepicker"
["label"]=> string(4) "Time"
["placeholder"]=> string(13) "Select a time"
["id"]=> string(6) "putwxl"
["required"]=> bool(true)
["width"]=> string(2) "49"
["name"]=> string(10) "timePicker"
["time"]=> bool(true)
}
I implemented the following PHP code to apply custom options specifically to the timePicker
field:
<?php
add_filter('bricks/element/form/datepicker_options', function ($options, $element) {
// Retrieve the form's fields
$fields = isset($element->settings['fields']) ? $element->settings['fields'] : [];
foreach ($fields as $field) {
// Check if the field is a datepicker with the name 'timePicker'
if (isset($field['type'], $field['name']) && $field['type'] === 'datepicker' && $field['name'] === 'timePicker') {
// Apply custom options for time selection
$options = [
'enableTime' => true,
'noCalendar' => true
];
break; // Terminate the loop after finding the target field
}
}
return $options;
}, 10, 2);
Despite targeting the timePicker
field in the code, the custom options are still being applied to both date fields. This results in both fields displaying as time pickers, which is not the intended outcome.
Could you please assist me in isolating the time picker options so that they only affect the timePicker
field and not the original date field?"