MAGENTO开发:如何在MAGENTO管理界面中创建自定义(菜单)表单

Magento具有出色的功能,允许开发人员在不编码任何HTML的情况下创建管理表单。使用任何数据创建任何表单都很容易。本文将指导您如何创建此类表单。

新的扩展创建

首先让我们创建新的扩展。在我们的例子中,它将是’Turnkeye_Adminform’。

注意:您可以在此处上传本文所述的扩展程序。

所以我们使用扩展声明创建文件:’app / etc / modules / Turnkeye_Adminform.xml’。
我们还需要根目录作为扩展名,它将是:’app / code / community / Turnkeye / Adminform’。

下一步是为我们的扩展创建config.xml文件:’app / code / community / Turnkeye / Adminform / etc / config.xml’。
我们将在此config.xml中声明块,模型,帮助别名,adminhtml路由器和布局文件更新。还有资源和翻译文件。

我们将使用’turnkeye_adminform’作为我们的块/模型/帮助器的前缀。最后,我们将在扩展的根目录中创建“sql”,“Model”,“Block”和“controllers”文件夹。

菜单项创建

准备阶段结束,我们可以继续前进。
我们需要一个管理区域中的菜单项,其中包含指向表单的链接。让我们创建文件’app / code / community / Turnkeye / Adminform / etc / adminhtml.xml’并将菜单声明放在这个文件中:

<?xml version = “1.0” ?>
 < config > 
    < menu > 
        < turnkeye  translate = “title”  module = “turnkeye_adminform” > 
            < title > Turnkeye </ title > 
            < sort_order > 90 </ sort_order > 
            < children > 
                < form  translate = “title”  module = “turnkeye_adminform” > 
                    <</ title > 
                    < sort_order > 10 </ sort_order > 
                    < action > adminhtml / adminform </ action > 
                </ form > 
            </ children > 
        </ turnkeye > 
    </ menu > 
</ config >

由于我们将在扩展中使用转换(module =“turnkeye_adminform”),因此我们需要创建Helper类。
这是一个简单的步骤,只需使用Helper类创建一个文件即可。

class  Turnkeye_Adminform_Helper_Data  扩展 Mage_Core_Helper_Abstract
 {
}

我们使用’adminform’控制器别名。在我们的主配置中,我们需要为它声明管理路由器:

< admin > 
        < routers > 
            < adminhtml > 
                < args > 
                    < modules > 
                        < turnkeye_adminform  after = “Mage_Adminhtml” > Turnkeye_Adminform_Adminhtml </ turnkeye_adminform > 
                    </ modules > 
                </ args > 
            </ adminhtml > 
        </ routers > 
    </ admin >
我们需要使用索引操作创建控制器文件'app / code / community / Turnkeye / Adminform / controllers / Adminhtml / AdminformController.php':

[cc lang ='php'] 
类Turnkeye_Adminform_Adminhtml_AdminformController扩展Mage_Adminhtml_Controller_Action 
{

/ ** 
*查看表单操作
* / 
public function indexAction()
{ 
$ this-> loadLayout(); 
$这- > _ setActiveMenu( 'turnkeye /形式'); 
$ this - > _ addBreadcrumb(Mage :: helper('turnkeye_adminform') - > __('Form'),Mage :: helper('turnkeye_adminform') - > __('Form')); 
$这- > renderLayout(); 
}

}

使用ACL作为控制器和菜单

让我们成为一名优秀的Magento开发人员,并为我们的表单使用ACL。它可以分两步完成。

1.我们需要将以下XML代码添加到’app / code / community / Turnkeye / Adminform / etc / adminhtml.xml’文件中:

<acl>
        <resources>
            <all>
                <title>Allow Everything</title>
            </all>
            <admin>
                <children>
                    <turnkeye>
                        <title>Turnkeye</title>
                        <sort_order>90</sort_order>
                        <children>
                            <form>
                                <title>Form</title>
                                <sort_order>10</sort_order>
                            </form>
                        </children>
                    </turnkeye>
                </children>
            </admin>
        </resources>
    </acl>
MAGENTO DEVELOPMENT: HOW TO CREATE A CUSTOM FORM IN MAGENTO ADMIN INTERFACE
2
26 APR 2012 21:31:37 Author: Vyacheslav Fedorenko


Magento have excellent feature which allow a developer to create the admin forms without coding any HTML. It is easy to create any form with any data. This article will guide you how to create such form.

New extension creation
First of all let's create new extension. In our case it will be 'Turnkeye_Adminform'.

Note: You can upload extension which described in this article here.

So we create file: 'app/etc/modules/Turnkeye_Adminform.xml' with extension declaration.
Also we need root directory for our extension, it will be: 'app/code/community/Turnkeye/Adminform'.

Next step is to create config.xml file for our extension: 'app/code/community/Turnkeye/Adminform/etc/config.xml'.
We will declare block, model, helper aliases, adminhtml router and layout file update in this config.xml. There will be also resources and translate file.

We will use 'turnkeye_adminform' as prefix for our block/model/helper. Finally we will create 'sql', 'Model', 'Block' and 'controllers' folders in root directory of our extension.

Menu item creation
Preparation stage is finished, and we can move forward.
We need a menu item in admin area with a link to our form. Lets create file 'app/code/community/Turnkeye/Adminform/etc/adminhtml.xml' and put the menu declaration in this file:

<?xml version="1.0"?>
<config>
    <menu>
        <turnkeye translate="title" module="turnkeye_adminform">
            <title>Turnkeye</title>
            <sort_order>90</sort_order>
            <children>
                <form translate="title" module="turnkeye_adminform">
                    <title>Form</title>
                    <sort_order>10</sort_order>
                    <action>adminhtml/adminform</action>
                </form>
            </children>
        </turnkeye>
    </menu>
</config>
Since we will use translation in our extension (module="turnkeye_adminform"), we need to create Helper class.
It is an easy step, just create a file with Helper class.

class Turnkeye_Adminform_Helper_Data extends Mage_Core_Helper_Abstract
{
}
We use 'adminform' controller alias. In our main config we need to declare admin router for it:

<admin>
        <routers>
            <adminhtml>
                <args>
                    <modules>
                        <turnkeye_adminform after="Mage_Adminhtml">Turnkeye_Adminform_Adminhtml</turnkeye_adminform>
                    </modules>
                </args>
            </adminhtml>
        </routers>
    </admin>
We need to create controller file 'app/code/community/Turnkeye/Adminform/controllers/Adminhtml/AdminformController.php' with index action:

[cc lang='php' ]
class Turnkeye_Adminform_Adminhtml_AdminformController extends Mage_Adminhtml_Controller_Action
{

/**
* View form action
*/
public function indexAction()
{
$this->loadLayout();
$this->_setActiveMenu('turnkeye/form');
$this->_addBreadcrumb(Mage::helper('turnkeye_adminform')->__('Form'), Mage::helper('turnkeye_adminform')->__('Form'));
$this->renderLayout();
}

}

Use ACL for controller and menu
Let's be a good Magento developers and use ACL for our form. It can be done in 2 steps.

1. We need to add the following XML code to 'app/code/community/Turnkeye/Adminform/etc/adminhtml.xml' file:

    <acl>
        <resources>
            <all>
                <title>Allow Everything</title>
            </all>
            <admin>
                <children>
                    <turnkeye>
                        <title>Turnkeye</title>
                        <sort_order>90</sort_order>
                        <children>
                            <form>
                                <title>Form</title>
                                <sort_order>10</sort_order>
                            </form>
                        </children>
                    </turnkeye>
                </children>
            </admin>
        </resources>
    </acl>
2. Also we need to rewrite _isAllowed() method in our controller class:

[cc lang='php' ]
/**
* Check allow or not access to ths page
*
* @return bool - is allowed to access this menu
*/
protected function _isAllowed()
{
return Mage::getSingleton('admin/session')->isAllowed('turnkeye/form');
}

您应该使用在ACL配置(turnkeye / form)中声明的路径,并且这个路径应该与您在菜单配置中声明的路径相同。

本文参考:
https://astrio.net/blog/magento-admin-form/

Leave a comment

您的电子邮箱地址不会被公开。 必填项已用 * 标注