0

I would like to have a multiple-value property attached to an entity.

Currently, all of the properties are handled with field_extra_field and then stored in a single database table so we don't add the overhead of lots more tables. We don't need all of the neat features of Field API like versioning, so this works.

However, we do need one feature, the multiple value handling. Is there any way to get this without making a real field?

1

The Examples project has an "ajax example" submodule that shows how such a thing can be done with #ajax in a form. Should get you far.

  • How could I have missed this! Thanks Bojan, I'll give it a shot and report back here. – linclark Jun 2 '11 at 17:41
0

I don't really get why you don't want to use the fields API. A field only creates two tables with the default field storage module, the data and the revision table. And the revision table is only used when the entity it is attached to uses revisioning. And you can attach the same field to multiple entities. So I'm not sure what you mean with "lots more tables".

Creating a field is pretty simple, all you need to do is define the schema for your database table, the widget form and the formatter for displaying it. See http://api.drupal.org/api/examples/field_example--field_example.module/7.

That said, you can of course replicate the multiple value handling yourself (you can even do that and still use the field API), field_multiple_value_form() is the function that contains the generic implementation for fields, which you can adapt. It basically comes down to keep track of the number of "rows" you currently have and add that amount of form elements + a button do add one more.

Image/File fields for example implement their own multiple value handling (currently totally broken in D7 when you have multiple fields) and so does my own Dynamic Properties module.

  • Unfortunately, replicating field_multiple_value_form() functionality is rather difficult if you are not assuming that the form element is a field. And fake fields defined by hook_field_extra_fields can't be used as fields in this context, either. – linclark Jun 2 '11 at 16:02

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.