Adding the Default Magento Contact Form to a Static Block
As an experienced Magento developer, I often encounter situations where clients need to add the default contact form to static blocks for better visibility or integration purposes. The built-in contact form is a powerful feature that allows customers and potential clients to reach out directly from your website.
To achieve this, you’ll need to follow these steps:
1. Locate the Contact Form Template
The contact form in Magento 2 is located at vendor/magento/module-contacts/view/frontend/templates/form.phtml. This template contains all the necessary HTML and JavaScript for rendering the default contact form.
2. Create a New Static Block
Head over to your Magento admin interface (Backend) and navigate to “Content” -> “Elements” -> “Blocks”. Click on “Add new block”.
Name your block something relevant, like “Contact Form Block”, and make sure to save it once you’ve entered the necessary details.
3. Use the Contact Form Template in Your Static Block
Now comes the crucial partâintegrating the contact form template into your static block. Most developers initially try using code like this:
{{block type="core/template" name="contactForm" template="contacts/form.phtml"}}
However, this approach doesn’t work because Magento 2 uses a different syntax for loading templates in blocks.
The Correct Approach
To correctly integrate the contact form into your static block, you need to use the Magento\Cms\Block class and specify the template directly. Here’s an example of how to do it:
{{block class="Magento\Cms\Block" name="contact_form" template="Magento_Contacts::form.phtml"}}
In this code:
- The
classattribute specifies the block class (Magento\Cms\Block in this case). - The
nameattribute gives your block a unique identifier. - The
templateattribute points to the form.phtml file located under the “contacts” directory of the “Magento_Contacts” module.
4. Test Your Static Block
Once you’ve saved your static block with the correct code, you can place it on any page or layout where you want the contact form to appear. To do this, go to your CMS page, click “Edit”, and add a new WYSIWYG editor field.
Inside that field, paste the following code:
{{block class="Magento\Cms\Block" name="contact_form" template="Magento_Contacts::form.phtml"}}
Save your changes, preview the page, and you should now see the default Magento contact form rendered perfectly within your static block.
Conclusion
Adding the default Magento contact form to a static block is an essential step for improving customer engagement and facilitating communication. By following the correct approach outlined above, you can seamlessly integrate this powerful feature into your website without any hassle or additional development work.
Remember, the key lies in using the Magento\Cms\Block class and specifying the template correctly. Always ensure that your templates are located under the appropriate module directory to avoid any conflicts or errors.
For more advanced Magento development tips and best practices, feel free to explore our blog at Laravel Company. We regularly publish articles on Magento 2 customization, performance optimization, and extension development.