在自定义模块上的system.xml中添加日期选择器

Her*_*tin 6 magento

正如在主题中所述,我试图在系统>配置区域中为自定义模块添加日期字段及其日期选择器(因此使用etc/system.xml).

我试图从下面的主题中获得灵感: Magento - 在system.xml上添加一个按钮,附加方法

但没有成功.

我确定这是一个创建正确的块或方法来创建自定义html字段的问题,但我无法读取通过Magento矩阵:)

我陷入了需要编写类(Datefield.php)的步骤:

    <?php
            class Namespace_Module_Block_Datefield extends Mage_Adminhtml_Block_System_Config_Form_Field {

             protected function _getElementHtml(Varien_Data_Form_Element_Abstract $element) {
    // ----> Am I wrong in calling ..._Abstract?  Should I call Varien_Data_Form_Element_Date? I've tried but no success either...

$this->setElement($element);

              $html = // ------------------> what to put here? Call a block or some other method?
                      ->setFormat('d-m-Y')
                      ->setLabel($this->__('Choose date'))
                      ->toHtml();

              return $html;
             }
            }    
            ?>
Run Code Online (Sandbox Code Playgroud)

你有诀窍怎么做?

非常感谢.埃尔韦

Her*_*tin 15

编辑02/19/2014:添加验证

我发现我认为这是一种更优雅的方式.实际上,satrun77方法还可以,但我们必须在Varien/Data/Form/Element /中放置一个文件,如果其他工作在项目上的人不幸使用相同的文件/类名,则可以覆盖该文件.此外,此方法将文件放在模块目录中,我认为,这比在目录树中分发文件更好.

在system.xml中:

<?xml version="1.0" encoding="UTF-8"?>
   <config>
   ....
       <fields>
       ...
          <run translate="label">
           <label>Date</label>
           <frontend_type>text</frontend_type> <!-- Use text instead of "myDateSelection" -->
           <frontend_model>module/adminhtml_system_config_date</frontend_model> <!-- Call a module specific renderer model -->
           <sort_order>20</sort_order>
           <show_in_default>1</show_in_default>
           <show_in_website>1</show_in_website>
           <validate>required-entry</validate> <!-- Optional -->
           <show_in_store>1</show_in_store>
          </run>
       </fields>
   ...
   </config>
Run Code Online (Sandbox Code Playgroud)

创建一个新文件:

app/code/[local,community]/Namespace/Module/Block/Adminhtml/System/Config/Date

以下内容:

class Namespace_Module_Block_Adminhtml_System_Config_Date extends Mage_Adminhtml_Block_System_Config_Form_Field
{
    protected function _getElementHtml(Varien_Data_Form_Element_Abstract $element)
    {
        $date = new Varien_Data_Form_Element_Date;
        $format = Mage::app()->getLocale()->getDateFormat(Mage_Core_Model_Locale::FORMAT_TYPE_SHORT);

        $data = array(
            'name'      => $element->getName(),
            'html_id'   => $element->getId(),
            'image'     => $this->getSkinUrl('images/grid-cal.gif'),
        );
        $date->setData($data);
        $date->setValue($element->getValue(), $format);
        $date->setFormat(Mage::app()->getLocale()->getDateFormat(Mage_Core_Model_Locale::FORMAT_TYPE_SHORT));
        $date->setClass($element->getFieldConfig()->validate->asArray());
        $date->setForm($element->getForm());

        return $date->getElementHtml();
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 如果我理解你说的话,这就是Magento在你使用Blocks时的工作方式.您不需要声明每个Block.您只声明主Block文件夹.即:如果你让我们说2块:`Namespace_Module_Block_First`和`Namespace_Module_Block_Second`,你只需要在你的config.xml中添加`<blocks> <class> Namespace_Module_Block </ class> </ blocks>`.然后Magento会知道所有Block类都驻留在app/code/[local | 社区] /命名空间/模块/阻止文件夹.如果这不是您之前评论中的意思,欢迎您要求精确! (4认同)