Commit 342ebd77 authored by Dries's avatar Dries
Browse files

- Patch #567064 by yched, sun: widgets done 'the easy way' have too many...

- Patch #567064 by yched, sun: widgets done 'the easy way' have too many limitations. Removes more code than it adds!
parent e4ca439b
......@@ -632,11 +632,15 @@ function hook_field_widget_info_alter(&$info) {
* Array of default values for this field.
* @param $delta
* The order of this item in the array of subelements (0, 1, 2, etc).
* @param $element
* A form element array containing basic properties for the widget: #title,
* #description, #required, #field, #field_instance, #field_name, #delta,
* #columns.
* @return
* The form item for a single element for this field.
*/
function hook_field_widget(&$form, &$form_state, $field, $instance, $langcode, $items, $delta = 0) {
$element = array(
function hook_field_widget(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
$element += array(
'#type' => $instance['widget']['type'],
'#default_value' => isset($items[$delta]) ? $items[$delta] : '',
);
......
......@@ -57,18 +57,18 @@ function field_default_form($obj_type, $object, $field, $instance, $langcode, $i
$delta = isset($get_delta) ? $get_delta : 0;
$function = $instance['widget']['module'] . '_field_widget';
if (function_exists($function)) {
if ($element = $function($form, $form_state, $field, $instance, $langcode, $items, $delta)) {
$defaults = array(
'#required' => $get_delta > 0 ? FALSE : $instance['required'],
'#columns' => array_keys($field['columns']),
'#title' => check_plain(t($instance['label'])),
'#description' => field_filter_xss($instance['description']),
'#delta' => $delta,
'#field_name' => $field['field_name'],
'#bundle' => $instance['bundle'],
'#object_type' => $instance['object_type'],
);
$element = array_merge($element, $defaults);
$element = array(
'#object_type' => $instance['object_type'],
'#bundle' => $instance['bundle'],
'#field_name' => $field['field_name'],
'#columns' => array_keys($field['columns']),
'#title' => check_plain(t($instance['label'])),
'#description' => field_filter_xss($instance['description']),
// Only the first widget should be required.
'#required' => $delta == 0 && $instance['required'],
'#delta' => $delta,
);
if ($element = $function($form, $form_state, $field, $instance, $langcode, $items, $delta, $element)) {
// If we're processing a specific delta value for a field where the
// field module handles multiples, set the delta in the result.
// For fields that handle their own processing, we can't make assumptions
......@@ -84,14 +84,6 @@ function field_default_form($obj_type, $object, $field, $instance, $langcode, $i
}
if ($form_element) {
$defaults = array(
'#field_name' => $field['field_name'],
'#tree' => TRUE,
'#weight' => $instance['widget']['weight'],
);
$form_element = array_merge($form_element, $defaults);
// Add the field form element as a child keyed by language code to match the
// field data structure: $object->{$field_name}[$langcode][$delta][$column].
// The '#language' key can be used to access the field's form element when
......@@ -99,7 +91,7 @@ function field_default_form($obj_type, $object, $field, $instance, $langcode, $i
// form element.
$addition[$field['field_name']] = array(
'#tree' => TRUE,
'#weight' => $form_element['#weight'],
'#weight' => $instance['widget']['weight'],
'#language' => $langcode,
$langcode => $form_element,
);
......@@ -145,6 +137,7 @@ function field_multiple_value_form($field, $instance, $langcode, $items, &$form,
$form_element = array(
'#theme' => 'field_multiple_value_form',
'#field_name' => $field['field_name'],
'#cardinality' => $field['cardinality'],
'#title' => $title,
'#required' => $instance['required'],
......@@ -157,21 +150,21 @@ function field_multiple_value_form($field, $instance, $langcode, $items, &$form,
$function = $instance['widget']['module'] . '_field_widget';
if (function_exists($function)) {
for ($delta = 0; $delta <= $max; $delta++) {
if ($element = $function($form, $form_state, $field, $instance, $langcode, $items, $delta)) {
$multiple = $field['cardinality'] > 1 || $field['cardinality'] == FIELD_CARDINALITY_UNLIMITED;
$defaults = array(
// For multiple fields, title and description are handled by the wrapping table.
'#title' => $multiple ? '' : $title,
'#description' => $multiple ? '' : $description,
'#required' => $delta == 0 && $instance['required'],
'#weight' => $delta,
'#delta' => $delta,
'#columns' => array_keys($field['columns']),
'#field_name' => $field_name,
'#object_type' => $instance['object_type'],
'#bundle' => $instance['bundle'],
);
$multiple = $field['cardinality'] > 1 || $field['cardinality'] == FIELD_CARDINALITY_UNLIMITED;
$element = array(
'#object_type' => $instance['object_type'],
'#bundle' => $instance['bundle'],
'#field_name' => $field_name,
'#columns' => array_keys($field['columns']),
// For multiple fields, title and description are handled by the wrapping table.
'#title' => $multiple ? '' : $title,
'#description' => $multiple ? '' : $description,
// Only the first widget should be required.
'#required' => $delta == 0 && $instance['required'],
'#delta' => $delta,
'#weight' => $delta,
);
if ($element = $function($form, $form_state, $field, $instance, $langcode, $items, $delta, $element)) {
// Input field for the delta (drag-n-drop reordering).
if ($multiple) {
// We name the element '_weight' to avoid clashing with elements
......@@ -184,14 +177,12 @@ function field_multiple_value_form($field, $instance, $langcode, $items, &$form,
'#weight' => 100,
);
}
$form_element[$delta] = array_merge($element, $defaults);
$form_element[$delta] = $element;
}
}
// Add 'add more' button, if not working with a programmed form.
if ($field['cardinality'] == FIELD_CARDINALITY_UNLIMITED && empty($form_state['programmed'])) {
$form_element[$field_name . '_add_more'] = array(
'#type' => 'submit',
'#name' => $field_name . '_add_more',
......
......@@ -313,37 +313,9 @@ function number_element_info() {
/**
* Implement hook_field_widget().
*
* Attach a single form element to the form. It will be built out and
* validated in the callback(s) listed in hook_element_info(). We build it
* out in the callbacks rather than here in hook_widget so it can be
* plugged into any module that can provide it with valid
* $field information.
*
* Field module will set the weight, field name and delta values
* for each form element.
*
* If there are multiple values for this field, the Field module will
* call this function as many times as needed.
*
* @param $form
* the entire form array, $form['#node'] holds node information
* @param $form_state
* the form_state, $form_state['values'] holds the form values.
* @param $field
* The field structure.
* @param $instance
* the field instance array
* @param $langcode
* The language associated to $items.
* @param $delta
* the order of this item in the array of subelements (0, 1, 2, etc)
*
* @return
* the form item for a single element for this field
*/
function number_field_widget(&$form, &$form_state, $field, $instance, $langcode, $items, $delta = 0) {
$element = array(
function number_field_widget(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
$element += array(
'#type' => $instance['widget']['type'],
'#default_value' => isset($items[$delta]) ? $items[$delta] : NULL,
);
......
......@@ -94,8 +94,8 @@ function options_element_info() {
/**
* Implement hook_field_widget().
*/
function options_field_widget(&$form, &$form_state, $field, $instance, $langcode, $items, $delta = NULL) {
$element = array(
function options_field_widget(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
$element += array(
'#type' => $instance['widget']['type'],
'#default_value' => !empty($items) ? $items : array(),
);
......
......@@ -563,40 +563,9 @@ function text_element_info() {
/**
* Implement hook_field_widget().
*
* Attach a single form element to the form. It will be built out and
* validated in the callback(s) listed in hook_element_info(). We build it
* out in the callbacks rather than here in hook_field_widget so it can be
* plugged into any module that can provide it with valid
* $field information.
*
* Field module will set the weight, field name and delta values
* for each form element.
*
* If there are multiple values for this field, the field module will
* call this function as many times as needed.
*
* @param $form
* the entire form array, $form['#node'] holds node information
* @param $form_state
* the form_state, $form_state['values'][$field['field_name']]
* holds the field's form values.
* @param $field
* The field structure.
* @param $instance
* the field instance array
* @param $langcode
* The language associated to $items.
* @param $items
* array of default values for this field
* @param $delta
* the order of this item in the array of subelements (0, 1, 2, etc)
*
* @return
* the form item for a single element for this field
*/
function text_field_widget(&$form, &$form_state, $field, $instance, $langcode, $items, $delta = 0) {
$element = array(
function text_field_widget(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
$element += array(
'#type' => $instance['widget']['type'],
'#default_value' => isset($items[$delta]) ? $items[$delta] : '',
);
......@@ -721,7 +690,6 @@ function text_textarea_with_summary_process($element, $form_state, $form) {
'#weight' => 0,
'#title' => t('Summary'),
'#description' => t('Leave blank to use trimmed value of full text as the summary.'),
'#required' => $element['#required'],
'#display' => $display,
'#attached' => array('js' => array(drupal_get_path('module', 'text') . '/text.js')),
'#attributes' => array('class' => array('text-textarea-summary')),
......@@ -738,7 +706,6 @@ function text_textarea_with_summary_process($element, $form_state, $form) {
'#title' => $display ? t('Full text') : $element['#title'],
'#description' => $element['#description'],
'#required' => $element['#required'],
'#required' => $instance['required'],
'#attributes' => array('class' => array('text-full-textarea')),
'#prefix' => '<div class="text-full-wrapper">',
'#suffix' => '</div>',
......
......@@ -429,7 +429,7 @@ function file_field_widget_settings_form($field, $instance) {
/**
* Implementation of hook_field_widget().
*/
function file_field_widget(&$form, &$form_state, $field, $instance, $langcode, $items, $delta = 0) {
function file_field_widget(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
$form['#attributes'] = array('enctype' => 'multipart/form-data');
$defaults = array(
......@@ -461,27 +461,21 @@ function file_field_widget(&$form, &$form_state, $field, $instance, $langcode, $
// Essentially we use the managed_file type, extended with some enhancements.
$element_info = element_info('managed_file');
$element = array(
$element += array(
'#type' => 'managed_file',
'#default_value' => isset($items[$delta]) ? $items[$delta] : $defaults,
'#required' => $instance['required'],
'#upload_location' => file_field_widget_uri($field, $instance),
'#upload_validators' => file_field_widget_upload_validators($field, $instance),
'#value_callback' => 'file_field_widget_value',
'#process' => array_merge($element_info['#process'], array('file_field_widget_process')),
// Allows this field to return an array instead of a single value.
'#extended' => TRUE,
// Add extra Field properties.
'#field_name' => $field['field_name'],
'#bundle' => $instance['bundle'],
'#object_type' => $instance['object_type'],
);
if ($field['cardinality'] == 1) {
// If there's only one field, return it as delta 0.
$element['#title'] = $instance['label'];
if (empty($element['#default_value']['fid'])) {
$element['#description'] = theme('file_upload_help', array('description' => $instance['description'], 'upload_validators' => $element['#upload_validators']));
$element['#description'] = theme('file_upload_help', array('description' => $element['#description'], 'upload_validators' => $element['#upload_validators']));
}
$elements = array($element);
}
......@@ -499,7 +493,7 @@ function file_field_widget(&$form, &$form_state, $field, $instance, $langcode, $
$elements[$delta] = $element;
$elements[$delta]['#default_value'] = $defaults;
$elements[$delta]['#weight'] = $delta;
$elements[$delta]['#required'] = ($instance['required'] && $delta == 0);
$elements[$delta]['#required'] = ($element['#required'] && $delta == 0);
}
// The group of elements all-together need some extra functionality
// after building up the full list (like draggable table rows).
......@@ -508,8 +502,11 @@ function file_field_widget(&$form, &$form_state, $field, $instance, $langcode, $
$elements['#theme_wrappers'] = array('fieldset');
$elements['#attributes']['class'] = array('file-widget');
$elements['#process'] = array('file_field_widget_process_multiple');
$elements['#title'] = $instance['label'];
$elements['#description'] = $instance['description'];
$elements['#title'] = $element['#title'];
$elements['#description'] = $element['#description'];
$elements['#object_type'] = $element['#object_type'];
$elements['#bundle'] = $element['#bundle'];
$elements['#field_name'] = $element['#field_name'];
// Add some properties that will eventually be added to the file upload
// field. These are added here so that they may be referenced easily through
......
......@@ -305,8 +305,8 @@ function image_field_widget_settings_form($field, $instance) {
/**
* Implementation of hook_field_widget().
*/
function image_field_widget(&$form, &$form_state, $field, $instance, $items, $delta = 0) {
$elements = file_field_widget($form, $form_state, $field, $instance, $items, $delta);
function image_field_widget(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
$elements = file_field_widget($form, $form_state, $field, $instance, $langcode, $items, $delta, $element);
$settings = $instance['settings'];
foreach (element_children($elements) as $delta) {
......
......@@ -489,40 +489,12 @@ function field_test_field_widget_info() {
/**
* Implement hook_field_widget().
*
* Attach a single form element to the form. It will be built out and
* validated in the callback(s) listed in hook_element_info(). We build it
* out in the callbacks rather than here in hook_widget so it can be
* plugged into any module that can provide it with valid
* $field information.
*
* If there are multiple values for this field, the field module will
* call this function as many times as needed.
*
* @param $form
* the entire form array, $form['#node'] holds node information
* @param $form_state
* the form_state, $form_state['values'][$field['field_name']]
* holds the field's form values.
* @param $field
* The field structure.
* @param $instance
* the instance array
* @param $items
* array of default values for this field
* @param $delta
* the order of this item in the array of subelements (0, 1, 2, etc)
*
* @return
* the form item for a single element for this field
*/
function field_test_field_widget(&$form, &$form_state, $field, $instance, $langcode, $items, $delta = 0) {
function field_test_field_widget(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
$element = array(
'value' => array(
'#title' => $instance['label'],
'value' => $element + array(
'#type' => 'textfield',
'#default_value' => isset($items[$delta]['value']) ? $items[$delta]['value'] : '',
'#required' => $instance['required'],
),
);
return $element;
......
......@@ -1211,8 +1211,8 @@ function taxonomy_term_title($term) {
/**
* Implement hook_field_widget().
*/
function taxonomy_field_widget(&$form, &$form_state, $field, $instance, $langcode, $items, $delta = NULL) {
$element = array(
function taxonomy_field_widget(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
$element += array(
'#type' => $instance['widget']['type'],
'#default_value' => !empty($items) ? $items : array(),
);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment