[OJS 3.4.0.5 - Theming] Context at frontend not update custom field to schema

Describe the issue or problem
Hello all,
I’m create new theme, I want to add some custom field to supplement information at frontend. But at backend, custom field has been add to form and data has updated after I submited. But at frontend, data has not been load. As I checked, the context’s schema does not exist custom fields, and of course the data does not exist in the context object.

Steps I took leading up to the issue

I added to hook ‘Schema::get::context’ and ‘Form::config::before’ to add new field’s config to context’s schema in init method of my theme plugin class. I consulted the previous topic and added to the command line $contextSchema = Services::get('schema')->get(PKPSchemaService::SCHEMA_CONTEXT, true); to make sure the scheme was loaded.


class HuafThemePlugin extends \PKP\plugins\ThemePlugin
{
    /**
     * @copydoc ThemePlugin::isActive()
     */
    public function isActive()
    {
        ...
    }
    /**
     * Initialize the theme's styles, scripts and hooks. This is run on the
     * currently active theme and it's parent themes.
     *
     */
    public function init()
    {

        // Use a hook to extend the context entity's schema
        Hook::add('Schema::get::context', array($this, 'addToSchema'));
        $contextSchema = Services::get('schema')->get(PKPSchemaService::SCHEMA_CONTEXT, true);
        Hook::add('Form::config::before', array($this, 'addToForm'), Hook::SEQUENCE_LAST);
        ...
     }

    public function addToSchema($hookName, $args)
    {
        $schema = $args[0];
        $schema->properties->footerInstitutionalName = (object) [
            'type' => 'string',
            'apiSummary' => true,
            'multilingual' => true,
            'validation' => ['nullable']
        ];

        return false;
    }

    public function addtoForm($hookName, $form)
    {

        // Only modify the masthead form
        if (!defined('FORM_APPEARANCE_SETUP') || $form->id !== FORM_APPEARANCE_SETUP) {
            return;
        }

        // Don't do anything at the site-wide level
        $context = Application::get()->getRequest()->getContext();
        if (!$context) {
            return;
        }

        if (is_null($form->getField('footerInstitutionalName'))) {
            $form->addField(new \PKP\components\forms\FieldText('footerInstitutionalName', [
                'label' => __('plugins.themes.huaf.footer.institutional_name_label'),
                'size' => 'large',
                'isRequired' => true,
                'isMultilingual' => true,
                'groupId' => 'theme_footer_institutional_details',
                'value' => $context->getData('footerInstitutionalName'),
                'description' => __('plugins.themes.huaf.footer.institutional_name_description'),
                'default' => ''
            ]));
        }

        return false;
    }
    ...
}

Although new field has been added to the form and data downloaded right at the backend. But in Frontend, I checked, shema and object of the context not have that.
What application are you using?
I’m using OJS 3.3.0-4

Anyone who has encountered this error and can help me. Sincerely thank.

I have found the solution after a period of reading all the source code. I would like to leave my solution here so that others if you encounter a similar case can be used. And anyone who has a better solution can leave a comment to improve it.

To get the current context with schema updated with custom fields, I use the following code so that the shema of the context is forced to reload.

Services::get('schema')->get(PKPSchemaService::SCHEMA_CONTEXT, true);

Then take the current context through the getContext method of the current router object, this method has the second parameter forceReload needs to be put into true to reset a context even if it’s already been loaded.

The solution is done according to the code below:

$contextSchema = Services::get('schema')->get(PKPSchemaService::SCHEMA_CONTEXT, true);
$currentContext = $request->getRouter()->getContext($request, true);
2 Likes

This topic was automatically closed after 8 days. New replies are no longer allowed.