1
votes

I'm developing a widget for Magento, in this widget i need to load the magento contact form. I've tried several options, but none of them seem to work.

the widget is located in

app/
   code/
       local/
         CompanyName/
              WidgetName/

and the phtml files are located in

app/
   design/
      frontend/
          default/
             default/
                 template/
                     templatename/
                           templatefile1.pthml
                           templatefile2.pthml
                           templatefile3.phtml

to load the magento contact form i've added this in templatefile3.phtml

<?php echo $this->getChildHtml('contactForm') ?>

but it's not showing up, even after adding xml to 2 files, i've added this line into app/design/frontend/base/default/layout/catalog.xml, right below <reference name="content">

<block type="core/template" name="contactForm" form_action="/contacts/index/post" template="contacts/form.phtml"/>

and also added this code to app/design/frontend/base/default/layout/default.xml (this file didn't even exist)

<default>
<cms_page>
    <reference name="content">
        <block type="core/template" name="contactForm" as="contactForm" template="contacts/form.phtml">
            <action method='setBlockId'><block_id>contactForm</block_id></action>
        </block>
    </reference>
</cms_page>

I have the idea i'm not putting the xml in the right files, but i have no idea witch files to use otherwise, and can't find any other tips than these two on the internet.

Can someone help me out?

3

3 Answers

1
votes

You can add the contact form to any CMS page using CMS tags like this inside the CMS content box:

{{block type="core/template" name="contactForm" template="contacts/form.phtml"}}

You could create a new page, and then inssert that snippet to show the contact form on this new page.

you could also make a copy of form.phtml and modify it for your needs, and update the tag code to use the new template.

0
votes

Please use the following function to display inside templates.

       <?php echo $this->getLayout()->createBlock("Magento\Framework\View\Element\Template")->setTemplate("Magento_Contact::form.phtml")->toHtml(); ?>
-1
votes

You can't just create a default.xml - to let Magento know that that a layout xml file exists you'd have to go through and create a module and specify the layout file's location. that is a very long winded way round of trying to achieve what you need.

Also I believe the handle <cms_page> is not supported. Referencing a block in a CMS page is best done by placing your xml in the layout update section under the 'design' tab for that CMS page.

So it's fairly straight forward to put the contact form into a CMS page - again use the layout update facility. Simply enter the following xml...

<reference name="content">
    <block type="core/template" name="contactForm" template="contacts/form.phtml"/>
</reference>

Note you don't need a layout handle - Magento already knows of course the specific CMS page you are inserting content into because this is a layout update for that page. So in the above you are just referencing the structural block you want to add to (in this case content) and telling Magento to add the contactForm block along with its associated template location.

I've worked on a site using this method before. you will likely find that once you have called the contactform into your CMS page, and despite appearing successfully in a browser, the form doesn't actually post. In fact the form will only send if used from the intended page yoursite.com/contacts

Simple fix. Create the file app/design/frontend/YOUR-PACKAGE/YOUR-THEME/template/contacts/form.phtml and duplicate the required content from the default form.phtml into it.

Change the script at the bottom of the form.phtml file from:

<script type="text/javascript">
    var contactForm = new VarienForm('contactForm', true);
</script>

To:

<script type="text/javascript">
    elem = $("contactForm");
    elem.writeAttribute('action', '/contacts/index/post');
</script>

And your CMS page contact form will now post as required.

Just as an addition (you may or may not find this useful).

I had a need for the contact form on my CMS page to be different from the standard form which appears on contacts. This is really simple too using the method above.

Duplicate the file app/design/frontend/YOUR-PACKAGE/YOUR-THEME/template/contacts/form.phtml and rename as something like app/design/frontend/YOUR-PACKAGE/YOUR-THEME/template/contacts/customForm.phtml - then make your required amends like different fields etc...

now in the layout update for the CMS page you want it to appear in simply add the following xml:

<reference name="content">
    <block type="core/template" name="customContactForm" template="contacts/customForm.phtml"/>
</reference>

And that's it.

Note how the block name has changed - the block name can literally be anything - Magento doesn't care what you call it but I would obviously recommend something logical!