Trying to add a child block to adminhtml form - php

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

Related

How to acces one .phtml file in another .phtml file magento?

In magento development how to access one ".phtml" file in one or more other ".phtml" files?
For example:
In my theme template folder there is one folder info. It contains info.phtml.This info.phtml display data from database.I want to use this info.phtml in my other .phtml files by using getChildHtml() how to do this?
Try this Code in your phtml file to call another phtml file
<?php
echo $this->getLayout()->createBlock('core/template')->setTemplate('test/test.phtml')->toHtml();
?>
To do that using getChildHtml() the other phtml file needs to be child block inside of the block you are working on.
That piece of xml came from app/design/frontend/yourtheme/layout/catalog.xml, inside that block declaration you will see more blocks that can be called via getChildHtml().
<block type="catalog/product_view" name="product.info" template="catalog/product/view.phtml">
<block type="catalog/product_list_related" name="product.related.products" as="related_products" template="catalog/product/view/related-products.phtml"/>
</block>
In this case you can call related-products.phtml like that getChildHtml('related_products'), inside of the parent block view.phtml.

Ajax in magento (load product view block)

What I want to achieve:
Clicking on a product link/image (at least in certain areas) to open a pop-up with the full product information (basically all the contents of the product view page).
What I did/tried so far:
created all the stuff outside the ajax php code (the module, links, templates, rewrites)
created the ajax controller (which can be accessed with a link similar to: http://test.com/index.php/ajaxproductview/ajax/index/id/2 ).
to follow various tutorials ( like this or this ) - that helped me get this far. But I don't want to load my custom block, I want the default product view block(s).
tried to add some code in the indexAction(). It gets there, but the code fails. I don't get any errors/notices/reports, just what it seems like an infinite loop that kills my processor.
$body = $this
->getLayout()
->createBlock('product.info') // taken from catalog.xml
->toHtml();
$this->getResponse()->setBody($body);
All the other pages work fine, and it's a fresh magento with only magneto and my module installed and activated.
My AJAX function simply gets this HTML response, puts it into a div, and opens a pop-up.
My question(s) is(are) - how can I set the product id, so the block knows what product to load, and how can I load this block correctly. I also tried something similar to this:
Thank you.
PS: I also tried this:
$layout = $this->getLayout();
$update = $layout->getUpdate();
$update->load('catalog_product_view');
$layout->generateXml();
$layout->generateBlocks();
$output = $layout->getOutput(); // $output is an empty string
The Product controller uses a helper to set the active product. You should be able to do the same in your controller!
Try this before you do your layouting:
$productId = (int) $this->getRequest()->getParam('id');
Mage::helper('catalog/product')->initProduct($productId, $this);
Another thing to be aware of:
If you add a block like the product.info block. It needs additional child blocks if it calls them in its template file.
It would be easiest to use a custom layout xml file. You can then add a specific layout for your action handle (your action handle consists of your routers node in your module's etc/config.xml file under <frontend><routers>, e.g. <Yourmodule> node, make sure to lowercase it! And then with underscores add the controller name and action name, in your case index_index) like this:
<yourmodule_index_index>
<remove name="right"/>
<remove name="left"/>
<block type="catalog/product_view" name="root" output="toHtml" template="catalog/product/view.phtml">
<!-- Add all the child blocks you need -->
</block>
</yourmodule_index_index>
This makes the view.phtml the root block which renders itself using its toHtml method.
Therefore, in your controller action, all you need is my two lines above and then:
$this->loadLayout();
$this->renderLayout();

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.

Magento Contact form in phtml file of custom created widget

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!

Change position of currency selector in Magento

Currently, the currency selector is at the top, here’s my development site:
http://nordschleife.metaforix.net/118/118/index.php/kyocera.html
However, I would like to switch the currency selector to just under “Price” heading of the table.
I tried
echo $this->getCurrency();
but there’s nothing. I guess I need some method like getCurrencyHtml(), but it seems that there’s no such a method.
Or must I edit layout files, and how should I go about doing this?
I can show you a way to do this, but in order to understand what's going on, you'll need to have at least a basic grasp of how Magento's layout files work. For that you should read the designer's guide here and a basic explanation of how it all works here.
Now there are several way of handling this, but I think the easiest way is to simply use the existing currency block. Seeing as you'll be putting it in that tiny cell I assume you wont be needing the "Select your Currency" headline. So we'll need a new template.
A block in Magento consists of two files, a block class that does all the work of generating dynamic content and a template file which uses the block class' methods along with some html to create the final result. The heavy lifting of getting the currency options is already done by the block class so if we can use that paired with a new template file we'll be set.
The existing declaration in the layout files and specifically directory.xml is
<block type="directory/currency" name="currency" before="catalog.leftnav" template="directory/currency.phtml"/>
So the template file is app\design\frontend[interface][theme]\template\directory\currency.phtml
Copy that to currency2.phtml and in there remove the heading.
Now to create a new block named "currency2" consisting of the old block class and the new template file we write
<block type="directory/currency" name="currency2" as="currency2" template="directory/currency2.phtml"/>
We'll be using that in /template/catalog/product/list.phtml so open catalog.xml and put the new block declaration under
<block type="catalog/product_list" name="product_list" template="catalog/product/list.phtml">
in the appropriate section (I assume catalog_category_default).
Finally open /template/catalog/product/list.phtml and add
<?php echo $this->getChildHtml('currency2'); ?>
where you want the block to appear.

Categories