1

I have created an entity via custom module. This entity does not have any bundle and it gets created perfectly fine. entity_get_info says it is not necessary to have bundles.

Next, I create a field for entity reference using field_create_field and that works fine too. Now, when i want to associate this field as an instance to my custom entity and when I call field_create_instance (without specifying bundle), I get the error saying:

drupal FieldException: Attempt to create an instance of field without a bundle

I go on to see that bundle is a required field in field_config_instance .. I tried to specify bundle as the same name as entity but that did not work for me either. Actually, it did create an instance but on the entity add/edit form when I do a field_attach_form, I get another error:

Notice: Undefined index: target_bundles in EntityReferenceHandler_base->buildEntityFieldQuery() (line 174 of /Applications/MAMP/htdocs/MYPROJECT/sites/all/modules/entityreference/handler/base.inc).

Any help will be highly appreciated.

Mukesh Agarwal
  • 204
  • 1
  • 2
  • 7

1 Answers1

3

When creating an instance for your field, you need to make sure you specify the bundle name. In case you dont have any bundles for your entity, by default a single bundle is created with the same name as that of your entity. This is what helped:

'entity_type' => 'MY_ENTITY', 'bundle' => 'MY_ENTITY',

The entire workflow can be reached at my blog: Use entity reference in your custom entities without bundles

Mukesh Agarwal
  • 204
  • 1
  • 2
  • 7