Magento Contact form in phtml file of custom created widget - php

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?

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.

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(); ?>

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!

Related

Magento - Add Some HTML/php/JS on All Pages of my Theme After_Body_Start

I've built a custom shopping cart for my Magento site that sends and API call to Shopify to handle payment.
The problem is I need the template file to load on every page in magento. I thought after_body_start would be a good place to put the template, but I cannot get it to load!
My template lives in \app\design\frontend\rwd\crystal\template\mgw\mwCart.phtml
and contains php, html and javascript. It was running just fine when I placed the code directly into the header.phtml
Here's the xml in \app\design\frontend\rwd\crystal\layout\local.xml which should load the template
<?xml version="1.0"?>
<layout version="0.1.0">
<default>
<reference name="after_body_start">
<block type="core/template" name="mwCart" template="mgw/mwCart.phtml" output="toHtml" before="-" />
</reference>
</default>
</layout>
I'm new to Magento and have read through the tutorials. I also checked out and tried any suggestions I could find.
I've seen this Magento - Add Some HTML on All Pages of my Theme After_Body_Start but did not solve my issue.
How can I get my template to load on every page?
Thank you.
You're close here... It the location of your local.xml file that is the problem. Place the existing file in the following location:
app/design/frontend/rwd/default/layout/local.xml
..and it should solve the problem. If you have an existing subtheme, e.g. rwd/mytheme, the file would go here:
app/design/frontend/rwd/mytheme/layout/local.xml
If you still aren't getting the desired results, check the configuration in Magento Admin->Configuration->Design->Themes and make sure that you are indeed using the rwd theme. If all else fails, the base/default location would be the last thing to try:
app/design/frontend/base/default/layout/local.xml

Trying to add a child block to adminhtml form

I am stuck a little with following problem:
I created a Fom in the adminhtml area using the pure php from Magento and standard template.
This all works as expected, but I cannot dynamically add and remove fields, I need Javascript and my own phtml file for that.
My Idea now was to simply include a childblock which is using a custom template at the end of the form.
So far I have added
$cblock =$this->getLayout()
->createBlock('netcon_konmod/adminhtml_caps_edit_mat')
->setTemplate('netcon_konmod_mat.phtml');
$this->_addContent($this->getLayout()
->createBlock('netcon_konmod/adminhtml_caps_edit')->setChild($cblock));
to my controller, created an empty Netcon_Konmod_Block_Adminhtml_Caps_Edit_Mat class which extends Mage_Adminhtml_Block_Widget
and created template file mat.phtml in .../template/netcon/konmod/
I also have my konmod.xml which updates my layout and which includes
<adminhtml_caps_edit>
<reference name="content">
<block type="netcon_konmod/adminhtml_caps_edit" name="netcon_konmod_caps_edit">
<block type="netcon_konmod/adminhtml_caps_edit_mat" name="netcon_konmod_caps_mat" template="netcon_konmod_mat.phtml" />
</block>
</reference>
</adminhtml_caps_edit>
I am rather new to Magento, and until now I only found ways to include a child block by calling getChildHtml from an already existing phtml file of the parent block.
However, since I create my form completely with the form and fieldset methods, I don't have my own phtml file in which I could include that call.
The way I have it set up right now, it is displaying the form normally, but does not include the childblock.
Any help would be appreciated, if it is at all possible to do this, I would like to avoid recoding my whole form as template.
Try
$childBlock = $this->getLayout()
->createBlock('konmod/adminhtml_caps_edit_mat')
->setTemplate('netcon_konmod_mat.phtml');
$this->_addContent($this->getLayout()
->createBlock('konmod/adminhtml_caps_edit')
//->setTemplate('netcon_konmod_mat.phtml')
->append($childBlock));

Understanding getChildHtml in Magento

From the following line in 2columns-right.phtml
<div class="col-main">
<?php echo $this->getChildHtml('global_messages') ?>
<?php echo $this->getChildHtml('content') ?>
</div>
I am not able to understand where the content in <?php echo $this->getChildHtml('content') ?> is coming from.
Which .phtml file is called to display the data by <?php echo $this->getChildHtml('content') ?>?
If we're discussing the frontend of the website, the particular line you've asked about....
<?php echo $this->getChildHtml('content') ?>
is added to the Magento layout XML in app/design/frontend/base/default/layout/page.xml. In Magento version 1.8, you'll find it defined in lines 92-94.
<block type="core/text_list" name="content" as="content" translate="label">
<label>Main Content Area</label>
</block>
By looking at the "type" attribute of this block tag, we can know what object class this section of the layout is. It comes from the "Core" module, and is of the block type Text List. The class name for this Mage_Core_Block_Text_List. (app/code/core/Mage/Core/Block/Text/List.php). Text Lists are simply block containers which purpose is to store additional child blocks inside them. You can add any number of child blocks to the text list and they will be rendered out either in the order they were added or the order they've been assigned.
So, to answer your question, there is no view script (.phtml file) that renders the contents of $this->getChildHtml('content'). The blocks which have been added to this block, may themselves have view scripts associated with them. To find out what view scripts those are, you'd have to find the layout XML which has added the block.
For example, if I had the following layout file added to the frontend of my website's theme:
<?xml version="1.0"?>
<layout>
<default>
<reference name="content">
<block type="core/template" name="my_view_script" template="hello/world.phtml" />
</reference>
</default>
</layout>
The code above, would add the block with an object class of Mage_Core_Block_Template to the block with the name 'content' (which happens to be the one you asked about). Magento will then look for the view script in the following locations, in this order:
app/design/frontend/PACKAGE_NAME/THEME_NAME/template/hello/world.phtml
app/design/frontend/PACKAGE_NAME/default/template/hello/world.phtml
app/design/frontend/base/default/template/hello/world.phtml
First one that is found, is the one it will use. If no view script is found Magento will log an error in var/logs/system.log (default log file setting) stating that the view script was not found. No output from the block will occur.
Note that depending on your settings in System -> Configuration -> (General) Design, there may be additional package/theme locations Magento will look in. There are also other scenarios such as if the "Custom Theme" is field is changed for individual CMS Pages, Catalog Categories, or Catalog Products, these individual model's view page may have an additional view script location (that will match the selected theme) that takes precedence over your site's default settings.
Magento will follow this same fallback logic when looking for translation files as well as layout XML files.
Please note, that it is perfectly acceptable to copy individual view scripts (avoid copying entire directories, copy over only view scripts you actually intend to modify) from app/design/frontend/base/default/template/ to your local theme, and customize them for the purposes of your website's theme. However, in order to have an upgrade compatible site, layout files should not be copied from base to your local theme. Doing so, does not follow upgrade compatible practices. Instead, XML Layout updates for your theme should be contained in app/design/frontend/PACKAGE_NAME/THEME_NAME/layout/local.xml. There is no layout instructions from app/design/frontend/base/default/layout/*, that cannot be removed/added-to/changed, what-have-you, with the proper XML instructions in local.xml.

call a phtml file in Layout Update XML to set title in magento [duplicate]

In Magento how to call a phtml file in cms page to set page title which title I set in my phtml file? I am using
$this->getLayout()->getBlock('head')->setTitle('your title');
to set page title.
To call a phtml file in a cms page or cms static block:
{{block type="core/template" template="templateFolder/your_template.phtml"}}
If you know, where the block file(php file) for your phtml file resides, then you can use it as type.
Example: Suppose you want to call new.phtml file that resides in catalog/product folder, and you know that its corresponding Block file(php file) resides in Catalog/Product folder, then you can use:
{{block type="catalog/product" template="catalog/product/new.phtml"}}
More reading: here
Hope this helps!
You cannot change the title of the page from a template file when using it in a cms block or cms page because the head block is already rendered when the page (or block) content is parsed.
It's not possible to change page title from phtml file of cms pages as already told by #Marius
you need to add to it's design in cms page as given below :
<reference name="head">
<action method="setCustomTitle" translate="title"> <title> Custom Title </title> </action>
</reference>
Add the below XML piece under CMS > Pages > Manage Content > Select a specific CMS Page
Navigate to "Design" tab > Layout Update XML >
<reference name="head">
<action method="setCustomTitle" translate="title"> <title> Custom Title </title> </action>
</reference>
Make sure the CACHE folders are DELETED under below:
{Root Magento Folder}/var/cache
{Root Magento Folder}/var/full_page_cache
Hope this helps!
Happy Coding...

How to unset action in Magento

I am trying to change Top links structure. I don't want to mess with xml files in default theme. I have only one xml file with everything inside. I don't want to create multiple xml files in my theme layout folder. I am trying to unset action inside top.links block.
This is the way I am trying to do:
<reference name="top.links">
<action method="unsetChild"><child>addLink</child></action>
<!-- <remove name="checkout_cart_link" /> -->
</reference>
When I put remove tag it works perfectly. But when I try to do unsetChild for addLink action it doesn't work. Do you know maybe how I should figure this out. I don't want separate xml files with the same names as in default theme.
The unsetChild function is from the Abstract block model and is used for removing a block that is a child of the current block. The block with the name top.links doesn't have a child block that is called addLink, hence the fact your configuration doesn't do anything. The action nodes with the method addLink call the addLink function on the Mage_Page_Block_Template_Links block. If the objective is to remove one of the links, you will need to use the action removeLinkByUrl.
<reference name="top.links">
<action method="removeLinkByUrl"><url>link/here</url></action>
</reference>
The exact arguments will obviously depend on what link you are attempting to remove.

Categories