Create Entity Reference Pre-populate Add Links using Views in Drupal 7 ( Tutorials )



This post is about adding an entity reference pre-populate link to a node in Drupal 7. Entity Reference Pr-populate module is such a powerful module but lacks the features of adding link out of the box. The method discussed utilities the flexibility of Views in Drupal 7 to achieve this.

Required Modules:
- Entity Reference
- Entity Reference Pre-populate
- Views
- EVA (i.e. Entity Views Attachments)

1. CREATE CONTENT TYPES:

For the sake of this example, I have created two content types -
- College
- Course
 
 
 
2. CREATE ENTITY REFERENCE FIELD

Create a Entity Reference field using autocomplete widget on the "Course" content type. This field can be set to required field if needed.

Steps:
 
- Go to /admin/structure/types and click on "Manage fields" under "Course" content type.
 
- Add a new Entity Reference field - "field_college_er"
 
 
 
- Check the "Enable Entity reference prepopulate" box, select "Disable field" for Action and "Do nothing" for Fallback behavior. Set Skip access permission to "Administer site configuration" but you can set anything you want as per your requirement.
 
 
 
- In the "College" field settings, leave the Number of values at 1, Target type is "Node", and select "College" for the Target bundles.
 
 
 

- Save the settings and you should be able to see the newly added Entity Reference field "field_college_er" in action when you go to Create Course Content.


 
NOTE: Our goal is to pre-populate "field_college_er" field from a link displayed on "College" content type node. It will be achieved by EVA and Views module - follow the steps below.

3. CREATE VIEWS USING EVA

- Now, create the Views to be able to display "Add new Course" links on parent node (i.e. College). We shall be using EVA to display this link on the "College" content type nodes.

- While at "admin/structure/views", click the "Add new view" link, name it "Add new course" and select Show Content of type "All". Un-check create a page, and leave create a block unchecked and then click on "Continue & Edit".

 
 
 
- Use the "+Add" button near the top, to add an "EVA Field" display

 
 
- Under the "ENTITY CONTENT SETTINGS" pane, select "Node" for the "Entity type", select "College" for Bundles (i.e. the content types that can be referenced by the "field_college_er" Entity Reference field on the "Course" content type), and leave Arguments at id. I opted not to display the title of the View display by leaving "No" set for Show title. It should look like the below screenshot after EVA Views display has been configured completely - keep following for individual settings for other panes.
 
 
- Under the "FIELDS" pane, Add the "Content: Nid" field and remove the Title field. Follow the below screen and expand and check the "Rewrite the output of this field" and type this code - <ul> <li><a href="/node/add/course?field_college_er=[nid]">Add new Course</a></li> </ul>
 
 
 

- Under "FILTER CRITERIA", add a filter of "Content: Type" and set it to "College" so that it will only be visible on college content type. Also, set it to display on only publishes contents.


- Under "SORT CRITERIA" pane, set it to "Post date".

- Now, Set the "CONTEXTUAL FILTER" on this display so that it is used only in the context of a node referenced by "field_college_er" field on the Course content type. Expand the "Advanced" pane, and click the "Add" button next to Contextual Filters and Select "Content: Nid", listed as "Content: Nid" which will be used as the Contextual Filter. There are couple of setting which you need to do as follows -
 
-- Under "When the Filter in Not Available", select the "Provide default value" radio button, and then select "Content ID from URL".
 

 
 
-- Under "When the Filter Value is Available or a Default is Provided", check the "Specify validation criteria" radio button, select "Content" for the Validator, and then select "College" (i.e. the content type which is referenced by "field_college_er" from Course content type). Also, Leave the select list option for "Action to take if filter value does not validate" to "Hide view".
 
 
 

-- Click the apply button to save the new contextual filter, and then save your view.


- Now, all the setup is complete and you should see the Entity Pre-populate Link "Add new Course"  while viewing any content node of content type "College". Once clicked, it will open the "Create Course" form with pre-populated link of "field_college_er" field.
 
 

 
 

1 comment:

Commonly Used Abbreviations Used Daily

Although, we tend to use short form of long words in daily life but there are many abbreviations which we don't remember. Below is the l...