1

I'm looking to create an entity reference field that can reference either nodes from a specific bundle, or users. Is their a way to do this either with a module , or programmatical,y?

  • You don't. Entityreference doesn't support it, and it's a sign of a bad architecture. Have two separate fields. – Bojan Zivanovic Dec 24 '13 at 21:16
0

I do not think you can do this with the Entity reference module. Look at its code:

/**
 * Implements hook_field_info().
 */
function entityreference_field_info() {
  $field_info['entityreference'] = array(
    'label' => t('Entity Reference'),
    'description' => t('This field reference another entity.'),
    'settings' => array(
      // Default to the core target entity type node.
      'target_type' => 'node',
      // The handler for this field.
      'handler' => 'base',
      // The handler settings.
      'handler_settings' => array(),
    ),
    'instance_settings' => array(),
    'default_widget' => 'entityreference_autocomplete',
    'default_formatter' => 'entityreference_label',
    'property_callbacks' => array('entityreference_field_property_callback'),
  );
  return $field_info;
}

I suggest you try a work-around. For example, you could create a content type that includes two entity-reference fields, one of type Node and one of type User. Maybe, for convenience, include a boolean or integer field indicating which of the two to use. Then use references to your new content type where you want to use node-or-user entity references.

Alternatively, you could use the Field collection module, which works the same way, creating node types to handle collections of fields: https://drupal.org/project/field_collection.

0

For D8 this is now possible with the Dynamic Entity Reference field:

https://www.drupal.org/project/dynamic_entity_reference

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.