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

发布于 2024-09-18 06:30:10 字数 1150 浏览 4 评论 0原文

正如主题中所述,我正在尝试在“系统”>“系统”中添加一个带有日期选择器的日期字段。自定义模块的配置区域(因此使用etc/system.xml)。

我试图从下面的线程中获得灵感: Magento - 向系统添加按钮。 xml 附加了方法

但没有成功。

我确信这是创建正确的块或方法来创建自定义 html 字段的问题,但我无法阅读 Magento Matrix :)

我陷入了需要对类进行编码的步骤(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;
             }
            }    
            ?>

你有办法做到这一点吗?

多谢。 埃尔韦

As stated in the subject, I am trying to add a date field with its date picker in the System > Configuration area for a custom module (thus using etc/system.xml).

I tried to get inspiration from the thread below :
Magento - Add a button to system.xml with method attached to it

but no success.

I'm sure this is a question of creating the right block or method to create a custom html field but I cannot read thru the Magento Matrix :)

I am stuck at the step where I need to code the class (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;
             }
            }    
            ?>

Do you have a trick on how to do that ?

Thanks a lot.
Hervé

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

吃兔兔 2024-09-25 06:30:11

编辑 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>

创建一个新文件:

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

EDIT 02/19/2014: added validation

I found what I think is a more elegant way of doing this. Actually, satrun77 methods is ok but we must place a file in Varien/Data/Form/Element/ which can be overwritten if someone else working on the project unluckily uses the same file/class name. Moreover, this method places the file in the module directories which is, I think, better than distributing files all over the directory tree.

In 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>

Create a new file :

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

with the content below:

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();
    }
}
謸气贵蔟 2024-09-25 06:30:11

app/code/local/Varien/Data/Form/Element/ 中创建类文件。确保文件名带有标识您模块的前缀(这只是为了区分您的自定义代码和 Magneto 核心文件)

class Varien_Data_Form_Element_MyDateSelection extends Varien_Data_Form_Element_Date
{
    public function getElementHtml()
    {
        // define image url
        $this->setImage(Mage::getDesign()->getSkinUrl('images/grid-cal.gif'));
        // define date format
        $this->setFormat('yyyy-MM-dd');

        return parent::getElementHtml();
    }
}

在模块 system.xml 内

<?xml version="1.0" encoding="UTF-8"?>
   <config>
   ....
       <fields>
       ...
          <run translate="label">
           <label>Run now</label>
           <frontend_type>myDateSelection</frontend_type>
           <sort_order>20</sort_order>
           <show_in_default>1</show_in_default>
           <show_in_website>1</show_in_website>
           <show_in_store>1</show_in_store>
          </run>
       </fields>
   ...
   </config>

将自定义代码放在 lib/ 文件夹或 app/Mage/Core/ 文件夹中不是为 Magento 创建自定义代码的最佳方法。这些文件夹用于核心代码,而不是自定义代码。

这种方法创建的代码量最少,并且不会更改任何核心文件。因此,在 lib/ 文件夹中包含额外的文件没有任何害处。但您需要记住,您的模块在 lib/ 中有额外的文件。

希望这有帮助

Create class file in app/code/local/Varien/Data/Form/Element/. Make sure the file name is prefixed with something that identify your module (this is just to differentiate your custom code from Magneto core files)

class Varien_Data_Form_Element_MyDateSelection extends Varien_Data_Form_Element_Date
{
    public function getElementHtml()
    {
        // define image url
        $this->setImage(Mage::getDesign()->getSkinUrl('images/grid-cal.gif'));
        // define date format
        $this->setFormat('yyyy-MM-dd');

        return parent::getElementHtml();
    }
}

Inside your module system.xml

<?xml version="1.0" encoding="UTF-8"?>
   <config>
   ....
       <fields>
       ...
          <run translate="label">
           <label>Run now</label>
           <frontend_type>myDateSelection</frontend_type>
           <sort_order>20</sort_order>
           <show_in_default>1</show_in_default>
           <show_in_website>1</show_in_website>
           <show_in_store>1</show_in_store>
          </run>
       </fields>
   ...
   </config>

Placing custom code inside lib/ folder or app/Mage/Core/ folder is not the best way to create custom code for Magento. These folders are for core code and not custom code.

This approach creates the least amount of code and does not change any of the core files. So, there isn't any harm from having extra file inside the lib/ folder. But you need to remember that you have extra file for your module in the lib/.

Hope this helps

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文