1

I am currently learning how to create entities in Drupal 7. I have successfully defined a new entity and made it fieldable. My question is how do a create a new instance of that entity? I understand that if I want to create a form in a normal module I use the field API to create the form to collect the information that I want. Since the entity is fieldable, I am assuming that there must be a function that creates the form for you based on the form elements that you have defined. I mean that would make sense considering the field definitions are stored in a consistent format in the database so it would stand to reason that a function could be created to automate the process of creating the form from that information. I assume this is what happens when you create a new article that has had fields added to it.

Does anyone know what this function is or what the proper procedure is to accomplish this task?

2

I am assuming that there must be a function that creates the form for you

Not quite - you need to create the form yourself with the API as normal, but you can use field_attach_form() to have the field elements added automatically. You'll also need the complementary field_attach_validate() and field_attach_submit() in your form's validate and submit handlers respectively.

Elements for your entity type's properties need to be added manually.

The best place to look for code examples is the entity example module in the Examples project, it has all of this in it and is pretty well commented.

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.