- Patch #950138 by yched, an.droid: Changed Abstract Field API () usage into an API function.

merge-requests/26/head
Dries Buytaert 2010-10-31 12:12:00 +00:00
parent ad8eacac3b
commit d91b1c77aa
6 changed files with 74 additions and 16 deletions

View File

@ -759,15 +759,25 @@ function hook_field_widget_info_alter(&$info) {
* invoke this hook as many times as needed.
*
* Note that, depending on the context in which the widget is being included
* (regular entity edit form, 'default value' input in the field settings form,
* etc.), the passed in values for $field and $instance might be different
* from the official definitions returned by field_info_field() and
* field_info_instance(). If the widget uses Form API callbacks (like
* #element_validate, #value_callback...) that need to access the $field or
* $instance definitions, they should not use the field_info_*() functions, but
* fetch the information present in $form_state['field']:
* - $form_state['field'][$field_name][$langcode]['field']
* - $form_state['field'][$field_name][$langcode]['instance']
* (regular entity form, field configuration form, advanced search form...),
* the values for $field and $instance might be different from the "official"
* definitions returned by field_info_field() and field_info_instance().
* Examples: mono-value widget even if the field is multi-valued, non-required
* widget even if the field is 'required'...
* Therefore, the FAPI element callbacks (such as #process, #element_validate,
* #value_callback...) used by the widget cannot use the field_info_field()
* or field_info_instance() functions to retrieve the $field or $instance
* definitions they should operate on. The field_widget_field() and
* field_widget_instance() functions should be used instead to fetch the
* current working definitions from $form_state, where Field API stores them.
*
* Alternatively, hook_field_widget_form() can extract the needed specific
* properties from $field and $instance and set them as ad-hoc
* $element['#custom'] properties, for later use by its element callbacks.
*
* @see field_widget_field()
* @see field_widget_instance()
*
* @param $form
* The entire form array.

View File

@ -411,3 +411,51 @@ function field_add_more_js($form, $form_state) {
return $element;
}
/**
* Retrieves the field definition for a widget's helper callbacks.
*
* Widgets helper element callbacks (such as #process, #element_validate,
* #value_callback, ...) should use field_widget_field() and
* field_widget_instance() instead of field_info_field() and
* field_info_instance() when they need to access field or instance properties.
* See hook_field_widget_form() for more details.
*
* @see field_widget_instance()
* @see hook_field_widget_form()
*
* @param $element
* The structured array for the widget.
* @param $form_state
* The form state.
* @return
* The $field definition array for the current widget.
*/
function field_widget_field($element, $form_state) {
$info = $form_state['field'][$element['#field_name']][$element['#language']];
return $info['field'];
}
/**
* Provides the instance definition array for a widget's helper callbacks.
*
* Widgets helper element callbacks (such as #process, #element_validate,
* #value_callback, ...) should use field_widget_field() and
* field_widget_instance() instead of field_info_field() and
* field_info_instance() when they need to access field or instance properties.
* See hook_field_widget_form() for more details.
*
* @see field_widget_field()
* @see hook_field_widget_form()
*
* @param $element
* The structured array for the widget.
* @param $form_state
* The form state.
* @return
* The $instance definition array for the current widget.
*/
function field_widget_instance($element, $form_state) {
$info = $form_state['field'][$element['#field_name']][$element['#language']];
return $info['instance'];
}

View File

@ -356,8 +356,8 @@ function number_field_widget_form(&$form, &$form_state, $field, $instance, $lang
* FAPI validation of an individual number element.
*/
function number_field_widget_validate($element, &$form_state) {
$field = $form_state['field'][$element['#field_name']][$element['#language']]['field'];
$instance = $form_state['field'][$element['#field_name']][$element['#language']]['instance'];
$field = field_widget_field($element, $form_state);
$instance = field_widget_instance($element, $form_state);
$type = $element['#number_type'];
$value = $element['#value'];

View File

@ -583,7 +583,7 @@ function file_field_widget_value($element, $input = FALSE, $form_state) {
if ($input) {
// Checkboxes lose their value when empty.
// If the display field is present make sure its unchecked value is saved.
$field = $form_state['field'][$element['#field_name']][$element['#language']]['field'];
$field = field_widget_field($element, $form_state);
if (empty($input['display'])) {
$input['display'] = $field['settings']['display_field'] ? 0 : 1;
}
@ -611,8 +611,8 @@ function file_field_widget_process($element, &$form_state, $form) {
$item = $element['#value'];
$item['fid'] = $element['fid']['#value'];
$field = $form_state['field'][$element['#field_name']][$element['#language']]['field'];
$instance = $form_state['field'][$element['#field_name']][$element['#language']]['instance'];
$field = field_widget_field($element, $form_state);
$instance = field_widget_instance($element, $form_state);
$settings = $instance['widget']['settings'];
$element['#theme'] = 'file_widget';

View File

@ -335,7 +335,7 @@ function image_field_widget_process($element, &$form_state, $form) {
$item = $element['#value'];
$item['fid'] = $element['fid']['#value'];
$instance = $form_state['field'][$element['#field_name']][$element['#language']]['instance'];
$instance = field_widget_instance($element, $form_state);
$settings = $instance['settings'];
$widget_settings = $instance['widget']['settings'];

View File

@ -1457,7 +1457,7 @@ function taxonomy_autocomplete_validate($element, &$form_state) {
$value = array();
if ($tags = $element['#value']) {
// Collect candidate vocabularies.
$field = $form_state['field'][$element['#field_name']][$element['#language']]['field'];
$field = field_widget_field($element, $form_state);
$vocabularies = array();
foreach ($field['settings']['allowed_values'] as $tree) {
if ($vocabulary = taxonomy_vocabulary_machine_name_load($tree['vocabulary'])) {