[OJS 3.2.0-3] FieldOptions - cannot select

Hi,

I’m trying to add a custom field (as shown in the Plugin Guide example):

        $ddcList = [
                0 => [ 'value' => 'ddc:000', 'label' => 'ddc:000 (Generalities, Science)'],
                1 => [ 'value' => 'ddc:004', 'label' => 'ddc:004 (Data processing Computer science)'],
                2 => [ 'value' => 'ddc:010', 'label' => 'ddc:010 (Bibliography)'],
        ];
        $form->addField(new \PKP\components\forms\FieldOptions('ddc', [
                        'label' =>  'ddcLabel',
                        'type' =>  'checkbox',
                        'value' => [0 => 'ddc:000'],
                        'options' => $ddcList,
        ]));

The problem is: I cannot select one of the boxes individually, it always behaves like ‘select all’.
It works if the name of the Field is ‘sidebar’ (not ‘ddc’), but that obviously isn’t a solution.
How can I get this working?

Another Question is: what should be the type of the corresponding schema?

Regards

@asmecher, can you probably help me out?

Hi @habib,

Hmm, the problem you describe (behaviour like ‘select all’) suggests that the variable being passed in as the value is not an array. Using the numeric indexes like you do shouldn’t be causing any issue. But I’m not sure how this gets encoded to JSON and then decoded into a JavaScript variable, so try removing them. (You can also remove the 'type' => 'checkbox' since this is the default.)

$form->addField(new \PKP\components\forms\FieldOptions('ddc', [
	'label' =>  'ddcLabel',
	'value' => ['ddc:000'],
	'options' => [
		['value' => 'ddc:000', 'label' => 'ddc:000 (Generalities, Science)'],
		['value' => 'ddc:004', 'label' => 'ddc:004 (Data processing Computer science)'],
		['value' => 'ddc:010', 'label' => 'ddc:010 (Bibliography)']
	],
]));

If the user can select more than one option, the schema should probably be an array:

"ddc": {
	"type": "array",
	"validation": [
		"nullable"
	],
	"items": {
		"type": "string",
		"validation": [
			"in:ddc:000,ddc:004,ddc:010"
		]
	}
}

Only add the validation: ['nullable'] property if this is allowed to be empty.

Hi @NateWr,

thanks for your reply! I tried this:

                $schema->properties->ddc = (object) [
                    'type' => 'array',
                    'validation' => ['nullable'],
                    'items' => [
                            'type' => 'string',
                            'validation' => ['in:ddc:000,ddc:004,ddc:010']
                    ]
            ];

and

                $ddcOptions = [
                    ['value' => 'ddc:000', 'label' => 'ddc:000 Generalities Science'],
                    ['value' => 'ddc:004', 'label' => 'ddc:004 Data processing Computer science'],
                    ['value' => 'ddc:010', 'label' => 'ddc:010 Bibliography'],
            ];
            $form->addField(new \PKP\components\forms\FieldOptions('ddc', [
                    'label' => 'ddc Label',
                    'groupId' => 'publishing',
                    'description' => 'this is a description',
                    'options' => $ddcOptions,
                    'value' => $context->getData('ddc') ? $context->getData('ddc') : [],
            ]));

The Problem now is I get the error: “The form was not saved because 1 error(s) were encountered. Please correct these errors and try again.” and in my logfile:

[18-May-2020 11:36:55 UTC] PHP Warning: Invalid argument supplied for foreach() in /srv/www/ojs02/htdocs/lib/pkp/classes/handler/APIHandler.inc.php on line 284
[18-May-2020 11:36:55 UTC] PHP Notice: Undefined index: type in /srv/www/ojs02/htdocs/lib/pkp/classes/handler/APIHandler.inc.php on line 343
[18-May-2020 11:36:55 UTC] PHP Notice: Trying to get property ‘type’ of non-object in /srv/www/ojs02/htdocs/lib/pkp/classes/handler/APIHandler.inc.php on line 343
[18-May-2020 11:36:55 UTC] PHP Notice: Undefined index: validation in /srv/www/ojs02/htdocs/lib/pkp/classes/handler/APIHandler.inc.php on line 343
[18-May-2020 11:36:55 UTC] PHP Notice: Trying to get property ‘type’ of non-object in /srv/www/ojs02/htdocs/lib/pkp/classes/handler/APIHandler.inc.php on line 343
[18-May-2020 11:36:55 UTC] PHP Notice: Trying to get property ‘type’ of non-object in /srv/www/ojs02/htdocs/lib/pkp/classes/services/PKPSchemaService.inc.php on line 331
[18-May-2020 11:36:55 UTC] PHP Notice: Trying to get property ‘type’ of non-object in /srv/www/ojs02/htdocs/lib/pkp/classes/services/PKPSchemaService.inc.php on line 338

Regards,

Hi @NateWr,

it works properly when I insert the schema directly into schemas/context.json. What am I doing wrong?

Regards

Show me the full code where you modify the schema.

I used the Template plugin for testing purposes:

import('lib.pkp.classes.plugins.GenericPlugin');
class PluginTemplatePlugin extends GenericPlugin {
        public function register($category, $path, $mainContextId = NULL) {
                $success = parent::register($category, $path);
                if ($success && $this->getEnabled()) {
                    HookRegistry::register('Schema::get::context', array($this, 'addToSchema'));
                    HookRegistry::register('Form::config::before', array($this, 'addToForm'));
                }
                return $success;
        }

        public function addToSchema($hookName, $args) {
                        $schema = $args[0];
                        $schema->properties->institutionalHome = (object) [
                                'type' => 'string',
                                'apiSummary' => true,
                                'multilingual' => false,
                                'validation' => ['nullable']
                        ];
                        $schema->properties->ddc = (object) [
                                'type' => 'array',
                                'multilingual' => false,
                                'validation' => ['nullable'],
                                'items' => [
                                        'type' => 'string',
                                        'validation' => ['in:ddc:000,ddc:004,ddc:010']
                                ]
                        ];
        }

        public function addtoForm($hookName, $form) {
                if ($form->id !== 'masthead') {
                return;
                }

                $context = Application::getRequest()->getContext();
                if (!$context) {
                        return;
                }

                $form->addField(new \PKP\components\forms\FieldText('institutionalHome', [
                        'label' => 'Institutional Home',
                        'groupId' => 'publishing',
                        'value' => $context->getData('institutionalHome'),
                ]));

                $ddcOptions = [
                        ['value' => 'ddc:000', 'label' => 'ddc:000 Generalities Science'],
                        ['value' => 'ddc:004', 'label' => 'ddc:004 Data processing Computer science'],
                        ['value' => 'ddc:010', 'label' => 'ddc:010 Bibliography'],
                ];
                $form->addField(new \PKP\components\forms\FieldOptions('ddc', [
                        'label' => 'ddc Label',
                        'groupId' => 'publishing',
                        'description' => 'this is a description',
                        'options' => $ddcOptions,
                        'value' => $context->getData('ddc') ? $context->getData('ddc') : [],
                        //'value' => [],
                ]));
        }
[...]

Hi @NateWr,

thanks again for your help.
As I’ve written, FieldOptions() does work correctly when I write the schema directly into context.json.

Now in our real journals I want to add some custom fields to the metadata page under the keywords
(don’t know if this is a problem since the keywords are multilingual).
I can get it right with FieldSelect(), no problem. But when I use FieldOptions() as above, even with the schema in the json-file (this time publication.js),
the “select all”-behaviour occurs. Regardless if the “value” is an array or not. When there is no new schema in publication.json, this behaviour goes away. I no case can I save the form.
Another problem is how to get the submissionId/publication and so on. I guess the “form”-object is different in this case.
Sorry for asking so many questions…

(./lib/pkp/schemas/publication.json)

"otto": {
    "type": "array",
        "multilingual": true,
        "validation": [
            "nullable"
        ],
        "items": {
            "type": "string",
            "validation": [
                "in:ddc:000,ddc:004,ddc:010"
            ]
        }
},

(plugin)

HookRegistry::register('Form::config::before', array($this, 'addToPublicationForm'));

[...]

public function addtoPublicationForm($hookName, $form) {
    if ($form->id !== 'metadata') {
        return;
    }

    $ottoOptions = [
        ['value' => 'ddc:000', 'label' => 'ddc:000 Generalities Science'],
    ['value' => 'ddc:004', 'label' => 'ddc:004 Data processing Computer science'],
    ['value' => 'ddc:010', 'label' => 'ddc:010 Bibliography'],
    ];

    $form->addField(new \PKP\components\forms\FieldOptions('otto', [
            'label' => 'otto Label',
            'description' => 'this is a description',
            'options' => $ottoOptions,
            'value' => [],
        ]));
}

Hmm, the select-all problem you describe shouldn’t be effected by the schema. To test, I put the following code into a test plugin and it worked ok:

HookRegistry::register('Form::config::before', function($hookName, $form) {
	if ($form->id !== 'metadata') {
		return;
	}

	$ottoOptions = [
		['value' => 'ddc:000', 'label' => 'ddc:000 Generalities Science'],
		['value' => 'ddc:004', 'label' => 'ddc:004 Data processing Computer science'],
		['value' => 'ddc:010', 'label' => 'ddc:010 Bibliography'],
	];

	$form->addField(new \PKP\components\forms\FieldOptions('otto', [
		'label' => 'otto Label',
		'description' => 'this is a description',
		'options' => $ottoOptions,
		'value' => [],
	]));
});

It loaded the new field in the metadata form and I could select/de-select each option individually.

Are you running OJS from a release package or did you build it from the repository on GitHub?

Here’s a short video just to make sure we’re talking about the same thing.

metadata

Yes, that’s what I meant. It’s an updated OJS (3.0.2.0 … → 3.1.2.1 → 3.2.0.3) , with the newest release package.
I didn’t try it out with a fresh OJS.

I have another new updated and untouched OJS (no own themes or plugins), it shows the same behaviour. When I use the above code it works correctly as in your video. But that is the same with my other OJS, what I tried to explain before. The problem only starts when I try to implement the schema.
I will try and get another OJS from github.

Can you share the full PHP code where you add the property to the schema from your plugin? You shared a JSON snippet but I’d like to see where your plugin extends the schema.

        public function register($category, $path, $mainContextId = NULL) {
                $success = parent::register($category, $path);
                if ($success && $this->getEnabled()) {
                        HookRegistry::register('Schema::get::publication', array($this, 'addToPublicationSchema'));

                        HookRegistry::register('Form::config::before', array($this, 'addToPublicationForm'));
                }
                return $success;
        }

        public function addToPublicationSchema($hookName, $args) {
                        $schema = $args[0];
                        $schema->properties->otto = (object) [
                                'type' => 'array',
                                'multilingual' => false,
                                'validation' => ['nullable'],
                                'items' => [
                                        'type' => 'string',
                                        'validation' => ['in:ddc:000,ddc:004,ddc:010']
                                ]
                        ];
        }

Hi @NateWr ,

there was also a difference if I did it like this:

                $schema->properties->otto = json_decode('{
                                        "type": "array",
                                        "multilingual": false,
                                        "validation": [ "nullable" ],
                                        "items": {
                                                "type": "string",
                                                "validation": [
                                                        "in:ddc:000,ddc:004,ddc:010"
                                                ]
                                        }
                                }');

or like that:

                $schema->properties->otto = (object) [
                        'type' => 'array',
                        'multilingual' => false,
                        'validation' => ['nullable'],
                        'items' => [
                                'type' => 'string',
                                'validation' => ['in:ddc:000,ddc:004,ddc:010']
                        ]
                ];

It suddenly worked using json_decode, I could save the form a few times. Before that I toggled the ‘type’ fom ‘array’ to ‘object’ and back again.
After that I used the lower code (without json_decode), and then it did again not work.

I know it sounds crazy, but it felt like this is unpredictable.

I really need your full code, not just snippets. If you don’t want to share your code in a public forum you can send it by PM, but otherwise I can’t reproduce the problem locally and so I won’t be much help.

Dear @habib
is there any news about your plugin.
Did I understand correctly that it should add Dewey DDC groups to a submission. I would be interested in that.
Best
Klaus

Hi Klaus,

yes ist does, but it is mixed with other things, for example adding “status type” for Dini/Dnb. It is kind of a plugin only for admins (no settings for example), I never had time for things like that.
You can pm me if you want to.

Best Regards

Dear @habib
I’m working on a similar end. We might want to notify @carola about this to be intergrated to and get support by OJS-de.net · GitHub
Do you share the plugin on github?

best
Klaus