Blog

How Inline Entity Form Helps You To Create A Reference Entity While Creating Another Entity

  • Tech
  • March 27 2018
  • 3 min read
Share

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
  1. 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
    Installing Inline Entity Form using FTP
  2. Once the downloader and installer are finished downloading, click on “Enable newly added modules”.
     
  3. Find Inline Entity Form in the list of modules, select it and click on Install.
     
  4. For rest of the methods, first, navigate to the root directory of your Drupal site,

$ cd /var/www/html/drupal8

 

  • Using Composer
  1. Add Inline Entity Form package using composer
    $ composer require "drupal/inline_entity_form"
     
  2. Enable Inline Entity Form module by navigating to Manage → Extend, selecting Inline Entity Form and click Install
     
  • Using Drush
  1. Download and enable Inline Entity Form using Drush  

$ drush dl inline_entity_form
$ drush en inline_entity_form -y

 

  • Using Drupal Console
  1. 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:

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

  1. Navigate to Manage → Structure → Content Types and click on “Add content type”
     
  2. 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”.
     
  3. Select “Content” under “Add a new field” drop-down and give a suitable label. Click on “Save and continue”.
     
  4. 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”.
     
  5. Check “Required field” if the referenced entity has a total participation in this relationship. Select the parent content type(s) and hit “Save settings”
     
  6. Repeat the steps 2 to 5 for each of the fields.
     

Setting Up Inline Entity Form

  1. Navigate to Manage → Structure → Content Types
     
  2. Click on the drop-down menu against the child entity and select Manage Form Display
     
  3. 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
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:

  1. Navigate to Manage → Content and click on “Add content”.
     
  2. 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.
     
  3. 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 node
    Inline Entity Form - Adding existing node
    Inline Entity Form - Adding new node
    Inline Entity Form - Adding new node
  4. Fill in the fields of the parent node
     
  5. 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.

Become our reader!

Get hand picked blogs directly in your inbox.
The subscriber's email address.