15

When we go to the path /admin/structure/types/manage/article/fields and use the Add new field option or the Add existing field option, does that create a field, create a field instance or attach a field instance? How to differentiate between them? So, programmatically, when to use field_create_field, field_create_instance and field_attach API?

In the field API documentation it only mentions the admin UI and never talks about the path where to create both fields and field instances. So it's confusing. I don't even know if I even make sense.

5
  • This should demystify it: dikini.net/31.08.2010/…
    – Clive
    Jan 21, 2013 at 16:04
  • and in normal case each field that you add creates a new field in the database and when you add an existing field it doesn't create a new table just adds it to the existing table. Jan 21, 2013 at 16:12
  • 1
    @Mohammed Shameem, thanks then whats an instance? What is the use case? Or rather where do we use it in reality? Jan 21, 2013 at 16:53
  • 1
    it is always better to use the same field instance if the purpose is or the function is going to be same. for example you have different content type with the same taxonomy vocab to be added in all. it would be better to add the same field to all the content type rather than create different fields to each content type. Internally this would decrease the number of tables build by drupal. and searching on the vocab would become better. Jan 21, 2013 at 16:57
  • 1
    So fields that we see at path node/add/article are field instances (which are reused at several different entity types). And fields at path /admin/structure/types/manage/article/fields are fields. In database I don't see a field instance table but only field_data table which, I guess, we get when we first create a field at path /admin/structure/types/manage/article/fieldsusing field_create_field. And an instance is then attached to a bundle (article in this case) that we see at path node/add/article. Jan 21, 2013 at 17:20

1 Answer 1

24

A field is the basic definition of a field itself. A field instance is an instance of that field, attached to an entity/bundle.

  1. Fields are created with field_create_field
  2. Field instances, with field_create_instance

To check if the field, or instance exists, use

  1. field_info_field or
  2. field_info_instance

When you create a field through the UI, Drupal always creates a field instance on that entity/bundle.

If you're adding an existing field, the field data is cloned and a field instance is created on the new entity/bundle.

Lastly, field settings apply globally to that field, field instance settings can vary between entity bundles.

1
  • 2
    Thanks a lot, you cleared my doubts. If I had the privilege I would +1 your post. Jan 22, 2013 at 17:35

Your Answer

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

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