-
16-10-2019 - |
题
我想创建一个属性读取,但是看起来不可能。
我试图通过addattribute() 'disabled' =>true
或者 'readonly' => true
取得任何成功。我发现了一些有关使用的建议 setLockedAttributes()
但是由于某种原因它不起作用
参考:
Varien_Data_Form_Element_Abstract::serialize($attributes = array(), $valueSeparator='=', $fieldSeparator=' ', $quote='"')
解决方案
从引用的文章中 - 我尝试过,这确实在1.6ce和1.7ce / 1.12ee上起作用。到目前为止,我还没有尝试过1.8/1.13。
https://stackoverflow.com/questions/6384120/magento-read-only-and-and-hidden-product-attributes
好的,看来可以做到这一点。在添加了一个观察者之后
catalog_product_load_after
事件,lockAttribute
方法的方法Mage_Catalog_Model_Abstract
类可用于使产品属性仅阅读。这是观察者方法的代码:
public function lockAttributes($observer) {
$event = $observer->getEvent();
$product = $event->getProduct();
$product->lockAttribute('attribute_code');
}
其他提示
如果您足以使其在产品管理中不可编辑,请使用前端输入类型 label
, ,将表单输入替换为纯文本:
addAttribute($entity, $code, array(
...
'input' => 'label',
...
));
请注意,这不会阻止通过API或操纵后请求保存属性。为了使其安全,请另外使用 lockAttribute()
如上所述.
另外,它仅适用于文本类型属性,对于其他类型,再次落到 lockAttributes
或扩展“标签”类型。
为了使此修复,您可以为此使用输入渲染器。不利的一面是,您必须为每个输入类型执行此操作,并通过设置每个属性进行设置。
为此,请使用 input_renderer 使用时的键 addattribute 属性或 frontend_input_renderer 使用时 UpdateAttribute。例子:
$installer->addAttribute(Mage_Catalog_Model_Product::ENTITY, 'yourattribute', array(
// ...
// won't be used actually as you use a custom renderer (constant equals to text), but I'm not sure what omitting this will have as effect..
'input' => Mage_Catalog_Model_Product_Option::OPTION_GROUP_TEXT,
'input_renderer' => 'yourns_yourmodule/adminhtml_product_helper_form_disabledText',
'frontend_class' => 'disabled',
'note' => 'This field is disabled',
// ...
));
然后和你的班 Yourns_Yourmodule_Block_Adminhtml_Product_Helper_Form_DisabledText
扩展您实际使用的输入类。对于文本字段将是 Varien_Data_Form_Element_Text
。对于选择将是 Varien_Data_Form_Element_Select
等等。
现在添加这样的代码以禁用属性,覆盖 getHtml
方法,设置属性并返回输入字段的实际HTML代码:
public function getHtml()
{
// Set disabled
$this->setReadonly(true, true);
return parent::getHtml();
}
该方法可以在 lib/varien/data/form/摘要.php 所有表单输入元素字段都将继承,因此应该可用。
/**
* Disable elements
*
* @param boolean $readonly
* @param boolean $useDisabled
* @return Varien_Data_Form_Abstract
*/
public function setReadonly($readonly, $useDisabled = false)
{
if ($useDisabled) {
$this->setDisabled($readonly);
$this->setData('readonly_disabled', $readonly);
} else {
$this->setData('readonly', $readonly);
}
foreach ($this->getElements() as $element) {
$element->setReadonly($readonly, $useDisabled);
}
return $this;
}
包括 禁用 属性设置中的类如上面所示,以可视化被拒绝的输入可能性。您也可以使用 $this->addClass('disabled')
在方法中,我还没有尝试过。