Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
API page: http://api.drupal.org/api/drupal/modules--field--field.api.php/function/...
Describe the problem you have found:
This needs to state that it is invoked both on entity forms, and in the field edit form where it is used to make the form element that allows setting a default value for the field.
The docs should also explain how a hook implementation can distinguish between the two cases.
Comment | File | Size | Author |
---|---|---|---|
#30 | 1356824-30-hook-field-widget-form-alter.patch | 6.61 KB | dcam |
#27 | 1356824-27-hook-field-widget-form-alter-tests-only.patch | 3.2 KB | dcam |
#27 | 1356824-27-hook-field-widget-form-alter.patch | 6.61 KB | dcam |
#22 | drupal-1356824-22.patch | 6.65 KB | tim.plunkett |
#22 | interdiff.txt | 7.2 KB | tim.plunkett |
Comments
Comment #1
joachim CreditAttribution: joachim commentedBest I can find is this which is rather brittle:
Comment #2
xjmAh, I ran into this as well. This is what I did:
Comment #3
xjmSo, to fix this, we'd add the entity and type explicitly to the context, maybe with the entity as
FALSE
orNULL
when on the default form, or maybe with some flag as another piece of context info.The problem in #1204230: Missing hook_field_widget_form_alter() was that field_multiple_value_form() doesn't get that info in context, so it can't pass it on to the alter hook.
Comment #4
Alan D. CreditAttribution: Alan D. commentedIn other hooks, entity is unset so this would be the consistent way of doing things in D7.
The way that I have been checking for the field default value form is:
Comment #5
joachim CreditAttribution: joachim commentedHere's a quick patch (untested, as the theme_username change has hosed my D8 and I haven't time to reinstall right now).
Given this doesn't change parameters, I reckon it could be backported.
Comment #6
Alan D. CreditAttribution: Alan D. commentedThis could be made a bit tighter by removing the if-else. Love to see this back-ported to D7!
Comment #7
Alan D. CreditAttribution: Alan D. commentedThe above patch missed one function call to the hook_field_widget_form pair and three API argument list updates. This is works fine on a development D7 site.
I assume that you can not use field_multiple_value_form() on the default value widget?
As an aside, what is the recommended markup of arguments. The field api uses both quoted and un-quoted:
[edit] The second one complies with the coding standard.
Comment #8
xjmNice, this seems like a straightforward enough way to fix this. Let's add test coverage for the new key.
Regarding how to document the keys, no quotes is the preferred standard. (There are outstanding documentation issues somewhere to clean this up.) Reference: http://drupal.org/node/1354#lists
Comment #9
Alan D. CreditAttribution: Alan D. commentedI leave the tests for someone else, these are not my strong point (I've only got about 5% coverage on my own contrib modules at the moment)
Goggled the argument standards, there are 380 still in head to correct ;)
Comment #10
Alan D. CreditAttribution: Alan D. commentedYuk. This applies to the actual widget form itself :(
I have found that an Ajax callback to a sub-component (using drupal_html_id()) fails here as the form is rebuilt and the id is updated. So you need to rebuild the entire widget or bypass the drupal_html_id() when generating the wrapper.
Comment #11
xjm@Alan D., can you clarify? Are you saying the patch doesn't target only the default form element? If so, let's set the issue back to NW.
Comment #12
Alan D. CreditAttribution: Alan D. commentedNo this patch is fine, I'm referring to the widget form:
The structure of my widget is:
Each time the ajax-trigger is activated, the resulting ID returned from drupal_html_id() used to assign the ID to the ajax-target is incremented by one (ie: it works once, then it breaks as the ajax-target has a new ID).
In the particular case of the default value form, a manual ID is fine, otherwise drupal_html_id() is being used, so I have ended up using a variant on the above (#1) check to see if the $form['#title'] is set to t('Default value).
Long story short, maybe we need to flag a parameter to hook_field_widget_form() with a default flag too?
Comment #13
joachim CreditAttribution: joachim commented> Long story short, maybe we need to flag a parameter to hook_field_widget_form() with a default flag too?
Yes, I'd say we do. Just found out that hook suffers from the same problem.
Comment #14
tim.plunkettPer #12/13, also still needs tests.
Comment #15
Adagio CreditAttribution: Adagio commentedFirst attempt in here, so please let me know if anything can be improved. This is a rerolled patch to 8.x, and added tests. Not every codepath is tested. In hook_field_widget_properties_alter and hook_field_test_field_widget_form_alter, I set a message when $context['default'] is true, and check for those on the manage field form and an entity form.
So hook_field_widget_properties_ENTITY_TYPE_alter and hook_field_widget_WIDGET_TYPE_form_alter() is not covered in these tests currently. However they are called from the same places as the generic ones, so I figured this was decent coverage.
Comment #16
Adagio CreditAttribution: Adagio commentedComment #18
Adagio CreditAttribution: Adagio commentedAnother go.
Comment #20
Adagio CreditAttribution: Adagio commentedCorrect line endings.
Comment #21
Adagio CreditAttribution: Adagio commentedComment #22
tim.plunkettRerolled for some code style issues. I think this is ready to go.
Comment #23
marcingy CreditAttribution: marcingy commentedLooks good
Comment #24
xjmLovely. Thanks everyone!
Comment #25
catchI stared at this a couple of times, the $context['default'] looks a bit hacky to me, but the only idea I had was adding a new hook which is no good for Drupal 7 backport and probably not a good idea anyway.
Committed/pushed to 8.x, moving to 7.x for backport.
Comment #26
larowlanSorry to chime in this late but I think $element['#entity'] is NULL (in D7 at least) on the default form.
Comment #27
dcam CreditAttribution: dcam commentedBackported #22 to D7.
Comment #28
Alan D. CreditAttribution: Alan D. commentedI don't think that this was committed :( Or if it was, the changes were lost when this area was refactored, assuming that this is still required.
See FieldInstance::getWidget()
Comment #29
mgifforddoesn't seem to apply now.
Comment #30
dcam CreditAttribution: dcam commentedRerolled #27.