I need to add an image field to the publication form, but I can’t get it to upload correctly. This is what I have so far. Can anyone help me?
<?php
import('lib.pkp.classes.plugins.ThemePlugin');
use APP\issue\Collector;
use APP\facades\Repo;
class ExampleThemePlugin extends ThemePlugin
{
public function init()
{
(...)
HookRegistry::register('TemplateManager::display', array($this, 'loadCurrentUrl'));
HookRegistry::register('TemplateManager::display', array($this, 'loadCCLicenseBadge'));
HookRegistry::register('TemplateManager::display', array($this, 'loadTemplateData'));
HookRegistry::register('TemplateManager::display', array($this, 'loadKeywords'));
HookRegistry::register('TemplateManager::display', array($this, 'articleMetrics'));
HookRegistry::register('TemplateManager::display', array($this, 'loadMetrics'));
HookRegistry::register('Schema::get::publication', array($this, 'addToPublicationSchema'));
HookRegistry::register('Form::config::before', array($this, 'addToPublicationForm'));
HookRegistry::register('LoadHandler', array($this, 'setPageHandler'));
}
public function addToPublicationForm($hookName, $form)
{
$request = Application::get()->getRequest();
$context = $request->getContext();
if (!$context || !defined('FORM_TITLE_ABSTRACT') || $form->id !== FORM_TITLE_ABSTRACT) {
return;
}
$dispatcher = $request->getDispatcher();
$publicFileManager = new PublicFileManager();
$baseUrl = $request->getBaseUrl() . '/' . $publicFileManager->getContextFilesPath($context->getId());
$temporaryFileApiUrl = $dispatcher->url($request, Application::ROUTE_API, $context->getPath(), 'temporaryFiles');
$form->addField(new \PKP\components\forms\FieldUploadImage('publicationImage', [
'label' => 'Imagen de la publicación',
'description' => 'Suba una imagen representativa para esta publicación.',
'groupId' => 'publishing',
'value' => $form->publication->getData('publicationImage'),
'baseUrl' => $baseUrl,
'options' => [
'url' => $temporaryFileApiUrl,
],
]));
}
public function addToPublicationSchema($hookName, $args)
{
$schema = $args[0];
$schema->properties->publicationImage = (object) [
'type' => 'object',
'apiSummary' => true,
'validation' => ['nullable'],
];
return false;
}
(..)
}
Thanks in advance.