I want to have a multiple upload for a page called "product"
so i modify the product model with
function form_fields($values = array(), $related = array())
{
$fields = parent::form_fields($values, $related);
$fields['image']=array('type'=>'asset', 'class'=>'multifile'); // i also tried to add multiple=>true
$fields['image']['folder']='images/products';
return $fields;
}
The image field is parsed to show "upload asset" and "select image" buttons.
1. clicking the "upload asset" button, i uploaded a file, then the "image" field is populated with the uploaded file name, but when i upload another, the "image" field is overwritten with the new one, i dont think this is the right way of multiple upload, at least both uploaded file name must be present. i cant find ways for me to do multiple upload
i used on_after_post to check the values, no values is detected..
function on_after_post($values){
$CI =& get_instance();
$data = $this->fuel->assets->uploaded_data(); // also tried $data=$CI->upload->data();
var_dump($data);
exit;
}
2. i tried quite a while and managed to upload few images, as i can see by clicking the "select images" button, newly uploaded images are shown.
with $fields['image']=array('type'=>'asset', 'class'=>'multifile', 'multiple'=>true);
by select images from drop down one by one, i can see the "image" field now is populated by selected image name separated with comma. but after i save, using on_after_post and vardump the $values['image'], i got a strange value of image values such as string(43) "["ayase-haruka_1.jpg","ayase-haruka_5.jpg"]" . This is not an array, i cant loop through it.
Question 1, how can i do multiple upload correctly? i searched, it says use "multifile" but apparaently it is not working at the moment
Question 2, anyway to multiple select images from the pop up modal of "select image" maybe by checkbox instead of using dropdown select which is quite tedious.
Question 3, when i am selecting using "select images", how can i get the array of images so i can loop.
Thanks
Comments
https://github.com/daylightstudio/FUEL-CMS/commit/6d3fed52fed35902fc41a13c61c9f8529a9af52d
When that field type is set to "multiple" it json_encodes the data as an array (which is what you were seeing) so you should set the field you are saving it in as a serialized field for your model so that it will properly decode the data upon retrieval:
public $serialized_fields = array('image');
You can also use the field type "file" and set the parameter "multiple" => TRUE. Then in your model, you can use on_after_post hooks to do any further processing of the images. The uploaded image data is saved under the $this->fuel->assets->uploaded_data().
$my_record->image; // will return an array of image names if serialized_fields is set for "image" field
If you use a "file" field type, you can access the uploaded image information in your model to do further processing on save (e.g. change image dimensions, concatenate information and save into a different field, etc), you can grab all the uploaded image info from the $this->fuel->assets->uploaded_data(). To see it in action, add a "file" field type in your model's form_fields method. Then create an on_after_post method on your model:
function on_after_post($values) { print_r($this->fuel->assets->uploaded_data()); exit(); }
Because this product form has other fields ( not only to do upload), so if i am editing the products, i am able to select the images for this product if the images already uploaded using "Select Images" field without the need to upload again. So when i use this button, i am able to select a list of images separated by comma in the image field, i am not able to use $this->fuel->assets->uploaded_data() because there will not be any upload, so how can i do in this case.
The multiple image upload for the "asset" field type not adding a comma is a bug that I just pushed a fix for in the develop branch on FUEL:
https://github.com/daylightstudio/FUEL-CMS/commit/6d3fed52fed35902fc41a13c61c9f8529a9af52d
when uploading, there still some problems:
1. when the field is empty and do a first time upload of ONE image, there is a comma in front of the image such as ",abc.jpg"
2. On the pop up window to do upload, if we select multiple images, then click upload, in the end, only first image is append to the current field values
e.g. image field already got value of "abc.jpg"
then click upload assets, in the pop up window select "abc2.jpg" , "abc3.jpg", click upload
the field only displays "abc.jpg,abc2.jpg"
i checked js, seem no problem, but no sure why the problem happens.
https://github.com/daylightstudio/FUEL-CMS/tree/develop
The easiest way to make updates is to use Git and add the following as a remote repository to pull from:
https://github.com/daylightstudio/FUEL-CMS.git
Then you can pull from the develop branch.