1 date.elements.inc | _date_default_field_widget_form(array $field, array $instance, $langcode, array $items, $delta, array $base) |
Helper for the default 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 $base: 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 2244 - Date forms and form themes and validation.
Code
function _date_default_field_widget_form(array $field, array $instance, $langcode, array $items, $delta, array $base) {
$element = $base;
// If this is a new entity, populate the field with the right default values.
// This happens early so even fields later hidden with #access get those
// values. We should only add default values to new entities, to avoid
// over-writing a value that has already been set. This means we can't just
// check to see if $items is empty, because it might have been set that way on
// purpose.
// @see date_field_widget_properties_alter() where we flagged if this is a new entity.
//
// We check !isset($items[$delta]['value']) because entity translation may
// create a new translation entity for an existing entity and we don't want to
// clobber values that were already set in that case.
// @see http://drupal.org/node/1478848.
$is_default = FALSE;
if (!isset($items[$delta]['value'])) {
if (!empty($instance['widget']['is_new'])) {
// New entity; use default values defined on instance.
$items = date_default_value($field, $instance, $langcode);
$is_default = TRUE;
}
else {
// Date is empty; create array structure for value keys.
$keys = date_process_values($field);
$items[$delta] = array_fill_keys($keys, '');
}
}
$timezone = date_get_timezone($field['settings']['tz_handling'], isset($items[$delta]['timezone']) ? $items[$delta]['timezone'] : date_default_timezone());
// TODO see if there's a way to keep the timezone element from ever being
// nested as array('timezone' => 'timezone' => value)). After struggling
// with this a while, I can find no way to get it displayed in the form
// correctly and get it to use the timezone element without ending up
// with nesting.
if (is_array($timezone)) {
$timezone = $timezone['timezone'];
}
$element += array(
'#type' => 'date_combo',
'#theme_wrappers' => array('date_combo'),
'#weight' => $delta,
'#default_value' => isset($items[$delta]) ? $items[$delta] : '',
'#date_timezone' => $timezone,
'#element_validate' => array('date_combo_validate'),
'#date_is_default' => $is_default,
// Store the original values, for use with disabled and hidden fields.
'#date_items' => isset($items[$delta]) ? $items[$delta] : '',
);
if ($field['settings']['tz_handling'] == 'date') {
$element['timezone'] = array(
'#type' => 'date_timezone',
'#theme_wrappers' => array('date_timezone'),
'#delta' => $delta,
'#default_value' => $timezone,
'#weight' => $instance['widget']['weight'] + 1,
'#date_label_position' => $instance['widget']['settings']['label_position'],
);
}
// Make changes if instance is set to be rendered as a regular field.
if ($instance['widget']['settings']['no_fieldset']) {
$element['#title'] = NULL;
$element['#theme_wrappers'] = array();
$element['#date_title_printed'] = FALSE;
}
else {
$element['#title'] = $instance['label'];
$element['#date_title_printed'] = TRUE;
}
return $element;
}