5

I'm trying to program a field collection with field collection item fields like the code as given by rbruhn on Creating field collection programatically. I want to create it on an easier level for testing purposes. My goal is to create a taxonomy reference dropdown and a textfield.

I managed to reproduce the field_collection type. However, I can't reproduce any field collections item fields.

Let's say I want a textfield in my field collection. Creating an ordinary textfield field collection item field would make sense to me, but it doesn't work.

Can someone tell me what I am doing wrong?

Instead of appearing inside my field collection, it appears as a field inside my node which contains my field collection field.

    array(
      'field' => array(
        'field_name' => 'name_of_my_field',
        'type' => 'text',
        ),
      ),
      'instance' => array(
        'field_name' => 'name_of_my_field',
        'entity_type' => 'field_collection_item',
        'bundle' => 'name_of_my_field_collection_field',,
        'widget' => array('type' => 'text_textfield'),
      )
    ),
0

This works for me (my field collection is called field_fc_test). I imagine the problem is with the way you are calling field_create_instance.

$new_field = array(
  'field' => array(
    'field_name' => 'name_of_my_field',
    'type' => 'text',
    ),
  'instance' => array(
    'field_name' => 'name_of_my_field',
    'entity_type' => 'field_collection_item',
    'bundle' => 'field_fc_test',
    'widget' => array('type' => 'text_textfield'),
  ),
);
field_create_field($new_field['field']);
field_create_instance($new_field['instance']);
2
  • This one just creates a text field not a collection. Feb 11 '15 at 14:34
  • This creates a text field within a field collection named "field_fc_test". You would need to modify this code to create a field collection.
    – Thomas4019
    Feb 12 '15 at 16:52
0

Here is some sample code to create a field collection called carousel_slides:

$field = array(
  'settings' => array(
    'hide_blank_items' => 1,
    'path' => '',
  ),
  'field_name' => 'carousel_slides',
  'type' => 'field_collection',
  'cardinality' => '-1',
);
if (field_info_field($field['field_name']) === NULL) {
  field_create_field($field);
}

And then to create the instance:

// carousel_slides instance
$instance = array(
  'label' => 'Slides',
  'widget' => array(
    'weight' => '1',
    'type' => 'field_collection_embed',
    'module' => 'field_collection',
    'active' => 0,
    'settings' => array(
    ),
  ),
  'settings' => array(
    'user_register_form' => FALSE,
  ),
  'display' => array(
    'default' => array(
      'label' => 'hidden',
      'type' => 'field_collection_view',
      'weight' => '1',
      'settings' => array(
        'edit' => '',
        'delete' => '',
        'add' => '',
        'description' => 0,
        'view_mode' => 'full',
      ),
      'module' => 'field_collection',
    ),
  ),
  'required' => 0,
  'description' => '',
  'default_value' => NULL,
  'fences_wrapper' => 'ul', // You probably won't need this unless you are using the fences module
  'field_name' => 'carousel_slides',
  'entity_type' => 'node',
  'bundle' => 'carousel',
  'deleted' => '0',
);
if (!field_read_field($instance['field_name'])) {
  field_create_instance($instance);
}

Here I'm doing checks to make sure that the field and instance doesn't exist first to make sure it won't cause an error and stop updates running when this code is in hook_update_N in an environment with a database that could potentially be out of sync.

There are also many properties here that you probably don't need. These arrays were created programmatically by exporting the field directly from Drupal with a script.

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.