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
- 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
$ drupal module:download inline_entity_form
$ drupal module:install inline_entity_form
Defining Content Types With Reference Fields
Continuing with the previously discussed example, let’s create three content types - Book, Author, and Publication with the following fields:
Field Label | Field Type |
Name | Title |
Description | Text (formatted, long, with summary) |
ISBN |
Number (integer) |
Author | Entity reference |
Publication | Entity reference |
Price | Number (float) |
Field Label | Field Type |
Name | Title |
Picture | Image |
Bio | Text (formatted, long, with summary) |
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.
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.
- 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.
In case of any query feel free to comment below.
Subscribe
Related Blogs
In conversation with Danish Usmani, CEO, OpenSense Labs
In a year-end interview, CEO Danish Usmani showcases OpenSense Labs' achievements, emphasising new client partnerships and…
Why should you prioritize lean digital in your company?
We are living in an era where the change and innovation rate is just so high. If you want your organization to reach new…
How to measure your open source program’s success?
Along with active participation, it is very important to look after the ROI of open-source projects, programs, and…