This code is used to create a customize field which is used to upload a image and display it while creating a new article or product in your component.
It will display image when you edit any item from your component and you can upload other image.
create a file in administrator/components/com_your_component/models/filed/uploadimage.php
create a file in administrator/components/com_your_component/models/forms/uploadimage.xml
add enctype="multipart/form-data" attribute of the from in below file:
administrator/components/com_your_component/views/your_view/tmpl/edit.php
In administrator/component/com_your_component/models/your_model.php
write below code into save function
It will display image when you edit any item from your component and you can upload other image.
create a file in administrator/components/com_your_component/models/filed/uploadimage.php
/**
* @package Joomla.Platform
* @subpackage Form
*
* @copyright Copyright (C) 2005 - 2014 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE
*/
defined('JPATH_PLATFORM') or die;
jimport('joomla.image.image');
/**
* Form Field class for the Joomla Framework.
*
* @since 0.0.6
*/
class JFormFieldUploadImage extends JFormField
{
/**
* The form field type.
*
* @var string
* @since 1.6
*/
protected $type = 'UploadImage';
/**
* Method to get the field input markup.
*
* @return string The field input markup.
*
* @since 1.6
*/
protected function getInput()
{
$html = array();
$html[] = '<div class="button2-left">';
$html[] = '<input type="file" name="' . $this->name . '"/>';
$html[] = '</div>';
$html[] = '<div>';
$html[] = '<img src="' . JUri::root() . 'media/com_your_component/images/category/ ' . $this->value . '"/>';
$html[] = '<input type="hidden" value="' . $this->value . '" name="jform[hiddenimag e]"/>';
$html[] = '</div>';
return implode("\n", $html);
}
}
create a file in administrator/components/com_your_component/models/forms/uploadimage.xml
<?xml version="1.0" encoding="utf-8"?>
<form>
<fieldset>
<field name="image"
default=""
type="UploadImage"
hide_none="1 "
label="COM_YOUR_COMPONENT_FIELD_PARAMS_IMAGE_LABEL"
description="COM_YOUR_COMPONENT_FIELD_PARAMS_IMAGE_DESC"
componentname="com_your_component"
/>
</fieldset>
</form>
add enctype="multipart/form-data" attribute of the from in below file:
administrator/components/com_your_component/views/your_view/tmpl/edit.php
In administrator/component/com_your_component/models/your_model.php
write below code into save function
$table = $this->getTable();
$input = JFactory::getApplication()->input;
$pk = (!empty($data['id'])) ? $data['id'] : (int) $this->getState($this->getName() . '.id');
$isNew = true;
$hiddenImage = array();
$hiddenImage = $input->post->get('jform', array(), array());
// Retrieve file details from uploaded file, sent from upload form
$file = $input->files->get('jform');
// Clean up filename to get rid of strange characters like spaces etc
$data['image'] = time() . '_' . JFile::makeSafe($file['image']['name']);
// Set up the source and destination of the file
$src = $file['image']['tmp_name'];
$dest = JPATH_SITE . '/media/com_your_component/images/category/' . $data['image'];
// First check if the file has the right extension, we need jpg only
if (!empty($file['image']['name']))
{
if (preg_match('/(.)+jpe*g|gif|png/i', $file['image']['name']))
{
if (!JFile::upload($src, $dest) )
{
$this->setError(JText::_('Invalid Image Type'));
return false;
}
else
{
unlink(JPATH_SITE . '/media/com_your_component/images/category/' . $hiddenImage['hiddenimage']);
}
}
}
elseif (!empty($hiddenImage['hiddenimage']))
{
$data['image'] = JFile::makeSafe($hiddenImage['hiddenimage']);
}
else
{
$this->setError(Jtext::_('Invalid Image Type Not upload condition..'));
return false;
}
No comments:
Post a Comment