In order to alter the properties of a field widget form element defined by another module, modules must loop over every field of every form in either
hook_field_attach_form(), searching for the correct field or widget type. This adds unnecessary overhead and makes field and form customization far more complicated than it needs to be. For example, a module enhancing the functionality of core taxonomy fields must check every form for taxonomy field widgets.
hook_field_widget_form_alter() (with widget-specific variant
hook_field_widget_WIDGET_TYPE_form_alter()) to allow the widget form to be altered directly.
Create new hooksDone Verify functionalityDone Document new hooksDone Review by field API maintainerDone Review final version of patch in #1204230-30: Missing hook_field_widget_form_alter()Done Sign-off by field API maintainerDone Review patch #33Done
User interface changes
Minor, backwards-compatible addition only. No existing code will be affected.
Two new hooks will become available:
hook_field_widget_form_alter(&$element, &$form_state, $context)
hook_field_widget_WIDGET_TYPE_form_alter(&$element, &$form_state, $context)
Original report by @j0rd
I'm trying to do a simple task.
I've created a field, and I want to put a custom validator on it. I'm using a textfield, and want to make sure for this particular instance, it's a URL link.
Thing is, I might add this field into more than one entity/bundle or what ever they are called these days. So I'd really like to attach it to the field instance (aka field_name) instead of the normal form_validation. This seems like the best place for it to be.
Unfortunately there's no hook to get at this. The closest I can get is hook_field_widget_form(), but unfortunately this is only called with the module that defines the widget...so as far as I can tell...I'm at a dead-end with out hacking core. There was some promising looking module_invokes in _field_invoke(), but again, those fall short.
I also think it would be a wise idea to assist developers to create a field_FIELDNAME_validator() type shortcut to a hook like this.
Maybe there's a way to do this, that I don't know about....but right now I'm stuck. I tried looking this up and only found two other people with the same problem I'm having.
So, is this something I can already do, but can't figure out? Or is this not important? Or is this something we'd like to add into core to ease developers lives & code portability.
PS. I know I could define my own widget to get my own validation...but IMHO, this is slightly overkill.