Current field-api have provided hook_field_widget_form_alter and hook_field_widget_WIDGET_TYPE_form_alter, that allow modules to alter the field widget form element. However in case the field's 'Number of values' setting is "Unlimited", hook_field_widget_form_alter only in turn alters widget form element in widget form. I think we should allow other modules to alter the whole field_widget form not only every element in widget form. For example we want to use field set to wrapper elements in stead of table.
<?php
function field_multiple_value_form($field, $instance, $langcode, $items, &$form, &$form_state) {
// code....
if ($field_elements) {
$field_elements += array(
'#theme' => 'field_multiple_value_form',
'#field_name' => $field['field_name'],
'#cardinality' => $field['cardinality'],
'#title' => $title,
'#required' => $instance['required'],
'#description' => $description,
'#prefix' => '<div id="' . $wrapper_id . '">',
'#suffix' => '</div>',
'#max_delta' => $max,
);
// Add 'add more' button, if not working with a programmed form.
if ($field['cardinality'] == FIELD_CARDINALITY_UNLIMITED && empty($form_state['programmed'])) {
$field_elements['add_more'] = array(
'#type' => 'submit',
'#name' => strtr($id_prefix, '-', '_') . '_add_more',
'#value' => t('Add another item'),
'#attributes' => array('class' => array('field-add-more-submit')),
'#limit_validation_errors' => array(array_merge($parents, array($field_name, $langcode))),
'#submit' => array('field_add_more_submit'),
'#ajax' => array(
'callback' => 'field_add_more_js',
'wrapper' => $wrapper_id,
'effect' => 'fade',
),
);
}
}
}
return $field_elements;
?>
We need change.
<?php
function field_multiple_value_form($field, $instance, $langcode, $items, &$form, &$form_state) {
//....
// allow another module alter for widget form elements.
drupal_alter('field_element_widget_form', $field_elements, $context);
return $field_elements;
}
?>
Comment | File | Size | Author |
---|---|---|---|
#14 | interdiff-1592814-11-14.txt | 441 bytes | wwedding |
#14 | 1592814-override-field_multiple_value_form-14.patch | 1.91 KB | wwedding |
#11 | 1592814-override-field_multiple_value_form-11.patch | 1.93 KB | nevergone |
#7 | 1592814-7.patch | 987 bytes | Chi |
#5 | 1592814-1.patch | 612 bytes | rogical |
Comments
Comment #1
johnvAn example is to have a multivalue widget where the user is not allowed to sort the data, and you need to remove the 'Order' column and the drag&drop js.
An alternative method is the following:
- create a MYMODULE_field_multiple_value_form($variables) function in MYMODULE.theme.inc , which overrides the original function. You can copy and modify the original code of theme_field_multiple_value_form;
- add the following in MYMODULE.module:
Comment #2
robmalon CreditAttribution: robmalon commentedI'm using Drupal 7.17. Discovered a module using this:
function hook_preprocess_field_multiple_value_form(&$variables)
So, I used it myself in one of my own custom modules and it's working well. Maybe that's not -exactly- the same thing as this, but perhaps useful to mention. I didn't find any documentation on implementing this as a hook.
Comment #3
monish_deb CreditAttribution: monish_deb commentedSame here, didn't find any information related to hook_preprocess_field_multiple_value_form. Does this feature do something like Field Collection module where we can add multiple field elements on choosing multiple values of different widget type ?
Comment #4
rogical CreditAttribution: rogical commentedThis is a simple enhancement, I'm using this to add another control button for references thumbnail.
Comment #5
rogical CreditAttribution: rogical commentedComment #6
wwedding CreditAttribution: wwedding commentedIt's been a while but I thought I'd make a note, since this issue is high in search results and it seems like this is still something a lot of people struggle with.
hook_preprocess_field_multiple_value_form(&$variables) is not a specific undocumented hook but is an implementation of hook_preprocess_HOOK(). The theme being preprocessed is theme_field_multiple_value_form().
The patch in #5 looks good and simple. However, I kind of feel like it would make more sense to refactor the logic that calls field_multiple_value_form() so that it could be modified using existing known hooks like hook_form_alter() or maybe even the widget alter hooks. Adding a specific hook just for this form element seems unnecessary.
Comment #6.0
wwedding CreditAttribution: wwedding commentedchange name of hook_field_alter.
Comment #7
Chi CreditAttribution: Chi commentedfield_multiple_value_form() is a huge form. Sometimes it would be useful to be able replace entire form as well. This patch is required for Multiple Value Widget module (7.x-2.x branch).
Comment #8
StephenRobinson CreditAttribution: StephenRobinson commentedHa Ha I came up with a much simpler option so I could set one image field as many in one content type and limit it to one on my other content types:
Comment #9
dillix CreditAttribution: dillix commentedComment #10
wwedding CreditAttribution: wwedding commentedIt's possible I missed an IRC conversation, but it seems there are a couple of different patches that take entirely different approaches and no real feedback about any of them, so I'm not 100% sure what got reviewed and tested.
I'd say that patch #5 is probably the way to go, if anything is going to be committed (and I'm guilty of not submitting any patch alternatives). #7 is an interesting idea but I don't think using variable_set() to control what function builds a form fits expectations created by other Form API patterns, which generally depend on using alter hooks to modify or replace default form structures.
Comment #11
nevergone CreditAttribution: nevergone commentedPlease review this patch.
Comment #12
asrobHi!
I've tested it under simplytest.me with Drupal 7.x. It seems working well so I changed its status to RTBC.
Comment #14
wwedding CreditAttribution: wwedding commentedIt passed simpletest in my environment so I'm not sure what that is about.
Tweaking the API documentation slightly and re-testing. Let's see what happens this time!
Comment #17
wwedding CreditAttribution: wwedding commentedLast re-test passed. Failures seem random and not related. Not switching it directly to RCTB just because I made a (minor) change.
Comment #18
David_Rothstein CreditAttribution: David_Rothstein commentedPretty sure this would be relevant for Drupal 8 too, right? (I don't see an equivalent hook or way to alter the whole multivalued form structure there either.)
Quick review in the meantime:
Don't think these should have quotes around them?
Would be nice to fill that in :)
Comment #24
NickDickinsonWildeI didn't find this when I search for it back in 2016.... so I made an issue #2822460: No hook to edit Field Widget wrappers created by WidgetBase::formMultipleElements, with a functional patch however without tests. But I got busy and didn't push it/write tests and *another* issue was created #2940201: hook_field_widget_form_alter() can no longer affect the whole widget for multi-value fields with effectively identical functionality + tests. That one got into 8.5.x. So despite this issue being the oldest of the three, closing it as duplicate.