How to reference nodes and other entities in your content in Drupal 7

Referencing other nodes, users or other entities in your content is pretty straightforward in Drupal 7. All you need is to install the Entity Reference module and you are on your way.

This tutorial will show you how you can reference nodes in your content. In other words, how you can have a node which contains a field that will display a link to another node (or multiple nodes) of any Content Type. The use case for this functionality is very broad, so this tutorial will focus on an abstract example to get you started.

First, prepare your 2 Content Types. A main one (for this example called Articles) and a referenced one (for this example called Related). Next up, install the Entity Reference module and its dependencies (Entity API and CTools) if you haven’t already done so.

Once everything is enabled, go to Structure – Content Types – Articles (or whatever your main Content Type is called) and add a new field of the type Entity Reference. As a widget you can choose one depending on your actual purpose. I usually go with autocomplete. Click Save and configure the new field.

Under Target Type select Node (you have other choices there too, feel free to experiment) and under Target bundles, the Content Type(s) you want available for referencing, in my case Related. Go through the rest of the configuration and Save.

Now, go to the Manage Display tab of your Content Type, and make sure your new field is visible in any View Modes and position you want.

The result: from now on, when you create a new Articles node, you can select in your autocomplete widget (or whichever type you chose) a Related node to reference. Of course you need to have had created nodes of the Content Type Related in order for the autocomplete widget to find them. Unleeeeess, you use the Inline Entity Form module - which makes life much easier.

And once the Articles node has been created with the Related node referenced, you should be able to see your new field with a link to the assigned Related node. And you can also have these referenced nodes displayed in a block on the sidebar.

Hope this helps!

Comments

A nice helpful article indeed. You explained Inline entity form module very well. I have a complex case scenario and I need urgent help. If you could just look at it and could suggest some hint, I will be very grateful to you.
On new account/registration page, along with other details user is asked to select a particular taxonomy term (hierarchical select) like: mobile>mobile phones / mobile accessories atc. or real estate > flats for sale/ appartments on rent/paying guest etc. I have separate Content types for each of these taxonomy terms with a particular taxonomy term set as default category. LIke Mobile phone content type has mobile phone term set as default category.
I want to lead users on new account registration to add a particular content type selected automatically on the basis of his chosen category in registration form.
How to do it.
I have created Item Category with term and hierarchical select widget. User select the item category on registration page.
Now task is to present Add Particular content type form on the basis of his chosen category.
For this I created another Ref. Category with field type Entity reference and Inline entity form as widget. Now on filed edit page I selected Target as Node and then selected all the node types, out of which one particular type needs to be presented before user.

Now on new account registration page, Inline entity form is furnishing an Ad Content form .. but it is doing it randomly. That is expected as I have not yet told it to select a particular content type only. I tried for hours, but unfortunately I could not figure out how to do it. How can I direct Inline Entity Form to select a particular node type on the basis of chosen term category by the user?
Please help. Thanks & Regards

Add new comment