Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Is there a way to create a Field Collection with Fields when you install a module, using field_create_field and field_create_instance? I tried dumping out the instances with this snippet:
$entity_type = 'myentity';
$field_name = 'field_table_rates';
$bundle_name = 'myentity';
$info_config = field_info_field($field_name);
$info_instance = field_info_instance($entity_type, $field_name, $bundle_name);
unset($info_config['id']);
unset($info_instance['id'], $info_instance['field_id']);
include_once DRUPAL_ROOT . '/includes/utility.inc';
$output1 = "field_create_field(" . drupal_var_export($info_config) . ");\n";
$output2 = "field_create_instance(" . drupal_var_export($info_instance) . ");";
krumo($output1);krumo($output2);exit;
It seems to create the Field Collection and its fields fine, but does not create an instance. Also, the field itself is not able to be selected and attached via the Manage Field screen, its not listed in the group of available fields.
Comments
Comment #1
jmuzz CreditAttribution: jmuzz commentedI'm not sure what myentity and field_table_rates are. My guess is that field_table_rates is the name of a field collection bundle that was made before this part of the code. If that is the case, the field for it will not exist yet, field_info_field will not return something you can use and you will need to supply your own values to field_create_field.
If that doesn't help, can you explain what myentity and field_table_rates are?
Comment #2
jmuzz CreditAttribution: jmuzz commented