4

I'm looking for example code that demonstrates creating a new entity reference field with multiple values, referring to multiple different bundles.

3

Sorry for the long code dump but there's really no other way to do this:

field_create_field(array(
  'translatable' => '0',
  'entity_types' => array(),
  'settings' => array(
    'target_type' => 'node',
    'handler' => 'base',
    'handler_submit' => 'Change handler',
    'handler_settings' => array(
      'target_bundles' => array(
        'article' => 'article',
        'page' => 'page',
        'webform' => 'webform',
      ),
      'sort' => array(
        'type' => 'none',
        'property' => 'nid',
        'field' => 'body:value',
        'direction' => 'ASC',
      ),
      'behaviors' => array(
        'views-select-list' => array(
          'status' => 0,
        ),
      ),
    ),
  ),
  'storage' => array(
    'type' => 'field_sql_storage',
    'settings' => array(),
    'module' => 'field_sql_storage',
    'active' => '1',
    'details' => array(
      'sql' => array(
        'FIELD_LOAD_CURRENT' => array(
          'field_data_field_entity_reference' => array(
            'target_id' => 'field_entity_reference_target_id',
          ),
        ),
        'FIELD_LOAD_REVISION' => array(
          'field_revision_field_entity_reference' => array(
            'target_id' => 'field_entity_reference_target_id',
          ),
        ),
      ),
    ),
  ),
  'foreign keys' => array(),
  'indexes' => array(
    'target_id' => array(
      'target_id',
    ),
  ),
  'field_name' => 'field_entity_reference',
  'type' => 'entityreference',
  'module' => 'entityreference',
  'active' => '1',
  'locked' => '0',
  'cardinality' => '-1',
  'deleted' => '0',
  'columns' => array(
    'target_id' => array(
      'description' => 'The id of the target entity.',
      'type' => 'int',
      'unsigned' => TRUE,
      'not null' => TRUE,
    ),
  ),
  'bundles' => array(
    'node' => array(
      'page',
    ),
  ),
));

The target_bundles in the handler_settings is where you assign the different bundles, and the cardinality of -1 ensures unlimited values for the field.

In case you're interested, this is the code that allowed me to build the above definition from an existing entity reference field (which seems to have originated at this blog post):

$entity_type = 'node';
$field_name = 'field_entity_reference';
$bundle_name = 'article';

$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';
$output = "field_create_field(" . drupal_var_export($info_config) . ");\n";
$output .= "field_create_instance(" . drupal_var_export($info_instance) . ");";
drupal_set_message("<textarea rows=30 style=\"width: 100%;\">". $output .'</textarea>');

It's a handy little script; I'm sure I've posted it somewhere here before but I can't find it at the moment.

| improve this answer | |
  • I've seen the same script before (and commented on it to) ... I saw it first at this blog post – tenken Jul 3 '12 at 14:46
  • @tenken Thanks I always wondered where I got that from; now I can provide proper attribution – Clive Jul 3 '12 at 14:48

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.