2010-09-07 84 views
6

正如在主題中所述,我試圖在自定義模塊的System> Configuration區域中添加一個日期選擇器的日期字段(因此使用etc/system.xml )。添加日期選擇器到自定義模塊上的system.xml

我試圖從下面的線程得到啓示: Magento - Add a button to system.xml with method attached to it

,但沒有成功。

我敢肯定,這是創造合適的塊或方法來創建自定義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; 
      } 
      }  
      ?> 

你有一個技巧,如何做到這一點?

非常感謝。 Hervé

回答

15

編輯2014年2月19日:添加了驗證

我發現了什麼,我認爲是這樣做的更優雅的方式。實際上,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> 

創建一個新的文件:

應用程序/代碼/ [地方,社區] /命名空間/模塊/塊/ Adminhtml /系統/配置/日期

與下面的內容:

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

是否有必要在config.xml中註冊此塊? – Ryre 2012-09-19 20:10:27

+0

是的。只要爲模塊使用自定義塊,Block類必須在其config.xml中聲明 – 2012-09-20 10:15:35

+0

感謝您的答覆。我發現有必要在config.xml(Namespace_Module)中註冊「父」,但沒有必要專門註冊日期。 – Ryre 2012-09-20 19:31:26

2

app/code/local/Varien/Data/Form/Element/中創建類文件。確保文件名前綴標識你的模塊的東西(這僅僅是區分磁核心文件自定義代碼)

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 /中有你的模塊的額外文件。

希望這有助於

+2

創建'應用程序/代碼的文件/本地/瓦瑞恩/數據/表格/元/'相反,它只會工作,以及並且不會去核心文件夾附近的任何地方。 – clockworkgeek 2011-04-06 00:53:19

+0

@clockworkgeek感謝它的工作 – satrun77 2011-04-06 05:58:45

相關問題