Using an entity reference

Entities are used to store and display data, which could be a node, a user, a taxonomy term, or a custom development. In this example, we'll look at a simple site for a kids camp using the Drupal Gardens platform to showcase this functionality. On this site, we'll have entities for camp counselors, locations, and the activities.

  1. Ensure that the Entity reference module is enabled for your website.

    To do this, go to Modules and enable the Entity Reference module if it's not selected. Drupal Gardens users require a Professional subscription to have this module. If you are not using Drupal Gardens, you can download and enable this module with Drush, or you can place it in /sites/all/modules and enable the module on the Modules page.

  2. Add a field to the users on this website that allows users to enter their camp nickname.

    You set this under Configuration > Account settings > Manage fields. Add the new field with the Label Camp Nickname and a Field Type of Text. Be sure to click Save to save your new field, and then click Save settings to save the default settings for the field.


  3. Click the Manage display tab, and change the display settings of the user, so that the nickname's Label is set to Hidden, and the History field has a Format of Hidden. Again, be sure to click Save to save your changes.


  4. Create some users for the site. We could create a new Role for the counselors, but for now, we'll leave them as authenticated users with no additional roles. There is always the option to later create a new Role. If you don't know how to create users, see creating users.
  5. Create a content type called Locations (Structure > Content types > +Add Content type) for the locations where the activities will take place. For this content type, all that is required is the Title field. Enter a Name and then click Save and add fields.


  6. Using the Locations content type, create three different locations:
    • Barn
    • Field
    • Beach
  7. Create the Activity content type. It must contain fields for the Title, Body (for a description of the activity), a Date field type, and fields that use Entity Reference to add the counselors and location.


  8. For the Counselor field, give it the Label of Counselor, and then select a Field type of Entity Reference. Select the Select list widget for selecting the user. The configuration of the field looks like the following, including a Target type of User.


  9. The setup for the Location is similar. To change things a bit, make the widget using Check boxes/radio buttons. When setting up the field, it is of the Target type of Content.


  10. In order for the Counselor name to be displayed to all users, go to the Manage Display tab, and then change the Format to Rendered entity.


  11. Create some content using the Activity content type. For this example, here are three activities:
    • Horseback riding
    • Soccer
    • Sailing

    Each will have the following:

    • Title
    • Body (description of the activity)
    • Time
    • Counselor (this is using the reference to the users on the site)
    • Location (this is using the reference field for the Content type: Locations)

Here's an example of the created content for the activity of soccer. We have a display of the details of the activity, the time of the activity, and (through the use of the Entity Reference) we're also displaying information about the counselor and the location.


Contact supportStill need assistance? Contact Acquia Support