Creating a view using an Entity Reference

In another article, Using an Entity Reference, we looked at using the Entity Reference module to relate other entities to one content type. We set up an example of a site for a kid's camp using a Drupal Gardens site. We created a content type of Activity that referenced two other entities: a node called Location and a member on the site that is a Camp Counselor. In this article, we will take things a bit further and display all the activities that are happening on a particular day through the use of the Views module.

Objectives

Before getting into the details of configuring the content types and the view, let's take a look at what the objective will be for the view. Having the desired outcome in mind will help ensure that all the content is set as needed before the view creation process.

  • Display the view as a table.
  • The view will show Activity, Details, Date & Time, Counselor and Location fields.
  • The listed Activity link will take the user to the full node display of the individual event.
  • The Counselor field will link to the page that contains the user details that includes the Bio information for that person.
  • The Location links will take us to the full node of each location that contains more information about each particular place.

The following screenshot is the final result we're after:

erv-1.png

Adding more fields

In the Using an Entity Reference article, the referenced entities for instruction were created, but adding some more fields to these entities will be helpful and more informative. The display settings will also be altered.

  1. For the Location, add a field for the Location Description with the type Long text.
  2. Add a field for Rules and Regulations where the field type is set as Long text and summary.
  3. Add a field for a Location Map that is set as an Image field.
  4. You can update the test nodes for the Barn, Beach and Field locations that have been created.

    erv-2.png

Customize an entity

The Counselor user entity had only contained the Drupal Gardens username, a field for a camp nickname and their avatar. For this entity, some customization will be required based on the desired final output.

  1. Go to Configuration > Account settings, and in the Personalization section clear the Enable user pictures check box. Click Save configuration.

    This prevents all of the Counselor data from being displayed on the Activity nodes. The only way to have hide this field on the Drupal Gardens site would be to use CSS. It's easier to just hide the option instead.

  2. Add a field for the Counselors to add their Bio by going to Configuration > Account settings and then clicking the Manage fields tab. The Bio field is of the type Long text and summary.

Change the display options

For the Manage display options, change the following options:

  1. Open the Custom display settings and select both the User account and the Tokens.

    erv-3.png

    The User account display will be for the Counselor member display page. The Tokens display will be used in the Activity content type to only display the Counselor nickname.

  2. Click Save.
  3. Click the User account display button near the top right, and then choose the type of labels desired (for example, Inline or Above).

    erv-4.png

  4. Click Save.

Setting the token display

Click the Tokens button to open the display. For thsi display, set the Camp Nickname as the only field visible. Set it's label as Hidden. If the display is not enabled this way, when a site visitor went to an Activity page, the information would show as:

Counselor:

Camp Nickname: [nickname]

Bio: [Bio info]

By being able to access the Tokens display on the Activity content type, the information will instead be shown as:

Counselor: [nickname]

erv-5.png

You can then update the test Counselors on the site. In this example, three have been created.

Adjust the Activity type

  1. The last configuration change needed before creating the view is to make some adjustments to the Activity content type.
  2. Add a field to add a tag for the day of the week. This will be used to filter the results when creating the view. It is of the type Term reference. With this, use a select list for the days of the week.

    erv-6.png

  3. Click Save.
  4. Now you can use the Tags vocabulary to set the day of the week. For more information about adding tags to content, see the Taxonomy documentation on drupal.org.

    erv-7.png

  5. On the Manage display tab, hide the field as it is not information the site visitor needs to see, as it's just there for the view filtering purposes.

    erv-8.png

  6. Also, for the Default display of the content under Manage display, the setting for the Counselor field needs to be enabled for the format setting of Rendered entity. The View mode is set to Tokens. As noted earlier, this setting is used so that on the full display of the content, the Tokens display setting of the User entity will be used.

    erv-9.png

The following is a sample of a full node display of an Activity.

erv-10.png

With the content updated, we have what we need to create the view to display the Activities.

There are three activities we are going to display; Horseback Riding, Sailing and Soccer. The activities will be displayed based on the day of the week that has been selected. We will walk through this process in part 2 of this article.

Contact supportStill need assistance? Contact Acquia Support