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 ?

 

Commentaires

Soumis par Anne-Pierre (non vérifié) le 07/09/2016 à 11:20 - Permalien

Hello Fabrice,
Thank you for the easy to follow tutorial. However, the form is not showing up on the node of content-type. link: http://be-in-charge.fr/node/3 . I tried pulling it up using views... nothing. Am I missing something?
The configuration is : Drupal core 8.1.8 and Contact Storage 8.x-1.0-beta7
Thanks for any pointers.

Hello, I will check if there is still available on these version. Contact storage is a module which changes quickly. But, first, have you checked that the permissions are correctly set for the contact forms ?

Soumis par Richard Morgan (non vérifié) le 28/10/2016 à 14:06 - Permalien

This is perfect - gives me webform back!

I can see how entity_embed would be an option, and perhaps a good solution - but it's not what I wanted... This gives me exactly the replacement of webforms in D7.

Thank You!!

Soumis par kikti.com (non vérifié) le 19/09/2017 à 00:51 - Permalien

Is it possible to hide the contact while creating the content ?

Soumis par remaye (non vérifié) le 18/03/2018 à 15:29 - Permalien

Hi.
I'm trying to add the user personal contact form to the user page so that anyone can dircetly contact a user through the personnal contact form.
Everything seems ok using the way you explained above : in each user form I can choose "Personal contact form (personal)" and save.
But when returning to the user page form the Contact field is marked : "- Restricted access - (personal)"
And when displaying the user page, the form doesn't show up (even for user admin).

The "Use users' personal contact forms " permission is checked.
If choosing any other contact form (like "Website Feedback (feedback)") it is working fine.

Do you have any idea on what is going wrong ?
Thanks.

Soumis par Shailesh Bhosale (non vérifié) le 12/04/2019 à 10:12 - Permalien

Hi, Thanks for the tutorial. I have followed the instructions. Works perfect on a single node.

But I need to create a view of my content type and include title (plus one field) value in message body.

Any help is appreciated to fix this issue.

Thanks

Soumis par Seb59 (non vérifié) le 18/04/2019 à 16:02 - Permalien

Hi, thanks for the tutorial. I'm experiencing some issue with this.
I would like to create a new content type that include a personal contact form so visitor can send a message to the node author.
I installed and enabled contact_storage module and followed the steps. No error message. When I create the node, I can select which contact form I would like to use. I did chose "personal contact form" (I'm using French version, so maybe it's not the proper English translation, but anyway that's one of the default contact form provided by drupal instal). This indicates to me that the field seems to be correctly configured.
I checked permissions and everything looks fine.

But when I display the node, nothing is shown. I do not know what to check... Please help

Soumis par julbis (non vérifié) le 08/02/2020 à 13:46 - Permalien

Hi, as Rosato:
I don't know why, but Rendered entity does not appear in manage display.
Though, I have contact storage enabled (enable it in extensions admin after extracting manually the archive in the modules folder, since "Install module from URL fails" for me locally.

Soumis par Lincoln (non vérifié) le 28/11/2021 à 15:55 - Permalien

Hey, first of all, thanks for the great tutorial!

I'm having an issue that I really have no idea why it's happening. I followed the tutorial, selected the form on my page, when I placed the field using Layout Builder, it renders on Layout Builder page with no problems, however when I access the actual node page, it just won't render the form. Any ideas on what might cause this?

Ajouter un commentaire