diff --git a/modules/field/field.api.php b/modules/field/field.api.php index 3287dd5..c8518a1 100644 --- a/modules/field/field.api.php +++ b/modules/field/field.api.php @@ -677,6 +677,7 @@ function hook_field_is_empty($item, $field) { * * @see hook_field_widget_info_alter() * @see hook_field_widget_form() + * @see hook_field_widget_form_alter() * @see hook_field_widget_error() * * @return @@ -786,6 +787,9 @@ function hook_field_widget_info_alter(&$info) { * @see field_widget_field() * @see field_widget_instance() * + * Other modules may alter the form element provided by this function using + * hook_field_widget_form_alter(). + * * @param $form * The form structure where widgets are being attached to. This might be a * full form structure, or a sub-element of a larger form. @@ -836,6 +840,30 @@ function hook_field_widget_form(&$form, &$form_state, $field, $instance, $langco } /** + * Alter forms for field widgets provided by other modules. + * + * @param $element + * The field widget form element as constructed by hook_field_widget_form(). + * @param $form_state + * An associative array containing the current state of the form. + * @param $context + * An associative array containing additional information about the current + * field widget: + * - field: The field structure. + * - instance: The field instance. + * - form: The form structure where widgets are being attached. + * This might be a full form structure, or a sub-element of a larger form. + * + * @see hook_field_widget_form() + * @see hook_form_alter() + */ +function hook_field_widget_form_alter(&$element, &$form_state, $context) { + if ($context['instance']['widget']['type'] = 'my_autocomplete_type') { + $element['#autocomplete_path'] = 'mymodule/path'; + } +} + +/** * Flag a field-level validation error. * * @param $element diff --git a/modules/field/field.form.inc b/modules/field/field.form.inc index 9250a51..7e28bd7 100644 --- a/modules/field/field.form.inc +++ b/modules/field/field.form.inc @@ -27,13 +27,13 @@ function field_default_form($entity_type, $entity, $field, $instance, $langcode, } // Let modules alter the widget properties. - $context = array( + $properties_context = array( 'entity_type' => $entity_type, 'entity' => $entity, 'field' => $field, 'instance' => $instance, ); - drupal_alter(array('field_widget_properties', 'field_widget_properties_' . $entity_type), $instance['widget'], $context); + drupal_alter(array('field_widget_properties', 'field_widget_properties_' . $entity_type), $instance['widget'], $properties_context); // Collect widget elements. $elements = array(); @@ -76,6 +76,19 @@ function field_default_form($entity_type, $entity, $field, $instance, $langcode, '#delta' => $delta, ); if ($element = $function($form, $form_state, $field, $instance, $langcode, $items, $delta, $element)) { + // Allow modules to alter the field widget form element. + $form_context = array( + 'entity_type' => $entity_type, + 'entity' => $entity, + 'form' => $form, + 'field' => $field, + 'instance' => $instance, + 'langcode' => $langcode, + 'items' => $items, + 'delta' => $delta, + ); + + drupal_alter(array($function, 'field_widget_form'), $element, $form_state, $form_context); // 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 @@ -193,6 +206,18 @@ function field_multiple_value_form($field, $instance, $langcode, $items, &$form, '#weight' => 100, ); } + // Allow modules to alter the field widget form element. + $context = array( + 'entity_type' => $instance['entity_type'], + 'form' => $form, + 'field' => $field, + 'instance' => $instance, + 'langcode' => $langcode, + 'items' => $items, + 'delta' => $delta, + ); + drupal_alter(array($function, 'field_widget_form'), $element, $form_state, $context); + $field_elements[$delta] = $element; } }