Drupal 8 : Inject a contact form inside a content type in 5 steps

Téléphone gros plan

How to insert a contact form inside a content Drupal 8? Or on a specific page in a specific location ? By default, contact forms created have a dedicated page. But if we want to use them elsewhere. After some research, I almost thought I should write a few lines of code to create a specific Plugin.

But contact forms, like almost everything now in Drupal 8, are entities. Finally, place a contact form, or any other form besides, is a simplicity that had eluded me until now.

No need of Panels, Display Suite or some custom preprocess functions.  We will just need the Contact storage module, whose primary purpose is to extend the contact forms to store in the database their different bids, and we will see a little further, which add a little icing on the cake .

Discover how to inject a contact form inside content. This method can as well be used on any fieldable entity, such as blocks, for example.

Step 1: We add an Entity Reference type field, and select the Other option.

Step1 : add reference field other

And we give it a label.

Step2: give a label to the field

 

Step 2: After saving, we can select the entity we want to reference. We select the entity Contact form.

Step3: select the contact form entity to be referenced

 

Step 3: We then get the field configuration page. We can keep the default options. 

Step4 : default configuration field

 

Step 4: After adding the field, we set up the display formatter for our field (in the manage display page configuration of the content type). We select the formatter Rendered entity for our contact field. This is where the Contact storage module step in by adding into the field formatter the Rendered entity option for displaying directly the contact form. Option which by default is not available.

Step5: configure field formatter

 

Step 5: And that's it!

We just need now to create our content, and select the form, we want to display, in the autocomplete field.

Step6: create the content and select the contact form

And we have our content with the form available for your visitors. We can of course, for each created content, select a different form.

Step7: the content with the form injected

 

If you want to place your form on some others pages, simply do the same with a block, which you can then place where you want. And all this without any line of code. Effective not ?

 

Ajouter un commentaire