Ala*_*orm 14 php forms magento autoload
有没有办法将自定义表单元素添加到Magento Adminhtml表单而不将自定义元素放在lib/Varian文件夹中?
我已经找到了基本上是Varian_Data_Form_Element_工厂的代码
public function addField($elementId, $type, $config, $after=false)
{
if (isset($this->_types[$type])) {
$className = $this->_types[$type];
}
else {
$className = 'Varien_Data_Form_Element_'.ucfirst(strtolower($type));
}
$element = new $className($config);
$element->setId($elementId);
if ($element->getRequired()) {
$element->addClass('required-entry');
}
$this->addElement($element, $after);
return $element;
}
Run Code Online (Sandbox Code Playgroud)
因此,如果我正确读取此内容,我确保EAV属性的前端返回特定的fieldType(例如supertextfield),系统将Varien_Data_Form_Element_Supertextfield在显示此属性的编辑表单时实例化/呈现 .
这很好,但这意味着我需要在lib/Varian文件夹层次结构中包含我的自定义表单元素.鉴于Magento是面向模块的,看起来这样做是错误的.
我意识到我可以在lib中使用custo自动加载器或符号链接,但我主要是想学习是否有
为属性添加自定义表单元素的规范方法
一种定制Magento自动加载器的规范方法.
liq*_*ity 26
这是一个旧帖子,但它仍然可以对某人有用:
是的你可以.
下面的代码位于:app/code/local/MyCompany/MyModule/Block/MyForm.php
class MyCompany_MyModule_Block_MyForm extends Mage_Adminhtml_Block_Widget_Form
{
protected function _prepareForm()
{
$form = new Varien_Data_Form(array(
'id' => 'edit_form',
'action' => $this->getUrl('*/*/save'),
'method' => 'post'
));
$fieldset = $form->addFieldset('my_fieldset', array('legend' => 'Your fieldset title')));
//Here is what is interesting us
//We add a new type, our type, to the fieldset
//We call it extended_label
$fieldset->addType('extended_label','MyCompany_MyModule_Lib_Varien_Data_Form_Element_ExtendedLabel');
$fieldset->addField('mycustom_element', 'extended_label', array(
'label' => 'My Custom Element Label',
'name' => 'mycustom_element',
'required' => false,
'value' => $this->getLastEventLabel($lastEvent),
'bold' => true,
'label_style' => 'font-weight: bold;color:red;',
));
}
}
Run Code Online (Sandbox Code Playgroud)
以下是自定义元素的代码,该代码位于app/code/local/MyCompany/MyModule/Lib/Varien/Data/Form/Element/ExtendedLabel.php中:
class MyCompany_MyModule_Lib_Varien_Data_Form_Element_ExtendedLabel extends Varien_Data_Form_Element_Abstract
{
public function __construct($attributes=array())
{
parent::__construct($attributes);
$this->setType('label');
}
public function getElementHtml()
{
$html = $this->getBold() ? '<strong>' : '';
$html.= $this->getEscapedValue();
$html.= $this->getBold() ? '</strong>' : '';
$html.= $this->getAfterElementHtml();
return $html;
}
public function getLabelHtml($idSuffix = ''){
if (!is_null($this->getLabel())) {
$html = '<label for="'.$this->getHtmlId() . $idSuffix . '" style="'.$this->getLabelStyle().'">'.$this->getLabel()
. ( $this->getRequired() ? ' <span class="required">*</span>' : '' ).'</label>'."\n";
}
else {
$html = '';
}
return $html;
}
}
Run Code Online (Sandbox Code Playgroud)
自助服务台再次罢工。看起来 Magento 设置包含路径的方式使您可以从本地代码分支中的 lib (不仅仅是 Mage_ 命名空间)中删除类文件
app/code/local/Varien/etc
Run Code Online (Sandbox Code Playgroud)
当自动加载器尝试加载 lib/Varien 类时,它会首先检查您的目录。如果 Varien 创建与您同名的数据元素,您仍然面临风险,但风险相对较低。