0

I have a question about adding fields to an entity I've defined in a drupal 7 module upgrading from d6 which has 4 types. The modules get a list of fields from a 3rd party server and if the user enables them through the admin interface they are added to the node object as cck fields before the node gets saved. Can I add fields to a bundle in a similar fashion using field_create_instance? Is it okay if the same type entity has different fields in different cases, or does the field get added to every entity of that type once it's added?

There are different resources (think data coming from different servers with potentially different fields) each with the same 4 entity types, and different fields could be enabled for each resource. I'm considering adding the fields which are enabled to the entity before it is saved. Entity type 1 could have different fields selected for resource 1 and resource 2. This is the first time I have used d7 entities which is why I'm asking how they work.

1 Answer 1

2

field_create_instance already adds a field instance to a bundle. You can't add fields to an entity type or an entity itself.

I'm not quite sure if I understood you right:

Guess 1:
You want for example two nodes of type person where one has a field phone number and the other one not, which is not possible. Your node type has to define all possible field types, but they may be optional, so if the external system doesn't provide data for it, simply leave it empty.

Guess 2:
You want to adjust a whole entity type (this means all existing entities) based on external systems, user input or whatever. Thats possible with the Field CRUD API.

2
  • I'm going to end up adding fields to my bundles with field_create_instance and just leave them empty if they are not enabled on a particular server. Thanks for helping me hash this out. Sorry I was so confusing in my question.
    – Arosboro
    Sep 5, 2011 at 19:49
  • Glad to help! What just came into my mind: The terms field and field instance are a little confusing. field instance doesn't refer to an actual field value (which could be assumed in terms of OOP). For example there is the field text and the field instance would be the field first_name connected to the node type person. Sep 6, 2011 at 8:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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