I am trying to create a field in hook_install() using the following code.

$field = array(     
  'field_name' => 'unit_count',
  'type' => 'text',


$instances = array();

$instances['unit_count'] = array(
  'field_name' => 'unit_count',
  'label' => $t('unit_count'),
  'description' => $t('unit_count'),
  'widget' => array(
    'type' => 'text_textfield',
  'required' => TRUE,
  'settings' => array(
    'text_processing' => 0,

foreach ($instances as $instance) {
  $instance['entity_type'] = 'node';
  $instance['bundle'] = 'first_example_bundle';

I am getting the following error:

Notice: Undefined index: id in field_create_field() (line 173 of /Library/WebServer/Sites/Dev/Public/Peirce/modules/field/field.crud.inc).

What is wrong with the code I am using?

  • Looking at the code in field_create_field(): Normally the $record['id'] would be set within drupal_write_record(), if 'id' is the auto-increment key in the field_config table. If this is not happening, it means that the call to drupal_write_record() one line above did fail for some reason.
    – donquixote
    Dec 30 '18 at 1:28

Im not sure what your error is. Every field has different widget settings and differing options ... keeping all the options you can set straight in my head is beyond me ... I have created fields in install profiles and using hook_install. I tend to build the fields using the Field UI found at Structure -> Content Type -> then add/edit Fields then export those settings to code for use in a module.

I found this blog post immensely helpful:

Exporting and creating field definitions in Drupal 7

From the blog post you can get a field and its instance config with:

$field_data = field_info_field('field_article_image');
$instance_data = field_info_instance('node', 'field_article_image', 'article');

You can create a field and instance(s) in PHP using the Field API:


You can use Devel or Drush with drush php-script or eval and the following code nabbed from the blog post. It will generate the above 2 lines of code with the $config and $instance data all filled out for you to use in your custom code:

$entity_type = '';
$field_name = 'field_';
$bundle_name = '';

$info_config = field_info_field($field_name);
$info_instance = field_info_instance($entity_type, $field_name, $bundle_name);
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>');


Once you see a valid export from an example field you are trying to create. It can be easy to re-use, tweak, or fix small parts of that field configuration to use in the same project or other projects. But I never sit down from scratch and try to code it all by hand for every field.

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.