0

I need to create a custom field widget that is actually a 2 input fields group. I also need to save these values (I guess this is done through hook_field_schema())

The main problem is that I'm a little bit confused about how to use hook_field_widget_info() and hook_field_widget_form() and how are these connected with the hook_field_schema()

Any insights / links about this? Thanks

1

As well as their schema and other basic definitions, fields have

  • Widgets

    Field API widgets specify how fields are displayed in edit forms. Fields of a given field type may be edited using more than one widget. In this case, the Field UI module allows the site builder to choose which widget to use. Widget types are defined by implementing hook_field_widget_info().

  • Formatters

    Field API formatters specify how fields are displayed when the entity to which the field is attached is displayed. Fields of a given field type may be displayed using more than one formatter. In this case, the Field UI module allows the site builder to choose which formatter to use. Field formatters are defined by implementing hook_field_formatter_info().

Each field needs a widget and formatter to do anything useful, but they don't necessarily have to be implemented by the same module that implements the field. For example, you could piggyback off of one of the select widgets if that made sense for your field.

For full code examples, check out the Examples module

  • I had a look at the examples modules but couldn't find what I was looking for. So I had a look at the field/text.module in core. This made more sense. One thing is somehow unclear: in hook_field_widget_form() if the returned array keys are the same as the columns in hook_field_schema(), they will automatically be saved in those columns in the database, right? – Marius Ilie Jun 10 '14 at 18:03
  • 2
    Yep exactly right, whatever's eventually in $form_state['values']['field_foo']['und'][0]['my_column'] (for example) will go into the my_column field from your schema (it'll actually be called field_foo_my_column in the database but as far as the API's concerned you'll always be dealing with my_column) – Clive Jun 10 '14 at 18:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.