EVA and Entity Reference Use Case How-to

This article describes the building of a practical example use case using modern Drupal 7 modules and site building techniques. I will show how to use Views, EVA, Fieldgroup, Entity Reference, Entity Reference Prepopulate, Display Suite modules to display content from multiple content types and other Drupal Entities on an article content type page. The goal is build a review system for the article content type.

Modules

Modules used in this how-to.

Display Suite

Display Suite allows you to take full control over how your content is displayed using a drag and drop interface. Arrange and configure the fields for your nodes, views, comments, user data etc. the way you want without having to work your way through dozens of template files.

https://drupal.org/project/ds

EVA

"Eva" is short for "Entity Views Attachment;" it provides a Views display plugin that allows the output of a View to be attached to the content of any Drupal entity. The body of a node or comment, the profile of a user account, or the listing page for a Taxonomy term are all examples of entity content. The EVA views display provides an interface in the Views UI attach a view as a field to any Drupal Entity. It also provides interface to select any value of the Entity that the EVA view field is attached to, as a contextual filter in the view. Allows the options and flexibility of views to be used in your content displays.

You can use EVA to display only fields values for a particular piece of content, giving incredible control over the display and formatting flexibility of the fields. For example you may desire to have two fields concatenated in some special way. You can add your fields to your EVA display, set the contextual filter to NID, add a Global: Text Field, and using tokens format your fields with HTML. Don’t forget to exclude your fields from display if you are going to use them together in a Global: Text Field. Example: You may have a city, state, and zip fields. You can combine them in a Global: Text Field in Views to display as “City, State ZIP”. When you manage the display for your content type add the EVA just created to the display and whenever a node the type is displayed it will pass its nid to the EVA and the EVA will return the fields you select, formatted as you desire.

https://drupal.org/project/eva

Entity Reference and Entity Reference Prepopulate

Entity Reference provides a field type that can reference arbitrary Drupal Entities. Entity Reference Prepopulate allows prepopulating any entity reference field via URL argument.

http://drupal.org/project/entityreference

https://drupal.org/project/entityreference_prepopulate

Fieldgroup

Fieldgroup will, as the name implies, group fields together. All fieldable entities will have the possibility to add groups to wrap their fields together. Fieldgroup comes with default HTML wrappers like vertical tabs, horizontal tabs, accordions, fieldsets or div wrappers

http://drupal.org/project/field_group

How-to Guide

Create a “Review” content type.

  • Add an entity reference field (Label: Article Reference fieldname:field_article_reference) to the view which will be prepopulated by a url argument via a link created by a custom Display Suite code field.
  • In the settings for the entity reference field in the “Additional behaviors” group check the “Entity reference prepopulate” checkbox. For the action property set Disable field. This shows the article that is referenced but doesn’t allow the user to change it when creating or editing review.
  • Create Display suite code field
  • Check the Node value for the Entities field. This will attach the custom field to Nodes (Content).
  • Enable tokens for the custom code field by checking the Token check box below theField Code
  • Set ‘Text Format’ to ‘Display Suite Code’ and create token html code for a link where field_article_reference is the name of the entity reference field created in step one and added to the article content type

Create a Review for Article EVA

  • Create a new view listing Content of Type Review
  • Add an EVA view display.
  • Enter a value for the view Title, "Reviews"
  • In the Entity content settings category
    1. Set Entity to Content (Nodes)
    2. Set Bundle to Article
    3. Leave Arguments set to id
  • Add a contextual filter that is the entity reference field of the Review content type.
    1. Add Contextual Filter: Content: Article Reference (field_article_reference)
    2. Under When the filter value is NOT available set hide view
  • Sort by postdate.
  • Display an unformatted list of Reviews teasers.

Add view to Article Display

  • Manage the display for the Article content type. For the Default or the Full Content display modes select custom display suite layout, for example (One Column)
  • Add a horizontal tab group Field group and two horizontal tab items Fieldgroups. Nest the two tab items in the tab group. Nest the article body and other fields (comments, links, title) in the first tab labeled “Article”.
  • Nest custom Display Suite Field second tab item labeled “Reviews”.
  • Nest the review list eva field in Reviews tab after the custom field.

Tags: