我已经使用各种教程为Magento支持的CMS页面创建了一些其他字段。
这些包括text,textarea和image upload字段。我已成功存储文本字段中的数据,但是当我上传图像并保存页面时,我看到以下错误:
注意:第56行的/var/www/html/magento/lib/Varien/Data/Form/Element/Image.php中的数组到字符串转换
我已将错误缩小到我的beforeSave类,该类控制在CMS页面保存上保存任何上传的图像,但我不能在我的生活中找出如何正确检查我的所有图像字段上传图像并成功将其保存到数据库中。
这是我的Observer.php和Config.xml文件。如果有人能够通过我的Observer课程向我指出问题的方向,那将非常感激。
class Ddog_Customcms_Model_Observer
{
public function cmsField($observer)
{
//get CMS model with data
$model = Mage::registry('cms_page');
//get form instance
$form = $observer->getForm();
//create new custom fieldset 'atwix_content_fieldset'
$fieldset = $form->addFieldset('Ddog_content_fieldset_header', array('legend'=>Mage::helper('cms')->__('Page Header'),'class'=>'fieldset-wide'));
//add new field
$fieldset->addField('content_custom_banner', 'image', array(
'name' => 'content_custom_banner',
'label' => Mage::helper('cms')->__('Banner Image'),
'title' => Mage::helper('cms')->__('Banner Image'),
'disabled' => false,
//set field value
'value' => $model->getContentCustomBanner()
));
$fieldset->addField('content_custom_description', 'text', array(
'name' => 'content_custom_description',
'label' => Mage::helper('cms')->__('Description'),
'title' => Mage::helper('cms')->__('Description'),
'disabled' => false,
//set field value
'value' => $model->getContentCustomDescription()
));
$fieldset = $form->addFieldset('Ddog_content_fieldset_items', array('legend'=>Mage::helper('cms')->__('Required Items'),'class'=>'fieldset-half'));
$fieldset->addField('content_custom_ingredients', 'textarea', array(
'name' => 'content_custom_ingredients',
'label' => Mage::helper('cms')->__('Ingredients'),
'title' => Mage::helper('cms')->__('Ingredients'),
'disabled' => false,
//set field value
'value' => $model->getContentCustomIngredients()
));
$fieldset->addField('content_custom_supplies', 'textarea', array(
'name' => 'content_custom_supplies',
'label' => Mage::helper('cms')->__('Supplies'),
'title' => Mage::helper('cms')->__('Supplies'),
'disabled' => false,
//set field value
'value' => $model->getContentCustomSupplies()
));
$fieldset->addField('content_custom_colours_tinted', 'textarea', array(
'name' => 'content_custom_colours_tinted',
'label' => Mage::helper('cms')->__('Colours Tinted'),
'title' => Mage::helper('cms')->__('Colours Tinted'),
'disabled' => false,
//set field value
'value' => $model->getContentCustomColoursTinted()
));
$fieldset = $form->addFieldset('Ddog_content_fieldset_method_step1', array('legend'=>Mage::helper('cms')->__('Method - Step 1'),'class'=>'fieldset-wide'));
$fieldset->addField('content_custom_step1_instructions', 'textarea', array(
'name' => 'content_custom_step1_instructions',
'label' => Mage::helper('cms')->__('Instructions'),
'title' => Mage::helper('cms')->__('Instructions'),
'disabled' => false,
//set field value
'value' => $model->getContentCustomStep1Instructions()
));
$fieldset->addField('content_custom_step1_image1', 'image', array(
'name' => 'content_custom_step1_image1',
'label' => Mage::helper('cms')->__('Image'),
'title' => Mage::helper('cms')->__('Image'),
'disabled' => false,
//set field value
'value' => $model->getContentCustomStep1Image1()
));
$fieldset->addField('content_custom_step1_image2', 'image', array(
'name' => 'content_custom_step1_image2',
'label' => Mage::helper('cms')->__('Additional Image'),
'title' => Mage::helper('cms')->__('Additional Image'),
'disabled' => false,
//set field value
'value' => $model->getContentCustomStep1Image2()
));
$fieldset->addField('content_custom_step1_image3', 'image', array(
'name' => 'content_custom_step1_image3',
'label' => Mage::helper('cms')->__('Additional Image'),
'title' => Mage::helper('cms')->__('Additional Image'),
'disabled' => false,
//set field value
'value' => $model->getContentCustomStep1Image3()
));
$fieldset = $form->addFieldset('Ddog_content_fieldset_method_step2', array('legend'=>Mage::helper('cms')->__('Method - Step 2'),'class'=>'fieldset-wide'));
$fieldset->addField('content_custom_step2_instructions', 'textarea', array(
'name' => 'content_custom_step2_instructions',
'label' => Mage::helper('cms')->__('Instructions'),
'title' => Mage::helper('cms')->__('Instructions'),
'disabled' => false,
//set field value
'value' => $model->getContentCustomStep2Instructions()
));
$fieldset->addField('content_custom_step2_image1', 'image', array(
'name' => 'content_custom_step2_image1',
'label' => Mage::helper('cms')->__('Image'),
'title' => Mage::helper('cms')->__('Image'),
'disabled' => false,
//set field value
'value' => $model->getContentCustomStep2Image1()
));
$fieldset->addField('content_custom_step2_image2', 'image', array(
'name' => 'content_custom_step2_image2',
'label' => Mage::helper('cms')->__('Additional Image'),
'title' => Mage::helper('cms')->__('Additional Image'),
'disabled' => false,
//set field value
'value' => $model->getContentCustomStep2Image2()
));
$fieldset->addField('content_custom_step2_image3', 'image', array(
'name' => 'content_custom_step2_image3',
'label' => Mage::helper('cms')->__('Additional Image'),
'title' => Mage::helper('cms')->__('Additional Image'),
'disabled' => false,
//set field value
'value' => $model->getContentCustomStep2Image3()
));
$fieldset = $form->addFieldset('Ddog_content_fieldset_method_step3', array('legend'=>Mage::helper('cms')->__('Method - Step 3'),'class'=>'fieldset-wide'));
$fieldset->addField('content_custom_step3_instructions', 'textarea', array(
'name' => 'content_custom_step3_instructions',
'label' => Mage::helper('cms')->__('Instructions'),
'title' => Mage::helper('cms')->__('Instructions'),
'disabled' => false,
//set field value
'value' => $model->getContentCustomStep3Instructions()
));
$fieldset->addField('content_custom_step3_image1', 'image', array(
'name' => 'content_custom_step3_image1',
'label' => Mage::helper('cms')->__('Image'),
'title' => Mage::helper('cms')->__('Image'),
'disabled' => false,
//set field value
'value' => $model->getContentCustomstep3Image1()
));
$fieldset->addField('content_custom_step3_image2', 'image', array(
'name' => 'content_custom_step3_image2',
'label' => Mage::helper('cms')->__('Additional Image'),
'title' => Mage::helper('cms')->__('Additional Image'),
'disabled' => false,
//set field value
'value' => $model->getContentCustomstep3Image2()
));
$fieldset->addField('content_custom_step3_image3', 'image', array(
'name' => 'content_custom_step3_image3',
'label' => Mage::helper('cms')->__('Additional Image'),
'title' => Mage::helper('cms')->__('Additional Image'),
'disabled' => false,
//set field value
'value' => $model->getContentCustomstep3Image3()
));
$fieldset = $form->addFieldset('Ddog_content_fieldset_method_step4', array('legend'=>Mage::helper('cms')->__('Method - Step 4'),'class'=>'fieldset-wide'));
$fieldset->addField('content_custom_step4_instructions', 'textarea', array(
'name' => 'content_custom_step4_instructions',
'label' => Mage::helper('cms')->__('Instructions'),
'title' => Mage::helper('cms')->__('Instructions'),
'disabled' => false,
//set field value
'value' => $model->getContentCustomStep4Instructions()
));
$fieldset->addField('content_custom_step4_image1', 'image', array(
'name' => 'content_custom_step4_image1',
'label' => Mage::helper('cms')->__('Image'),
'title' => Mage::helper('cms')->__('Image'),
'disabled' => false,
//set field value
'value' => $model->getContentCustomstep4Image1()
));
$fieldset->addField('content_custom_step4_image2', 'image', array(
'name' => 'content_custom_step4_image2',
'label' => Mage::helper('cms')->__('Additional Image'),
'title' => Mage::helper('cms')->__('Additional Image'),
'disabled' => false,
//set field value
'value' => $model->getContentCustomstep4Image2()
));
$fieldset->addField('content_custom_step4_image3', 'image', array(
'name' => 'content_custom_step4_image3',
'label' => Mage::helper('cms')->__('Additional Image'),
'title' => Mage::helper('cms')->__('Additional Image'),
'disabled' => false,
//set field value
'value' => $model->getContentCustomstep4Image3()
));
}
public function addFormEnctype(Varien_Event_Observer $observer)
{
try {
$block = $observer->getEvent()->getBlock();
if ($block instanceof Mage_Adminhtml_Block_Cms_Page_Edit_Form) {
$form = $block->getForm();
$form->setData('enctype', 'multipart/form-data');
$form->setUseContainer(true);
}
} catch(Exception $e) {
Mage::logException($e);
}
}
public function beforeSave(Varien_Event_Observer $observer)
{
try {
$model = $observer->getEvent()->getPage();
$request = $observer->getEvent()->getRequest();
$data = $request->getPost();
$path = Mage::getBaseDir('media') . '/wysiwyg/'; //. DS . self::MEDIA_DIR;
if ( ! file_exists($path)) {
mkdir($path, 0777, true);
}
$imagesAttributesNames = array('content_custom_banner','content_custom_step1_image1','content_custom_step1_image2','content_custom_step1_image3','content_custom_step2_image1','content_custom_step2_image2','content_custom_step2_image3','content_custom_step3_image1','content_custom_step3_image2','content_custom_step3_image3','content_custom_step4_image1','content_custom_step4_image2','content_custom_step4_image3');
$images = array();
foreach($imagesAttributesNames as $imageAttributeName) {
$uploadedImage = $_FILES[$imageAttributeName];
if ($uploadedImage['name'] > '1') {
Mage::log('uploading');
$uploader = new Varien_File_Uploader($imageAttributeName);
Mage::log('set');
$uploader->setAllowedExtensions(array('jpg', 'jpeg', 'gif', 'png', 'bmp'));
$uploader->setAllowRenameFiles(true);
$uploader->setFilesDispersion(false);
$uploader->save($path, $uploadedImage['name']);
Mage::log($uploader);
$model->setData($imageAttributeName, 'wysiwyg/' . $uploader->getUploadedFileName()); //$path.'/'.$uploader->getUploadedFileName());
} else {
if(isset($data[$imageAttributeName]['delete']) && $data[$imageAttributeName]['delete'] == 1) {
$data[$imageAttributeName] = '';
$model->setData($imageAttributeName, $data[$imageAttributeName]);
} else {
unset($data[$imageAttributeName]);
$model->setData($imageAttributeName, implode($request->getPost($imageAttributeName)));
}
}
}
} catch(Exception $e) {
Mage::logException($e);
}
}
}
<?xml version="1.0"?>
<config>
<modules>
<Ddog_Customcms>
<version>1.0.2</version>
</Ddog_Customcms>
</modules>
<global>
<page>
<layouts>
<Ddog_custom_page>
<label>Recipe Steps</label>
<template>page/recipe-steps.phtml</template>
</Ddog_custom_page>
</layouts>
</page>
<models>
<Ddog_Customcms>
<class>Ddog_Customcms_Model</class>
</Ddog_Customcms>
</models>
<events>
<adminhtml_block_html_before>
<observers>
<Ddog_Customcms_cms_page_edit_form_add_enctype>
<type>singleton</type>
<class>Ddog_Customcms_Model_Observer</class>
<method>addFormEnctype</method>
</Ddog_Customcms_cms_page_edit_form_add_enctype>
</observers>
</adminhtml_block_html_before>
<cms_page_prepare_save>
<observers>
<Ddog_Customcms_cms_page_prepare_save>
<type>singleton</type>
<class>Ddog_Customcms_Model_Observer</class>
<method>beforeSave</method>
</Ddog_Customcms_cms_page_prepare_save>
</observers>
</cms_page_prepare_save>
<adminhtml_cms_page_edit_tab_content_prepare_form>
<observers>
<Ddog_Customcms_page_edit_tab_content>
<type>singleton</type>
<class>Ddog_Customcms_Model_Observer</class>
<method>cmsField</method>
</Ddog_Customcms_page_edit_tab_content>
</observers>
</adminhtml_cms_page_edit_tab_content_prepare_form>
</events>
<resources>
<Ddog_Customcms_setup>
<setup>
<module>Ddog_Customcms</module>
</setup>
</Ddog_Customcms_setup>
</resources>
</global>
</config>
答案 0 :(得分:0)
在保存数据之前,您需要将图像格式从数组更改为字符串。要解决此问题,只需在“beforeSave”函数中的所有“$model->setData($data);
”之前插入以下代码即可。
代码是:
if(isset($data['image_url']['value'])) // Use your image_field_name instead of image_url i.e. content_custom_step1_image1
{
$data['image_url'] = $data['image_url']['value']; // Use image_field_id instead of image_url i.e. content_custom_step1_image1
}