1 date.elements.inc | _date_html5_field_widget_form(array $field, array $instance, $langcode, array $items, $delta, array $element) |
Helper for the HTML5 widget form.
Parameters
array $field: The field structure.
array $instance: The field instance.
string $langcode: The language associated with $items.
array $items: Array of default values for this field.
int $delta: The order of this item in the array of subelements (0, 1, 2, etc).
array $element: A form element array containing basic properties for the widget.
Return value
array: The form elements for a single widget for this field.
File
- core/
modules/ date/ date.elements.inc, line 2340 - Date forms and form themes and validation.
Code
function _date_html5_field_widget_form(array $field, array $instance, $langcode, array $items, $delta, array $element) {
$field_settings = $field['settings'];
$widget_settings = $instance['widget']['settings'];
// We have to take some liberties here: only two types of "granularity",
// date with or without time. Time eventually without seconds.
$granularity = array_filter($field_settings['granularity']);
$type = empty($granularity['hour']) ? 'html_date' : 'html_datetime';
$timestep = empty($granularity['second']) ? 60 : 1;
$date_limits = _date_html5_get_date_limits($widget_settings['limits']);
$attributes = _date_html5_get_attributes($date_limits, $granularity);
$timezone_db = date_get_timezone_db($field_settings['tz_handling']);
$timezone = date_get_timezone($field_settings['tz_handling']);
if (isset($items[$delta]['timezone'])) {
$timezone = $items[$delta]['timezone'];
}
$value = '';
$has_todate = !empty($field_settings['todate']);
if ($delta == 0) {
$default = _date_html5_get_default_value($field, $instance, $langcode);
}
if (isset($items[$delta]['value'])) {
$value = _date_html5_get_form_value($items[$delta]['value'], $type, $timestep, $timezone_db, $timezone);
}
elseif ($delta == 0) {
$value = $default['start'];
}
$element['value'] = array(
'#type' => $type,
'#title' => ($has_todate) ? t('Start date') : t('Date'),
'#default_value' => $value,
'#required' => ($instance['required'] && $delta == 0),
'#attributes' => $attributes,
'#date_timezone' => $timezone,
'#timezone_db' => $timezone_db,
'#min' => $date_limits['min'],
'#max' => $date_limits['max'],
);
if ($has_todate) {
$value2 = '';
if (isset($items[$delta]['value2'])) {
$value2 = _date_html5_get_form_value($items[$delta]['value2'], $type, $timestep, $timezone_db, $timezone);
}
elseif ($delta == 0) {
$value2 = $default['end'];
}
$element['value2'] = array(
'#type' => $type,
'#title' => t('End date'),
'#default_value' => $value2,
'#required' => (($field_settings['todate'] != 'optional') && $delta == 0),
'#attributes' => $attributes,
'#date_timezone' => $timezone,
);
}
if ($field_settings['tz_handling'] == 'date') {
$element['timezone'] = array(
'#type' => 'date_timezone',
'#title' => t('Timezone'),
'#date_label_position' => NULL,
'#default_value' => isset($items[$delta]['timezone']) ? $items[$delta]['timezone'] : '',
);
}
// Prevent running again for every additional field item.
if ($delta == 0) {
$element['#element_validate'] = array('date_html5_element_validate');
$element['#attached']['css'][] = backdrop_get_path('module', 'date') . '/css/date-html5.css';
}
// Wrap in a fieldset or not, set labels accordingly.
if ($widget_settings['no_fieldset']) {
if ($field['cardinality'] == '1') {
$element['value']['#title'] = check_plain($instance['label']);
if (!empty($instance['description'])) {
$element['value']['#description'] = field_filter_xss($instance['description']);
}
}
}
else {
$element['#theme_wrappers'][] = 'fieldset';
$element['#title'] = check_plain($instance['label']);
$element['#description'] = field_filter_xss($instance['description']);
}
$element['#field_name'] = $field['field_name'];
$element['#field_type'] = $field['type'];
$element['#field_label'] = $instance['label'];
return $element;
}