How to programmatically add a field to an entity

Although I'd probably create fields through the UI and export using the Features module (because I'm lazy), if you feel like adding fields to an entity programmatically, you might find http://drupal.org/node/1228878 helpful.  That page actually covers:

  • Creating an entity type - ie the equivalent of a node type but for entities (entity_create($type, $values)->save(); or entity_save($type, entity_create($type, $values));)
  • Create a field (field_create_field($field);)
  • Create a field instance - ie tell Drupal which entities the field applies to (field_create_instance($field_instance);)

Tags: 

Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.