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?
2 Answers
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.
For D8 this is now possible with the Dynamic Entity Reference field: