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.
Continuing with the previously discussed example, let’s create three content types - Book, Author, and Publication with the following fields:
Text (formatted, long, with summary)
Text (formatted, long, with summary)
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.