0

In Drupal 7, I am looking to create a field in a content type that can either reference another specific content type and/or also reference a user. I am creating the content type programmatically in the .install file and am looking to be able to reference both types in the same field.

It currently works with one or the other as:

  'field_sport_team_members' => array(
    'field_name' => 'field_sport_team_members',
    'label' => 'Team Members',
    'cardinality' => FIELD_CARDINALITY_UNLIMITED,
    'type' => 'node_reference',
    'description' => 'The members of the team',
    'weight' => '3.5',
    'required' => 0,
    'settings' => array(
      'referenceable_types' => array('club_person'),
    ),
  ),

for the reference to the content type and

'field_club_person_user' => array(
  'field_name' => 'field_sport_team_members',
  'type' => 'user_reference',
  'label' => 'Team Members',
  'description' => 'If this entry corresponds to a user, you can select the user here.',
  'weight' => '3',
  'cardinality' => FIELD_CARDINALITY_UNLIMITED,
  'translatable' => TRUE,
  'locked' => FALSE,
  'settings' => array(
  'referenceable_roles'  => array (
    4 => '4', //captain
    5 => '5', //player
   ),
   'referenceable_status' => array(
     1 => '1',
     0 => 0,
    ),
   ),
  ),

for the reference to the user type.

I guess my real question is now with the advent of "entities", is it possible to make a entity reference, but also to specify which content types to search for as well?

4

There are a few ways to do this. You said "and/or", you could use Field Collection and add a node reference and a user reference to it. See the field collection module.

The other option would be the relation module or the entity reference module which can both reference any "entity." The former is a lot more complex. Entity reference has a usable widget with autocomplete which might be exactly what you need. To narrow the entity reference down to the exact content types you want along with users, you will probably have to write a custom widget. You can see the default widget implementation in the source.

|improve this answer|||||
  • How would I incorporate those modules into the install file? Would I need to read through their functions and reference them in the .install? Or do they provide more options for me to put in the content types I'm trying to make in the .install? Thanks! – belvedere Feb 6 '12 at 21:31
  • After you add the modules as a dependency to your module, you can refer to their specific sources to see how they are defined with code. I couldn't tell you off the top of my head. – Jonathan Rowny Feb 6 '12 at 21:46

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.