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.
See #417122: Allow drupal_alter() on Field and Widget Settings
One of the hook added was hook_widget_settings_alter()
. What is it in D7?
It would be nice if the change is documented in http://drupal.org/node/224333
Comments
Comment #1
Mark TrappI believe you're looking for
hook_field_widget_properties_alter()
.Comment #2
mattyoung CreditAttribution: mattyoung commentedI tried that but
hook_field_widget_properties_alter()
didn't get call when I edit the field settings (admin/structure/types/manage/{CONTENT-TYPE}/fields/{FIELD}). In D6, I usehook_widget_settings_alter()
to add form fields to the widget settings form (admin/content/node-type/{CONTENT-TYPE}/fields/{FIELD}) and have those values saved. I need the same hook for D7.Comment #3
mattyoung CreditAttribution: mattyoung commentedOk, I clear the cache and my
hook_field_widget_properties_alter()
is called now. But I fail to see how to use this hook to do what I did in D6 withhook_widget_settings_alter()
:How to I change this into D7?
Comment #4
mlncn CreditAttribution: mlncn commentedIf there is no equivalent to this hook, it looks like it could be reproduced with form_alter(). Where were the settings saved to?
Comment #5
anrikun CreditAttribution: anrikun commentedComment #6
anrikun CreditAttribution: anrikun commentedComment #7
anrikun CreditAttribution: anrikun commentedComment #8
anrikun CreditAttribution: anrikun commentedThis issue really needs some documentation please.
Modules that relied on this hook can not be ported to D7.
Comment #9
BerdirI don't think there is a specific hook for this, I worked around this by implementing hook_form_alter().
See http://api.worldempire.ch/api/properties/properties.module/function/prop... for an example.
Comment #10
Alan D. CreditAttribution: Alan D. commentedThere is a subpage for this info: http://drupal.org/node/728792 - Updating CCK Field Modules from D6 to D7.
Comment #11
anrikun CreditAttribution: anrikun commentedThank you for the link Alan, but there is no info about hook_widget_settings_alter() yet.
Comment #12
bryancasler CreditAttribution: bryancasler commentedsubscribe
Comment #13
jhodgdona) Should this be discussed in the core issue queue? I don't think Drupal Core is necessarily responsible for migrating CCK to core Fields. I don't necessarily agree with that philosophy, but that's what I've been told in the past. This should either be a feature request (add a widget settings alter hook, in which case it is probably too late for D7 to add to the API), or it's a contrib doc task.
b) Fixing the tags, since this is regarding update documentation and we have a special tag for that.
Comment #14
mattyoung CreditAttribution: mattyoung commented#4:
> Where were the settings saved to?
The settings are saved in the CCK field. There is a $op="save" in
hook_widget_settings_alter(&$v, $op, $widget)
to indicate additional what settings to save. These settings are available in the '#field_info' key in the node edit form when that form is rendered.> it looks like it could be reproduced with form_alter().
The form can be changed, but do the field widget save them? This is very important.
Comment #15
anrikun CreditAttribution: anrikun commentedUsing form_alter() is nothing but a workaround and definitely not a viable solution.
It's stated everywhere that the Field module is CCK moved to Core.
If the Field module doesn't have the features that CCK provided, then it looks like a major regression to me.
Comment #16
Alan D. CreditAttribution: Alan D. commentedRegression maybe, but it should still be possible to do this yourself. Try an #afterbuild & custom submit handler. This is the likely solution imagefield extended will take with changes from imagefield => image module.
Comment #17
marcingy CreditAttribution: marcingy commentedFields is not CCK moved to core, it is cck like functionality added to core therefore this is not a regression.
Comment #18
jhodgdonChanging title and priority, since this appears to be an update doc issue.
Comment #19
heshanlksub
Comment #20
fax8 CreditAttribution: fax8 commentedI'm affected by this lack of documentation. This is critical for porting ad_gallery to drupal 7.
Comment #21
jhodgdonThis should be in the Documentation project, since that is where the CCK -> Fields update doc is being worked on.
Comment #22
heshanlkThis could be done with form alter. But have to add custom #submit handlers and properly identify the form elements, at least if we have #process callback for the widget element it would be easy to extend the widget form rather than using form_alter()
Comment #23
gagarine CreditAttribution: gagarine commentedtracking, need it for http://drupal.org/project/node_repeat
Comment #24
pcambraSuscribe
Comment #25
Alan D. CreditAttribution: Alan D. commentedHere is a partial example :)
This shows how to do the form alters to hit both the field AND instance settings. Make sure you have a good look at the form element names, these may be different to what you expect. [Use the devel modules' dpm() or PHP var_dump() or print_r()]
I didn't have to save anything, rather I had to theme multiple fields into a nicer table display. I'm fairly confident that you could simply add an additional submit handler and save the values somewhere.
If saving to the variables table, try something like:
This stores all of the sub tree in a single variable, so much cleaner than having 100's of different ones floating around.
If someone completes a full working example, post it back and it may be added to the docs.
For those interested, the name field module has a full working themed example of the above code. It shows how you can theme up the forms nicely. But beware, there were about 50 fields themed up into two compact tables so it is not the easiest to follow. [btw, there were issues using the #theme property about a year ago, but I can not remember the details and these may be resolved now.]
Comment #26
Alan D. CreditAttribution: Alan D. commentedTo save the values appears a bit more complex. The #submit handlers that I added to the settings form did not kick in when I needed to do this for the name field module. So to save the values, I actually had to use the series of hook_field_ACTION_instance() to save these.
Hope someone finds this helpful :)
Comment #27
anrikun CreditAttribution: anrikun commentedThere is no progress so far on this issue.
This is a real blocker for these modules:
http://drupal.org/project/imagefield_focus
http://drupal.org/project/ad_gallery
http://drupal.org/project/node_repeat
My question is: is it really a documentation problem?
Comment #28
Alan D. CreditAttribution: Alan D. commentedWell, the above solution (#26) works for me, so albeit not great, there is a work-around. I've used this pattern 3 times now, and no issues to report yet.
Comment #29
Damien Tournoud CreditAttribution: Damien Tournoud commentedThe only thing to document is how to use:
* hook_field_info_alter() or hook_field_widget_info_alter() to define a default value for a setting (field-level or widget-level),
* and hook_form_field_ui_field_edit_form_alter() to add the form for the setting.
Everything else (including saving the data) is handled for you by the Field API.
Comment #30
anrikun CreditAttribution: anrikun commentedFor reference, I'm adding a quote from #672526: hook_widget_settings_alter() is missing in D7 (and D8) and several D6 modules cannot be ported to D7 because of this.:
yshed wrote:
I hope that #1204230: Missing hook_field_widget_form_alter() gets committed to D8 and backported to D7 quickly.
Comment #31
goron CreditAttribution: goron commentedDoes anyone have an example they could show of doing this in the way suggested (by yched) above? It would really help to see some code for this.
Thanks.
Comment #32
anrikun CreditAttribution: anrikun commentedFYI #1204230: Missing hook_field_widget_form_alter() has been committed!
I guess this can be closed then.
Comment #33
Alan D. CreditAttribution: Alan D. commentedI think that people are wanting a way to also save this data. I'm using the way outlined above using the 3 hook_field_ACTION_instance() hooks and it works, but quoting Damien Tournoud from #29
The only thing to document is how to use:
* hook_field_info_alter() or hook_field_widget_info_alter() to define a default value for a setting (field-level or widget-level),
* and hook_form_field_ui_field_edit_form_alter() to add the form for the setting.
Everything else (including saving the data) is handled for you by the Field API.
So I tried adding a test value into the settings alter:
And this value is nowhere to be seen in the field data array after saving, not the field, instance nor widget settings. So is this either a bug that needs to be resolved (either internally in this module or in core Drupal) or this magic from CCK has not been included in core.