Learn how to use the Inline Entity Form which allows you to embed entity forms within content types.
We’ll cover the following:
- What Inline Entity Form is
- Download and install the module
- How to configure the module
- How to use the module
Composer command
composer require drupal/inline_entity_form
Transcript
Trainer (00:00):
In the previous video, we created an entity reference field, which allowed us to reference custom blocks. And this lets us reuse custom blocks across multiple products. If we edit this product and look at the components field, there is one limitation with this order complete field, and that is we can only select existing products. We can’t create a new custom block from here. Let’s say we want to create a new block and add it below this Refund policy block. To do that, we need to go to structure, and let me open that up in a new tab, and then go to block layout, custom block library, create the custom block first here. And then we go back to the product edit form and reference the new block using this order complete field. This process requires jumping between different screens and sections of the site, which isn’t ideal.
Trainer (00:59):
We can streamline the process of creating new blocks by using a third party module called Inline Entity Form. To learn about the module, just go to drupal.org/project/inline_entity_form. The module provides a widget which renders other entity forms inline, hence the name Inline Entity Form. We can configure the Inline Entity Form widget on our components field, which will allow us to create a custom block directly from the product edit form without having to go to the custom block library page to create the block. Let’s start things off by first, downloading the module using Composer. So open up your terminal and then type in composer require drupal/inline_entity_form, and then hit enter. Now, you may see this error message in Composer when you are downloading the module. And the reason for that is because the composer.json file is configured to only download stable releases.
Trainer (02:10):
And if we have a look at Inline Entity Form and scroll all the way down, we can see that the only stable release is the Drupal 7 version of the module. The Drupal 8 and 9 version of the module hasn’t been declared as stable that is why Composer is throwing up that error. Now, to work around this, all we need to do is specify the version when we run the composer require command. So what I’ll do is I’ll copy the version, and then let’s jump back into our terminal, and I’ll clear that. And let me bring up the composer require command. And all you need to do is put in a colon, a caret, and then paste in the version and then hit enter. And here, you can see that composer has downloaded the module without complaining about the fact that the module isn’t stable. Now, chances are, in the future, you won’t have this problem because the module maintainer will make this Drupal 8/9 version a stable release.
Trainer (03:13):
Now that we’ve downloaded the module, let’s jump back into our Drupal site and then, let me close this tab, let’s go to extend, and search for inline entity, and enable the module. Then go to structure, content types, and then go to manage form display on the product row. Then scroll down all the way to the components field. From the components widget drop down, you should see two new widgets, Inline entity form-Simple and Inline entity form-Complex. The Inline entity form-Simple isn’t that useful. And I prefer to use the Inline entity form-Complex one, because it gives you more options. So let’s go ahead and select Inline entity form-complex, and then click on the cogwheel. And make sure Allow users to add a new custom block entities is checked, as well as Allow users to add existing custom block entities and Allow users to duplicate custom block entities. There are other settings on this widget that I do recommend you play around with. The Form mode dropdown allows you to select a different form mode if one is configured. Form modes allow you to contextually group field widgets. It’s very similar to a view mode.
Trainer (04:41):
If we scroll up and go to manage display, and I’ll open that up in a new tab, you can see the view modes here. And if we click on one of them, you can see that this view mode is contextually grouping these field formatters. Form modes are the same as view modes. However, they are used to group field widgets instead of field formatters. Let’s jump back to the manage form display and then scroll all the way down. Below the form mode dropdown, you can configure if new revisions will be created. You can also override labels by checking Override labels and then entering in a singular or plural version of the label. And then below that, you can make the widget collapsible by checking the Collapsible checkbox. Once everything has been configured, click on Update and then scroll down, and click on Save.
Trainer (05:39):
Now let’s go to content. And let’s edit this product, then scroll down to the components field. And now, the field looks very different. We can see the Refund policy that we added previously. And we can edit the block by clicking the edit button and then making a change and then clicking on Update custom block. But be careful, any change done here will change it across all the other products if they are referencing this block. Then you can duplicate the block by clicking on Duplicate. And if you click on Remove, this will stop the field from referencing the block. It won’t delete the block unless you check Delete this custom block from the system. But if you leave that checkbox unchecked and simply click on Remove, it’ll just stop the field from referencing that custom block. Then if we click on Add existing custom block, we can add the Refund policy block back in.
Trainer (06:45):
And to create a new custom block, all you need to do is click on Add new custom block. And let’s go ahead and create a custom block called Terms and conditions of purchase. So to do that, all I’ll do is enter in a block description, and I’ll paste in some text. And I’ll also make this bit of text bold. And then click on Add custom block. Another thing you can do is reorder the blocks by clicking on the toggle and then moving them up or down. Once everything has been created, scroll down and click on Save. And if we view the product, we should see the new block below the Refund policy. If we go to structure, block layout, and custom block library, we can see the block that we just created. So that is how you use Inline Entity Form. I use it on most of my Drupal sites, and I highly recommend that you use it as well.