I'm new to magento, and I'm confused how magento handle forms.
I'm using sample data given by magento
In my admin panel, I go to catalog,manage product, and try to figure out how magento save those form data to database. let's say I edit the item which id=881, from the url, when render the page,they use app/design/adminhtml/default/default/template/catalog/product/edit.phtml for the template, and in this file, there is lines of code
<form action="<?php echo $this->getSaveUrl() ?>" method="post" id="product_edit_form" enctype="multipart/form-data">
<?php echo $this->getBlockHtml('formkey')?>
<div style="display:none"></div>
</form>
and i tried to find out which action script specified to handle this post form, I echo it to the page and got something like localhost/magento/index.php/admin/catalog_product/save/id/881/key/d092b22cecaae47664a8c9f9eea63a50/, I think admin is the namesapce, catalog_product is the controllers' name, and save should be the action name, and I look for the file located in Mage_Catalog_controllers_ProductController.php and i can not find the save action.
Admin is your module name, which should give away where you should be looking for the controller,
If you open up app\code\core\Mage\Adminhtml\etc\config.xml,
you will notice,
#FIle :82
<admin>
<routers>
<adminhtml>
<use>admin</use>
<args>
<module>Mage_Adminhtml</module>
<frontName>admin</frontName>
</args>
</adminhtml>
</routers>
</admin>
Notice, <frontName>admin</frontName>
thus for above path of the controller is,
app\code\core\Mage\Adminhtml\controllers\Catalog\ProductController.php
will give you the actions related to product in admin, same way you can track your blocks, helpers and models.
Related
I am building a magento extension that will need a custom url for the frontend that is set via the admin panel config setting for the extension.
Along the lines of the "set admin url" setting in the system settings (which can also be set in the app/etc/local.xml file as well).
basically i have in my config.xml file
<frontend>
<routers>
<extensionname>
<use>standard</use>
<args>
<module>My_Extensionname</module>
<frontName>extensionname</frontName>
</args>
</extensionname>
</routers>
</frontend>
This creates the url site.com/extensionname
But I want to be able to set the url in the system/settings tab in the admin panel
I have looked through the core code and seen glimpses of code that does (a predispatch models controllers etc i think) this for the default admin url key setting
How would i go about this? Would i set up an observer to catch the request to url? or observer settings change and programmatically create a url rewrite?
What about the content & root template as well in the layout/extensionname.xml?
<layout version="0.1.0">
<extensionname_index>
<reference name="root">
<action method="setTemplate"><template>extensionname/page.phtml</template></action>
</reference>
<reference name="content">
<block type="extensionname/extensionname" name="extensionname" template="extensionname/extensionname.phtml" />
</reference>
</extensionname_index>
Would this still be used even though i would be using a custom url from the settings
Ok could not get any of the above info (links) to work from my end (in regards to using custom routers etc) because i think my extension is not using any collections from the database as its just a landing page so wont have index/index/id values etc.
So went with a dirty hack to do the job for now. See below.
etc/config.xml (observer event when admin field setting saved)
<config>
...
<frontend>
<routers>
<myextension>
<use>standard</use>
<args>
<module>Mycompany_Myextension</module>
<frontName>myextension</frontName>
</args>
</myextension>
</routers>
...
</frontend>
<global>
...
<events>
<admin_system_config_changed_section_myextension>
<observers>
<myextension>
<type>singleton</type>
<class>myextension/observer</class>
<method>observersave</method>
</myextension>
</observers>
</admin_system_config_changed_section_myextension>
</events>
...
</global>
</config>
Model/Observer.php (Save a URL Rewrite) (EDITED)
public function observersave(Varien_Event_Observer $observer)
{
#remove the old urlrewrite
$url = Mage::getStoreConfig('myextension/general/url');
$uldURLCollection = Mage::getModel('core/url_rewrite')->getResourceCollection();
$uldURLCollection->getSelect()
->where('id_path=?', 'myextension');//EDIT: so overwrites on each save
$uldURLCollection->setPageSize(1)->load();
if ( $uldURLCollection->count() > 0 ) {
$uldURLCollection->getFirstItem()->delete();
}
#add url rewrite
$modelURLRewrite = Mage::getModel('core/url_rewrite');
$modelURLRewrite->setIdPath('myextension/'.strtolower($url))
->setTargetPath('myextension/index/index/id/'.$url.'')
->setOptions('')
->setDescription('New URL - Created as a new setting was saved')
->setRequestPath('myextension/url/'.$url.'');//EDIT: added extra rewrite url paths so rewrite can never conflict if admin setting field is set to a "key default url" like "admin" or "checkout" or "contacts" etc
$modelURLRewrite->save();
}
controllers/IndexController.php (redirects if no ID...) (EDITED)
public function preDispatch()
{
//$url = Mage::getStoreConfig('myextension/general/url');
if ( !strstr($this->getRequest()->getRequestUri(), 'myextension/index/index/id') ) {
parent::preDispatch();
}
}
public function indexAction()
{
$url = Mage::getStoreConfig('myextension/general/url');
if ( trim($this->getRequest()->getParam('id')) == '' ) {
$this->_redirect('/');//Edit: changed redirect to root
} else {
$id = $this->getRequest()->getParam('id');
if($id == $url) {
$this->loadLayout( array(
'default',
'myextension_index_index'
));//EDIT: added if statement to see if myextension/index/index/id matched admin setting, if not redirect to root
$this->renderLayout();
}else{
$this->_redirect('/');//Edit: changed redirect to root
}
}
}
so at this point i have URLs at: (EDITED)
site.com/myextension/url/myadminfieldvalue
&
site.com/myextension/index/index/id/myadminfieldvalue
(EDITED)
but in template/myextension/myextension.phtml
<?php if($current_url == ''.$base_url.'myextension/index/index'){ ?>
<p>Disabled cause i dont really want info at this url..</p>
<?php }else{ ?>
<p>show data because your accessing myextension/index/index via the rewrite /myextension/url/myadminfieldvalue.</p>
<?php } ?>
A bit more work is needed to achieve what i want but for now it works for me and hope others may find this useful.
I have created three different modules for magento custom column layout, magento custom prices and magento custom file extension(this extension supposed to allow any video file) inside the code/local codepool folder.
I followed the folder structure of each module from the code/core codepool. However these modules are not recognized.
To test if my code is correct, I paste the magento custom column layout config.xml code to app/code/core/Mage/Page/etc to update it and it works.
Question:
How can my code works in code/local codepool?
This is a portion of my working magento custom column layout config.xml file.
<layouts>
<empty module="page" translate="label">
<label>Empty</label>
<template>page/empty.phtml</template>
<layout_handle>page_empty</layout_handle>
</empty>
<one_column module="page" translate="label">
<label>1 column</label>
<template>page/1column.phtml</template>
<layout_handle>page_one_column</layout_handle>
<is_default>1</is_default>
</one_column>
<full_column module="page" translate="label">
<label>Full 1 column</label>
<template>page/full1column.phtml</template>
<layout_handle>page_one_column_full</layout_handle>
<is_default>1</is_default>
</full_column>
<two_columns_left module="page" translate="label">
<label>2 columns with left bar</label>
<template>page/2columns-left.phtml</template>
<layout_handle>page_two_columns_left</layout_handle>
</two_columns_left>
<two_columns_right module="page" translate="label">
<label>2 columns with right bar</label>
<template>page/2columns-right.phtml</template>
<layout_handle>page_two_columns_right</layout_handle>
</two_columns_right>
<three_columns module="page" translate="label">
<label>3 columns</label>
<template>page/3columns.phtml</template>
<layout_handle>page_three_columns</layout_handle>
</three_columns>
</layouts>
Thanks!
Config.xml is just the configuration file of your module and it's not intended for (direct) layout updates. For this purpose you have to create one config.xml file for each module and, in this files, create the XML instructions, through XML node "updates", that say to magento "this is the path for the layout file of this module":
...
<frontend>
...
<layout>
<updates>
<(modulename)>
<file>(name_of_layout_file.xml)</
</(modulename)>
</updates>
</layout>
</frontend>
From: Frontend (Magento - Wiki - config.xml Reference)
Now, you have to create that file within the layout folder of your template, putting inside all the code you need to update the global layout.
More infos:
Adding Additional Layout XML Updates via Modules (Jan 2012; by Alan Storm)
Sorry it's been a while since I post this question. I have been in a vacation for the past weeks so I don't have time to visit this page.
I already figure out the solution for this kind of problem.
Every custom module that a developer will create should register it first to app/etc/modules folder.
Step 1: I followed the folder structure from the app/code/core and put it on the app/code/local. The structure goes like this app/code/local/Mage/Page/etc/config.xml
Step 2: To register my new modules. I Created an .xml file in app/etc/modules and name it any name I want.
Step 3: I open the .xml file I have created and add this piece of code
<?xml version="1.0"?>
<config>
<modules>
<Mage_Page><!-- <Mage_Page> tag came from two folders. Mage is my namespace from app/code/local/Mage and Page is my module name from app/code/local/Mage/Page. -->
<active>true</active>
<codePool>local</codePool>
</Mage_Page>
</modules>
</config>
Step 4: Save all the files you have created/updated.
Step 5: I refresh the admin page and checked if my work is working and yes it is working.
This took me a while due to lack of knowledge and experience in developing magento. But thanks for your help.
Thanks!
I want to know how I can change the URL of my checkout cart. Whenever I continue to checkout or click on the checkout button, I get to the URL: mypage.com/onepagecheckout
I'm using this extension: http://www.magentocommerce.com/magento-connect/one-page-checkout.html
The only way I got it working was making one rewrite and a redirect, one rewrite from "onepagecheckout" to "checkout" and another one permanent redirect from "onepagecheckout" to "checkout". I don't think this method is best practice, so I would like to know if there's a better option.
I tried going to the app/code/community/IWD/OnepaceCheckout/etc/config.xml and changed frontname to "checkout", but even that did not change anything.
Thanks for your time and help would be greatly appreciated!
Please check in config.xml.here you found that onepagecheckout
just change onepagecheckout according
<frontend>
<routers>
.......
<use>standard</use>
<args>
<module>IWD_OnepaceCheckout/</module>
<frontName>onestepcheckout</frontName>
</args>
.......
</routers>
also changed it onestepcheckout.xml accoding to you wish frontName
Solved the problem by changing the in config.xml on both standard and admin.
I thought I had this one sorted but I have run into a snag. I want to add a 'Honey Pot' to the customer registration form, for those unfamiliar this technique involves hiding a text input using CSS and assumes that the average bot will want to fill it in. Humans however, will not see the field so it needs to validate as empty.
In Magento I created a new module, added the following to the config.xml:
<global>
<fieldsets>
<customer_account>
<honeytrap><create>1</create><update>1</update></honeytrap>
</customer_account>
</fieldsets>
<models>
<customer>
<rewrite>
<customer>MyStore_Honeytrap_Model_Customer</customer>
</rewrite>
</customer>
</models>
</global>
I then added a little bit extra to the validate function to check the field is empty. This all correct as far as I can see but at about line 278 in the AccountController.php the extractData() discards the input field from the post data in the request. I'm still very new to Magento so hoping to learn something here too but how do I prevent the field being stripped out of the post by extractData()?
Guess I just want to know what I'm missing, I've read a few posts on the internet regarding adding a custom field so as far as I know this should be working but maybe I've missed something out as I didn't include the Entity setup since I don't need to save this field in the database it's purely to validate the registration is from a human (as much as possible).
Thanks for any help, I'm sure it's probably something ridiculous that I've missed.
EDIT: Thanks to #gordon-knoppe pointer on using the event:
public function check_trap(Varien_Event_Observer $observer)
{
$event = $observer->getEvent();
$post = $event->getControllerAction()->getRequest()->getPost();
// Check Honeytrap is empty
if (Zend_Validate::is( trim($post['fname']) , 'NotEmpty'))
{
$customerHelper = Mage::helper('customer');
$error = $customerHelper->__('A problem has occured with your registration.');
Mage::getModel('customer/session')->addError($error);
Mage::app()->getResponse()
->setRedirect(Mage::getUrl('customer/account', array('_secure' => true)))
->sendResponse();
exit;
}
}
With this in the config.xml:
<events>
<controller_action_predispatch_customer_account_createpost>
<observers>
<mystore_honeytrap_observer>
<type>singleton</type>
<class>Mystore_Honeytrap_Model_Observer</class>
<method>check_trap</method>
</mystore_honeytrap_observer>
</observers>
</controller_action_predispatch_customer_account_createpost>
</events>
A more detached way to handle this could be to register an observer for before the relevant controller action (controller_action_predispatch_*) to detect whether your form field has been populated and, if so, redirect them out to prevent the native action from ever processing the request.
I am building a second add to cart button with a different redirect on a magento platform.
The tutorial I've used is self explainatory. See here
It works okey as the redirect is in place.
The thing is: The cart itself doesnt show its page (cart.phtml) anymore.
Any idea how to fix this? Or where the problem exists?
If I remove the code underneath the cart is back again, as the redirect isn't working anymore.
File : config.xml
<global>
<routers>
<checkout>
<rewrite>
<cart>
<to>mycheckout/cart</to>
<override_actions>true</override_actions>
<actions>
<add>
<to>mycheckout/cart/add</to>
</add>
</actions>
</cart>
</rewrite>
</checkout>
</routers>
</global>
Seems like your extension does not extend the cart controller that you are overwriting making it impossible to display the methods from original controller