I'm trying to add a entityreference field linking two bundles of a same entity using Field API, but the following code does not seem to work (field is created in db and appears in admin UI, but widget and field settings are not set at all):

function booking_install() {
    ...
    $field = array(
      'field_name' => 'artist_ref',
      'cardinality' => 1,
      'type' => 'entityreference'
    ); 
    field_create_field($field);
        
    $instance = array(    
      'field_name' => 'artist_ref',
      'label' => 'Artist',
      'widget' => array(
         'type' => 'options_select',
      ),
      'entity_type' => 'booking',
      'bundle' => 'gig',
      'target_bundles' => array('artist'),
    );
    field_create_instance($instance);
}

Here entity type 'booking' contains two bundles 'gig' and "artist', the intent is to add a field 'artist_ref' to gig referencing an artist.

Something must be wrong with my code. Could that basic use case be documented somewhere?

Comments

ParisLiakos’s picture

thats how i managed to do it,after hours of debugging:/

  $field = array(
    'field_name' => 'your_field_name',
    'type' => 'entityreference',
    'cardinality' => FIELD_CARDINALITY_UNLIMITED,
    'settings' => array(
      'target_type' => 'your_entity_type',
      'handler_settings' => array('target_bundles' => array('your_entity_bundle')),
    ),
  );
  field_create_field($field);

  $instance = array(
    'field_name' => 'your_field_name',
    'entity_type' => 'your_instance_entity_type',
    'bundle' => 'your_instance_entity_bundle',
    'label' => 'Your Label',
    'widget' => array(
      'type' => 'options_select',
    ),
    'settings' => array(
      'target_type' => 'your_entity_type',
      'handler_settings' => array('target_bundles' => array('your_entity_bundle')),
    ),
  );
  field_create_instance($instance);
ParisLiakos’s picture

Version: 7.x-1.0-alpha1 » 7.x-1.x-dev
Category: support » task

Please add the example, in a README.txt or here in d.o so it will be easier to find and people wont waste hours:)

unistereo’s picture

Cheers rootatwc, I got it working thanks to you!

For the record, this slightly shorter version seems to be working too (at least for me):

<?php

  $field = array(
    'field_name' => 'your_field_name',
    'type' => 'entityreference',
    'cardinality' => FIELD_CARDINALITY_UNLIMITED,
    'settings' => array(
      'target_type' => 'your_entity_type',
      'target_bundles' => array('your_entity_bundle'),
    ),
  );
  field_create_field($field);

  $instance = array(
    'field_name' => 'your_field_name',
    'entity_type' => 'your_instance_entity_type',
    'bundle' => 'your_instance_entity_bundle',
    'label' => 'Your Label',
    'widget' => array(
      'type' => 'options_select',
    ),
  );
  field_create_instance($instance);
?>

Here the 'settings' array is only used once (for field_create_field) and has a slightly different structure.
Can't remember where I stole that code from, nor whether that version is better or worse, but here it is.
Now can only wait on maintainers to approve or suggest a proper method.

ParisLiakos’s picture

Oh weird, i tried this but didn't work :)
thats why i used the settings array on both callbacks.but w/e glad you made it:)

soshial’s picture

It would be great to have even a simple explanation in readme how to create such fields programmatically!

rogueturnip’s picture

This worked for me until I tried to view the entity with the reference.

I get the following error when only the label is to be displayed:
Notice: Undefined index: entity in entityreference_field_formatter_view() (line 643 of /Users/tony/Documents/Web Sites/drupal7/sites/all/modules/entityreference/entityreference.module).

If I try to put a link with the label it causes the entity to not show at all.

Any one else have this problem? Am I missing something in the field instance?

giorgio79’s picture

Related discussion: #1388924: Programmatically create nodes with Entity reference field I found this thread by searching programmatically adding and removing references....

alexmc’s picture

I'm not sure if this is the same issue, but can you add and manipulate entity references through the REST Service

tcm5025’s picture

Just wanted to point out that #1 works and #3 does not. Hope it might help others who come here and use the wrong one first like me.

dragon658’s picture

Working code (examples above didn't work for me):

$field = array(
    'cardinality' => 1,
    'deleted' => 0,
    'entity_types' => array('commerce_notify_in_stock_request'),
    'field_name' => 'request_user',
    'foreign keys' => array(
      'users' => array(
        'columns' => array(
          'target_id' => 'uid',
        ),
        'table' => 'users',
      ),
    ),
    'indexes' => array(
      'target_id' => array(
        0 => 'target_id',
      ),
    ),
    'locked' => 0,
    'module' => 'entityreference',
    'settings' => array(
      'handler' => 'base',
      'handler_settings' => array(
        'behaviors' => array(
          'views-select-list' => array(
            'status' => 0,
          ),
        ),
        'sort' => array(
          'type' => 'none',
        ),
        'target_bundles' => array(),
      ),
      'target_type' => 'user',
    ),
    'translatable' => 0,
    'type' => 'entityreference',
  );
  field_create_field($field);

  $instance = array(
    'bundle' => 'commerce_notify_in_stock_request',
    'default_value' => NULL,
    'deleted' => 0,
    'description' => '',
    'display' => array(
      'default' => array(
        'label' => 'above',
        'module' => 'entityreference',
        'settings' => array(
          'link' => FALSE,
        ),
        'type' => 'entityreference_label',
        'weight' => 1,
      ),
      'teaser' => array(
        'label' => 'above',
        'settings' => array(),
        'type' => 'hidden',
        'weight' => 0,
      ),
    ),
    'entity_type' => 'commerce_notify_in_stock_request',
    'field_name' => 'request_user',
    'label' => 'user',
    'required' => FALSE,
    'settings' => array(
      'user_register_form' => FALSE,
    ),
    'widget' => array(
      'module' => 'entityreference',
      'settings' => array(
        'match_operator' => 'CONTAINS',
        'path' => '',
        'size' => 60,
      ),
      'type' => 'entityreference_autocomplete',
      'weight' => -3,
    ),
  );
  field_create_instance($instance);
Raman Starshykh’s picture

Issue summary: View changes

Hi guys.
There is an easy way to create all the necessary fields
Go to the admin/structure/types/manage/my_type/fields
Create field
Then we call the two functions

$instance = field_info_instance('node','name_my_field','my_type');
$field_info = field_info_field('name_my_field');

The values of these functions are used in our module

blakefrederick’s picture

Thank you Raman for #10 and #11. Creating the field in the UI, then running the two functions to get the correct values and structure of the field data saved me hours of debugging. Thank you!

Kris77’s picture

@Raman @blakefrederick Where should the two functions be performed ?

Thanks.

Raman Starshykh’s picture

@Kris77 you can use devel module - execute php code or use in your custom module - custom hook_menu or other
As a result, you get the correct data arrays