Creating Fields in a Custom Module
I’m going to be very brief here, because Joel Stein covers this beautifully. Also, if you’re looking to create fieldgroups, see this related post.
If you need your module to add a field to your Drupal 7 site, follow this quick and easy process:
- Build the field through the “manage fields” UI.
- Modify the code below to include a proper $field_name, $entity_type and $bundle_name. I needed to add a field to the user profile, so I set the $entity_type to “user” and the $bundle_name to “user”.
123456789101112$field_name = ''; // e.g., field_story_pagecount$entity_type = ''; // e.g., node$bundle_name = ''; // e.g., story$info_config = field_info_field($field_name);$info_instance = field_info_instance($entity_type, $field_name, $bundle_name);unset($info_config['id']);unset($info_instance['id'], $info_instance['field_id']);include_once DRUPAL_ROOT . '/includes/utility.inc';$output = "field_create_field(" . drupal_var_export($info_config) . ");\n";$output .= "field_create_instance(" . drupal_var_export($info_instance) . ");";drupal_set_message("<textarea rows=30 style=\"width: 100%;\">" . $output . '</textarea>'); - Run this through the Devel “Execute PHP Code” page, or use Drush.
- Paste the output within your module’s hook_install() function within mymodule.install.
- Cleanup the formatting.
- Delete the field you created through the UI.
- Enable your module.
- Check if the field shows up.
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
/** * Implements hook_install(). */ function activecollab_install() { field_create_field(array( 'translatable' => '0', 'entity_types' => array(), 'settings' => array( 'max_length' => '75', ), 'storage' => array( 'type' => 'field_sql_storage', 'settings' => array(), 'module' => 'field_sql_storage', 'active' => '1', 'details' => array( 'sql' => array( 'FIELD_LOAD_CURRENT' => array( 'field_data_field_activecollab_apikey' => array( 'value' => 'field_activecollab_apikey_value', 'format' => 'field_activecollab_apikey_format', ), ), 'FIELD_LOAD_REVISION' => array( 'field_revision_field_activecollab_apikey' => array( 'value' => 'field_activecollab_apikey_value', 'format' => 'field_activecollab_apikey_format', ), ), ), ), ), 'foreign keys' => array( 'format' => array( 'table' => 'filter_format', 'columns' => array( 'format' => 'format', ), ), ), 'indexes' => array( 'format' => array( 'format', ), ), 'field_name' => 'field_activecollab_apikey', 'type' => 'text', 'module' => 'text', 'active' => '1', 'locked' => '0', 'cardinality' => '1', 'deleted' => '0', 'columns' => array( 'value' => array( 'type' => 'varchar', 'length' => '75', 'not null' => FALSE, ), 'format' => array( 'type' => 'varchar', 'length' => 255, 'not null' => FALSE, ), ), 'bundles' => array( 'user' => array( 'user', ), ), )); field_create_instance(array( 'label' => 'API Key', 'widget' => array( 'weight' => '3', 'type' => 'text_textfield', 'module' => 'text', 'active' => 1, 'settings' => array( 'size' => '75', ), ), 'settings' => array( 'text_processing' => '0', 'user_register_form' => 1, ), 'display' => array( 'default' => array( 'label' => 'above', 'type' => 'text_default', 'settings' => array(), 'module' => 'text', 'weight' => 0, ), ), 'required' => 0, 'description' => '', 'default_value' => NULL, 'field_name' => 'field_activecollab_apikey', 'entity_type' => 'user', 'bundle' => 'user', 'deleted' => '0', )); } |