function boolean_field_widget_form in Boolean Field 7
Implements hook_field_widget_form().
File
- ./
boolean.module, line 314 - Defines boolean field type.
Code
function boolean_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
// Allow for a site using the cardinality patch.
// @see https://www.drupal.org/files/issues/1029298-60-move-cardinality-setting.patch
$cardinality = isset($instance['cardinality']) ? $instance['cardinality'] : $field['cardinality'];
$labels = explode("\n", $instance['settings']['labels']);
// If the boolean field is added to an entity after instances of that entity
// exist, then $delta == count($items) is TRUE when $delta == 0. Add condition
// on $delta > 0 to catch this use case and display the field.
if ($cardinality == FIELD_CARDINALITY_UNLIMITED && $delta && $delta == count($items)) {
// Suppress the automatic display of another item by the Field API to
// prevent the item being added to the database if the form is submitted.
// This should be resolved in HTML 5 when a checkbox will have a third state
// indicating whether it is set.
return array();
}
$is_set = isset($items[$delta]) && $items[$delta]['value'] != -1;
$suppress = empty($instance['settings']['suppress_warning']) ? FALSE : TRUE;
$value = $is_set ? $items[$delta]['value'] : 0;
$display_delta = count($items) > 1;
// @todo Is this ever true? This is passed one item.
$display_label = '';
if ($cardinality == FIELD_CARDINALITY_UNLIMITED) {
$display_label = $delta;
}
elseif (!empty($labels[$delta])) {
$display_label = $labels[$delta];
}
elseif ($cardinality > 1) {
$display_label = $delta;
}
$element += array(
'#type' => 'checkbox',
'#default_value' => $value,
'#on_value' => $on_value = 1,
'#off_value' => $off_value = 0,
'#no_value' => -1,
'#is_set' => $is_set,
);
$prefix = $suffix = '';
if (empty($instance['settings']['omit_strings'])) {
// Add prefix and suffix.
// When editing, always display the text applicable to a true state.
$states = boolean_value_info();
$strings = $instance['settings'][$states[1]];
// [$items[$delta]['value']]];
$prefix = isset($strings['prefix']) ? $strings['prefix'] . ' ' : '';
$suffix = isset($strings['suffix']) ? ' ' . $strings['suffix'] : '';
}
// Set the title.
$element['#title'] = $prefix . $display_label . $suffix;
if (!$is_set && !$suppress) {
// Highlight the field so the user knows it has not been set.
$element['#title'] .= ' (value not set)';
$element += array(
'#prefix' => '<div class="not-set" style="color: red;">',
'#suffix' => '</div>',
);
}
$element['#value_callback'] = 'boolean_field_widget_value';
$element['#element_validate'][] = 'boolean_field_widget_validate';
return array(
'value' => $element,
);
}