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.
- 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/modulesand enable the module on the Modules page.
- 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 Nicknameand 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.
- 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.
- 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.
- 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.
- Using the Locations content type, create three different locations:
- 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.
- 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.
- 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.
- 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.
- Create some content using the Activity content type. For this example, here are three activities:
- Horseback riding
Each will have the following:
- Body (description of the activity)
- 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.