将日期选择器添加到自定义模块上的 system.xml
正如主题中所述,我正在尝试在“系统”>“系统”中添加一个带有日期选择器的日期字段。自定义模块的配置区域(因此使用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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
编辑 02/19/2014:添加验证
我发现了我认为更优雅的方法。实际上,satrun77 方法是可以的,但我们必须在 Varien/Data/Form/Element/ 中放置一个文件,如果从事该项目的其他人不幸使用相同的文件/类名,该文件可能会被覆盖。此外,我认为这种方法将文件放置在模块目录中,这比将文件分布在整个目录树中要好。
在 system.xml 中:
创建一个新文件:
app/code/[local, Community]/Namespace/Module/Block/Adminhtml/System/Config/Date,
内容如下:
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:
Create a new file :
app/code/[local, community]/Namespace/Module/Block/Adminhtml/System/Config/Date
with the content below:
在
app/code/local/Varien/Data/Form/Element/
中创建类文件。确保文件名带有标识您模块的前缀(这只是为了区分您的自定义代码和 Magneto 核心文件)在模块 system.xml 内
将自定义代码放在 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)Inside your module system.xml
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