Esempio n. 1
0
 /**
  * Create Field and Return it
  *
  * @param Varien_Data_Form_Element_Abstract $fieldset
  * @param Varien_Object                     $e
  * @param Mage_Eav_Model_Entity_Attribute   $attribute
  * @param string                            $fieldStep
  *
  * @return Varien_Data_Form_Element_Abstract
  */
 protected function _createField(Varien_Data_Form_Element_Abstract $fieldset, Varien_Object $e, Mage_Eav_Model_Entity_Attribute $attribute, $fieldStep = 'standard')
 {
     // Get Config Data
     $configData = $this->getConfigData();
     if ('' == $attribute->getFrontendLabel()) {
         return false;
     }
     // Define Attribute Code
     $attributeCode = $attribute->getAttributeCode();
     $attributeCode = $fieldStep == 'newfield' ? "{$attributeCode}_dynamic_new" : $attributeCode;
     // Get Attribute Data and Inheritance
     $path = $this->_configPath . $attributeCode;
     if (isset($configData[$path])) {
         $data = $configData[$path];
         $inherit = false;
     } else {
         $data = (string) Mage::getConfig()->getNode(null, $this->getForm()->getScope(), $this->getForm()->getScopeCode())->descend($path);
         $inherit = true;
     }
     // Get field Renderer
     if ($e->frontend_model) {
         $fieldRenderer = Mage::getBlockSingleton((string) $e->frontend_model);
     } else {
         $fieldRenderer = $this->_getFieldRenderer();
     }
     // Define Type, Name, and Label
     $fieldType = (string) $e->frontend_type ? (string) $e->frontend_type : 'text';
     $name = str_replace('_attrCode_', $attributeCode, $this->_fieldNameTemplate);
     $label = $fieldStep == 'newfield' ? "" : $attribute->getFrontendLabel();
     // Pass through backend model in case it needs to modify value
     if ($e->backend_model) {
         $model = Mage::getModel((string) $e->backend_model);
         if (!$model instanceof Mage_Core_Model_Config_Data) {
             Mage::throwException('Invalid config field backend model: ' . (string) $e->backend_model);
         }
         $model->setPath($path)->setValue($data)->afterLoad();
         $data = $model->getValue();
     }
     // Pre-populate New field with label text
     if ('newfield' == $fieldStep && '' == $data) {
         $data = $attribute->getFrontendLabel();
     }
     // Select Field for Existing attributes.
     $field = $fieldset->addField($attributeCode, $fieldType, array('name' => $name, 'label' => $label, 'value' => $data === 0 ? '' : $data, 'inherit' => $fieldStep == 'newfield' ? false : $inherit, 'field_config' => $e, 'scope' => $this->getForm()->getScope(), 'scopeId' => $this->getForm()->getScopeId(), 'scope_label' => '[STORE VIEW]', 'can_use_default_value' => $this->getForm()->canUseDefaultValue((int) $e->show_in_default), 'can_use_website_value' => $this->getForm()->canUseWebsiteValue((int) $e->show_in_website), 'can_use_store_value' => $this->getForm()->canUseWebsiteValue((int) $e->show_in_store)));
     // Add Validation
     if ($e->validate) {
         $field->addClass($e->validate);
     }
     // Determine if value can be empty
     if (isset($e->frontend_type) && 'multiselect' === (string) $e->frontend_type && isset($e->can_be_empty)) {
         $field->setCanBeEmpty(true);
     }
     // Set Field Renderer
     $field->setRenderer($fieldRenderer);
     // Use Source Model to define available options
     if ($e->source_model) {
         $sourceModel = Mage::getSingleton((string) $e->source_model);
         if ($sourceModel instanceof Varien_Object) {
             $sourceModel->setPath($path);
         }
         $field->setValues($sourceModel->toOptionArray());
     }
     return $field;
 }