Programming fields in Drupal

Field creation

The example of adding a field as my_custom_field() to the page node type with field_create_field() and field_create_instance() is as follows:

$field = array(
    'field_name' => 'my_custom_field',
    'cardinality' => 1,
    'translatable' => TRUE,
    'type' => 'text',
  );
field_create_field($field);

$instance = array(
    'field_name' => 'my_custom_field', 
    'entity_type'  => 'node', 
    'label' => 'My custom field', 
    'bundle' => 'page', 
    'required' => TRUE,
    'widget' => array(
      'type' => 'text_textfield',
    ),
    'display' => array(
      'default' => array(
        'type' => 'text_default',
      ),
    ),
  );
field_create_instance($instance);

Field editing

The example of changing the parameter required in the existing field my_custom_field() in the page node type with the hook field_read_instance() and field_update_instance() is as follows:

  $instance = field_read_instance('node', 'my_custom_field', 'page');
  $instance['required'] = FALSE;
  field_update_instance($instance);

Deleting fields

The example of deleting an existing field my_custom_field() in the page node type with field_delete_field(), field_delete_instance() and field_info_instance() is as follows:

field_delete_field('my_custom_field');
  field_delete_instance(field_info_instance('node', 'my_custom_field', 'page'));

The hook hook_update_N() can be used for implementation of the above mentioned code.