HomeMagento® 2 Custom Contact FormsTutorialsAdding forms to phtml templates

Adding forms to phtml templates

If you’d like to insert a contact form into a phtml template you can use the following code snippet:

echo \Magento®\Framework\App\ObjectManager::getInstance()
    ->create('Magento®\Cms\Model\Template\FilterProvider')
    ->getBlockFilter()
    ->filter(
            '{{widget type="PluginCompany\ContactForms\Block\Form\Widget\View" form_id="1" show_form_as="form"}}'
    )
;

If you’d like to add a pop-up contact form to a phtml template, the code snippet looks as follows:

echo \Magento®\Framework\App\ObjectManager::getInstance()
    ->create('Magento®\Cms\Model\Template\FilterProvider')
    ->getBlockFilter()
    ->filter(
            '{{widget type="PluginCompany\ContactForms\Block\Form\Widget\View" form_id="1" show_form_as="popup" link_title="Contact Us"}}>'
    )
;

In both examples, the bold {{widget code}} can be generated by adding a Custom Contact Forms widget to a CMS page or block.

Was this article helpful to you? Yes 2 No