Drupal 8 introduced Entity Reference as a part of Drupal core. This allows entities (such as nodes, taxonomies, users, file, etc) to add a reference to other entities out of the box. This relationship may be a one-to-one or one-to-many. The referenced item is called a parent entity and the referencing item is known as child entity.
The usual workflow expects the users to add the parent entity first, and then add a reference in the child entity. Now, in case of complex sites, with multi-level relationships, this can become very cumbersome.
Consider an example, let's say we have a Book content type which has two reference fields - Author and Publication. A book may have more than one author, hence it is a one-to-many relationship. Moreover, a book must have at least one author and a publication. In this case, creating an instance of a book requires its parent entities, that is, its author and publication to be created beforehand.
Therefore, a need for a better workflow arises. A contributed module, Inline Entity Form solves this problem by providing widgets for creating, modifying, and removing parent(referenced) entities in the same editor as creating the child(referencing) entities.
Prerequisites
Inline Entity Form requires Entity API module, which is now a part of Drupal core.
Downloading and Installing Inline Entity Form
As of writing this article, Inline Entity Form is in its beta release for Drupal 8. As a word of caution, be careful while installing the beta versions of modules on a live Drupal site.
Follow any of the below methods to install the Inline Entity Form module on your Drupal site:
Using UI
Navigate to Manage → Extend → Install new module and enter the .tar.gz or .zip URL of the Inline Entity Form module and hit Install Installing Inline Entity Form using FTP
Once the downloader and installer are finished downloading, click on “Enable newly added modules”.
Find Inline Entity Form in the list of modules, select it and click on Install.
For rest of the methods, first, navigate to the root directory of your Drupal site,
$ cd /var/www/html/drupal8
Using Composer
Add Inline Entity Form package using composer
$ composer require "drupal/inline_entity_form"
Enable Inline Entity Form module by navigating to Manage → Extend, selecting Inline Entity Form and click Install
Using Drush
Download and enable Inline Entity Form using Drush
$ drush dl inline_entity_form
$ drush en inline_entity_form -y
Using Drupal Console
Download and install Inline Entity Form using Drupal Console
Continuing with the previously discussed example, let’s create three content types - Book, Author, and Publication with the following fields:
Book
Field Label
Field Type
Name
Title
Description
Text (formatted, long, with summary)
ISBN
Number (integer)
Author
Entity reference
Publication
Entity reference
Price
Number (float)
Author
Field Label
Field Type
Name
Title
Picture
Image
Bio
Text (formatted, long, with summary)
Publication
Field Label
Field Type
Name
Title
Location
Text (plain)
Creating Content Types With Entity Reference Fields
Navigate to Manage → Structure → Content Types and click on “Add content type”
Give a name and description to the content type. Rename the Title field as per requirement and click “Save and manage fields”. Click on “Add field”.
Select “Content” under “Add a new field” drop-down and give a suitable label. Click on “Save and continue”.
Now depending on your relationship type - one-to-one or one-to-many, set “Allowed the number of values” to Limited (1), Limited (n) or Unlimited and click on “Save field settings”.
Check “Required field” if the referenced entity has a total participation in this relationship. Select the parent content type(s) and hit “Save settings”
Repeat the steps 2 to 5 for each of the fields.
Setting Up Inline Entity Form
Navigate to Manage → Structure → Content Types
Click on the drop-down menu against the child entity and select Manage Form Display
Change the widget of the reference fields to “Inline entity form - Complex”. Additionally, click on the gear icon, and check Allow users to add existing nodes. Note that the “Inline entity form - Simple” does not allow the addition of existing nodes.
Form Display settings for Book content type
Creating And Referencing An Entity While Creating Another Entity
Now let’s create an instance of Book using the Inline Entity Form:
Navigate to Manage → Content and click on “Add content”.
Select the child content type, ‘Book’ in this example. Note that the entity reference fields are presented in a new widget. We can add new parent nodes, add existing nodes, and even edit the existing nodes using this widget.
Click on “Add new node”. An inline form appears that allows the creation of new parent node without leaving this page. Inline Entity Form - Adding existing nodeInline Entity Form - Adding new node
Fill in the fields of the parent node
Click “Save” to create the content.
Inline Entity Form allows management of referenced entities on the fly, thereby saving a lot of time and effort. This is particularly useful for sites with many entities having complex relationships, such as sites built using Drupal Commerce.
E-commerce sites having a listing of the product items can take immense advantage of Inline Entity Form. In such cases, the Product Displays can contain references to Product items, which can be easily managed using the inline widget.
A BIG SHOUT OUT to the bloggers and evangelists willing to impart their knowledge with their writing.
Share your experience and opinion with us and let the world be the stage to your ideas and work. Share your piece with us at [email protected]. We also welcome ideas in the planning phase.
What Do We Expect?
The content should align with our interest in web development and open source technology. However we are open to topics from in and around the industry.
An average blog post should be of around 1000 words, but this is of course subjective.
We love crazy and colourful content. Feel free to link awesome pictures, infographics, stats, and all.
We are also open to articles with cool tips and tricks, some How-to and step by step guide as well