Add an image field to plugins like AW Blog
Edit in /app/code/community/AW/Blog/Block/Manage/Blog/Edit/Tab/Form.php
$fieldset->addField('filename', 'file', array(
'label' => Mage::helper('blog')->__('Image'),
'required' => false,
'name' => 'filename',
'after_element_html' => 'Formaat 125 x 118 pixels',
));
Edit in /app/code/community/AW/Blog/controllers/Manage/BlogController.php after:
public function saveAction() {
if ($data = $this->getRequest()->getPost()) {
Add:
if(isset($_FILES['filename']['name']) && $_FILES['filename']['name'] != '') {
try {
/* Starting upload */
$uploader = new Varien_File_Uploader('filename');
// Any extention would work
$uploader->setAllowedExtensions(array('jpg','jpeg','gif','png'));
$uploader->setAllowRenameFiles(false);
// Set the file upload mode
// false -> get the file directly in the specified folder
// true -> get the file in the product like folders
// (file.jpg will go in something like /media/f/i/file.jpg)
$uploader->setFilesDispersion(false);
// We set media as the upload dir
$path = Mage::getBaseDir('media') . DS ;
$uploader->save($path, $_FILES['filename']['name'] );
} catch (Exception $e) {
}
//this way the name is saved in DB
$data['filename'] = $_FILES['filename']['name'];
}
Further add an field ‘filename’ in your db (table ‘aw_blog’).
And finally in /app/code/community/AW/Blog/Block/Blog/Edit/Form.php make sure that $form variable is defined as follows, so it works with multipart/form-data:
$form = new Varien_Data_Form(array(
'id' => 'edit_form',
'action' => $this->getUrl('*/*/save', array('id' => $this->getRequest()->getParam('id'))),
'method' => 'post',
'enctype' => 'multipart/form-data',
)
);
hi!
just found your post explaining just what i need! The only thing i dont seem to get working is the part in:
/app/code/community/AW/Blog/Block/Manage/Blog/Edit/Form.php
I dont really know if the code should be in the function _prepareForm or somewhere else! Could you please let me know!
Regards,
Santi
Thanks for your repsonse. Indeed it contained some bugs. I updated the post. It should work correctly. Can you confirm it if it works?
Any idea what im doing wrong?
I added all the code but its not saving the info in the database.
I do see the image in the media map.
I added filename to the database with varchar(255) utf8_general_ci
Hi, I face the same issue.
The image doesn’t get saved to the table.
Did you got this issue solved? 🙁
Hi,
same thing here. The new field is displayed in backend but not stored in the database and not displayed when saved.
What is missing (i use the last version of aw_blog).
thanks
Hi, I have the same problem. I added the image field via system.xml though, not programatically.
How do you add alt tags to the image for SEO purposes?
Its not working for me. I created a field named ‘filename’ in database after adding above code, but nothing worked for me.
For everyone who has problems saving the file to the database, please refresh your indexes.
Tks for this, it’s works great! I do encounter a small issue – when the post is edited, the image is deleted from DB. Any clue? Tks for help
You just need to add below line
$model->setFeaturedImage($_FILES[‘featured_image’][‘name’]);
before
$model->save();
in app/code/community/AW/Blog/controllers/Manage/BlogController.php file.
Is there a way of implementing this within an extension rather than amending AW Blogs core code? So that AW Blog could be updated in the future and you could retain any additional fields added?
I have an issue when you upload an image and click save, it saves with the image but if you go back edit the post content then click save it wipes out the image, and you have to re add it again :/
Pingback: Get Full Image path on frontend uploaded from custom field in database - Gomagento2
Pingback: Get Full Image path on frontend uploaded from custom field in database - MageTechWeb
This works great for me, thanks v much for the guide!
One quick amendment. The version I’m using has a slightly different file structure.
Instead of “/app/code/community/AW/Blog/controllers/Manage/BlogController.php”
It’s
“app/code/community/AW/Blog/controllers/Adminhtml/Awblog/Manage/BlogController.php”
(Just the addition of the adminhtml folder)