In my custom theme I would like to add an optional field to upload an additional image.
I would like to understand how to do it for both journals and the main site.
Unfortunately, addOption method doesn’t support file uploads: pkp-lib/ThemePlugin.inc.php at b92e3ea9cc773d9a602b17aeaf8a4280a5bf029a · pkp/pkp-lib · GitHub. Thus, to implement this approach would be quite tricky - overring the method from a theme and implement all the background logic.
Depending on the task, the easier solution would be hardcode it in the template or the text option that is expecting URL pointing to the image.
Hi @Vitaliy ,
in function init() I do:
$request = Application::get()->getRequest();
$dispatcher = $request->getDispatcher();
$temporaryFileApiUrl = $dispatcher->url($request, ROUTE_API, CONTEXT_ID_ALL, ‘temporaryFiles’);
So then
$this->addOption(‘my_new_option_name’,‘FieldUploadImage’,array(
‘label’ …
…
‘options’ => [
‘url’ => $temporaryFileApiUrl,
]
))
At this point I have the upload field working.
After the upload, the image file is deposited in the temporary area which I would not know how to move to the public area.
Hmm, and it should contain also url. I didn’t get a chance to test this new feature with additional option types and this requires diving into the code. @NateWr, do you happen to know where the problem might be here?
I haven’t looked closely, but I think you’ll struggle to get this to work properly. Where the image upload field is working now it expects that the handler will receive a specific value pointing to a temporary file, move the file into the correct place, and rewrite the option value before saving.
The handler you would want to intervene in is here. However, have you considered using the Publisher Library instead? The user could upload a file to the publisher library, make it public, then enter the URL into a text theme option.
Hi @NateWr ,
I needed a valid upload option for the theme at both the journal and global site level.
The Publisher Library could be a valid temporary solution for a journal theme but not for global site.
Thank you very much I will try another way without using files to upload.