Attempt to create field name which already exists and is active.

Created on 12 July 2023, almost 2 years ago

I have created a custom module. Show me an error when I want to enable my custom module. The error is -
FieldException: Attempt to create field name windfarm_unit_count which already exists and is active. in field_create_field() (line 85 of \modulebuilt\modules\field\field.crud.inc).

//create fields
$fields = array();
$fields['windfarm_unit_count'] = array( 
 'field_name' => 'windfarm_unit_count',
 'type' => 'number_integer',
 //optional
 'cardinality' => 1,
 'settings' => array(
   'max_length' => 5,
  )
 );
 $fields['windfarm_latitude'] = array(
  'field_name' => 'windfarm_latitude',
  'type' => 'number_float',
  'settings' => array(
   'max_length' => 20,
  )
 );
$fields['windfarm_turbine_manufacturer'] = array(
  'field_name' => 'windfarm_turbine_manufacturer',
  'type' => 'text',
  'settings' => array(
   'max_length' => 60,
  )
 );
 foreach ($fields as $field) {
  field_create_field($field);
 }
 
 //create field instances
 $instances = array();
 $instances['windfarm_unit_count'] = array(
  'field_name' => 'windfarm_unit_count',
  'label' => $t('Number of Units'),
  'desription' => $t('Number of Individual unit at a given facility'),
  'widget' => array(
   'type' => 'text_textfield',
   ),
   'required' => TRUE,
   'settings' => array(
    'text_processing' => 0,
    ),
 );
$instances['windfarm_latitude'] = array(
 'field_name' => 'windfarm_latitude',
 'label' => $t('Latitude'),
 'desription' => $t('Signed degrees formate (DDD.dddd)'),
  'widget' => array(
   'type' => 'text_textfield',
   ),
   'settings' => array(
    'text_processing' => 0,
    ),
    'display' => array(
     'default' => array(
      'type' => 'hidden',
      ),
     ),
   );

$instances['windfarm_longitude'] = array(
 'field_name' => 'windfarm_longitude',
 'label' => $t('Longitude'),
 'desription' => $t('Signed degrees formate (DDD.dddd)'),
  'widget' => array(
   'type' => 'text_textfield',
   ),
   'settings' => array(
    'text_processing' => 0,
    ),
    'display' => array(
     'default' => array(
      'type' => 'hidden',
      ),
     ),
   );

 $instances['windfarm_turbine_manufacturer'] = array(
 'field_name' => 'windfarm_turbine_manufacturer',
 'label' => $t('turbine manufacturer'),
 'desription' => $t('The name of the turbine manufacturer'),
  'widget' => array(
   'type' => 'text_textfield',
   ),
   'display' => array(
    'default' => array(
      'label' => 'inline',
     ),
    ),
  );

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

📌 Task
Status

Active

Version

1.0

Component

Code

Created by

🇮🇳India Ultratanmy

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.71.5 2024