Closed buddh4 closed 8 years ago
You don't even have to forward your request, just upload your files to: /file/file/upload
and submit the returned guid
Yes I had a reason why I used manual file upload , because I already used the default humhub file upload but when I upload and save or update news module it prints the JSON output in the browser not appending to the stream. I requested helps from everyone but no response so I used this.
Ah ok, the file handling looks good anyways, so I think its ok for now.
But If I am allowing multiple images to be added on a news post then I have to use the humhub file upload right? Whats that attaching a file with news activerecord and marked as persistent?
Attaching the news record to a file means setting the object_model and object_id of the file database entry, this is needed if you want to upload a file before saving your activerecord. All file entries without a given object_id/object_model are removed by a cronjob.
In my case will that removed by the cronjob?
As I checked, there is a file db entry with object_model/object_id created for your uploaded images, but honestly I didn't really figured out where the file entry is created its a bit confusing since there are multiple upload functions.
HI Julian
You advised me to attach the file on the controller where the form is handled.
This is the line where we save the image guid in news table - https://github.com/nadilhassan/newsmodule/blob/master/controllers/NewsController.php#L212
Is it OK if I add \humhub\modules\file\models\File::attachPrecreated($this->content, implode(',', $this->fileList)); this line below above code.
Meanwhile here I am getting only the image guid then how can I parse the parameters for the function attachPrecreated()? Can you please help me to fill these parameters.
the second argument of attachPrecreated is a comma seperated list of guids in your case i think you just have to set one guid. in version 1.2 we'll furthermore allow an array value.
You'll receive the guid after the upload of your file to 'file/file/upload'.
Yes I am OK with that GUID of the image. what about the first argument, what is to be in our case? a news model($model) or anything else?
Your news active record.
Thanks. I will to this 👍
I have done with this. Thanks for your support!
I'would suggest using the humhub file upload for your news images instead of handling it by yourself. Please check the following upload action of the custom pages module:
https://github.com/humhub/humhub-modules-custom-pages/blob/master/modules/template/controllers/UploadController.php#L27
In this example you forward your action to the
/file/file/upload
action, which will save your file and create an entry in the file table. You'll have to add the returned fileguid
to your form as hidden field. (Your files have to be set as $_FILES['files']).When submitting your file you'll have to attach the file, which means it will be attached to your news activerecord and marked as persistent (otherwise it will be removed by a cronjob)
as here: https://github.com/humhub/humhub-modules-custom-pages/blob/master/modules/template/models/forms/TemplateElementForm.php#L114
(the attachPrecreated function expects a comma seperated string list of guids, which is in your case just one guid)
Part of the file activerecord: https://github.com/humhub/humhub/blob/master/protected/humhub/modules/file/models/File.php#L412
And something like this should be added to your news activerecord:
https://github.com/humhub/humhub-modules-custom-pages/blob/master/modules/template/models/TemplateContentActiveRecord.php#L152