Is it possible to attach a field to an entity programmatically?

I want to create a new entity, say vehicle, which has radio buttons defined for the brand of the vehicle. I would like this field to not be able to be deleted or modified via the UI.

1 Answer 1


Yes, it is possible, the function is field_create_instance(). If the field was not created before, you will need to create it with field_create_field() first.

  • 1
    Thanks! I also found this, which looks to be helpful: drupal.org/node/1228878
    – bkildow
    Oct 25, 2011 at 12:02
  • Add field to entity programmatically in Drupal 7: browse-tutorials.com/snippet/…
    – ram4nd
    Dec 13, 2011 at 15:21
  • 1
    The function is field_create_instance() if anyone tries to copy and paste. I tried to edit the post but edits must be at least 6 characters.
    – user1359
    Aug 18, 2014 at 16:33

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.