5

first of all, English is not my native language so sorry if this reads bad.

I'm trying to program a field collection with field collection item fields like the code as given by rbruhn here: https://drupal.org/comment/4653514#comment-4653514

I want to create it on an easier leavel for testing purposes. My goal is to create a taxonomy reference dropdown and a textfield.

I manage to reproduce the field_collection type. Hower, 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, this would make sense to me (but it doesn't work). Can someone tell me what I do 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']);
  • This one just creates a text field not a collection. – Potney Switters 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.