Several checkboxes in theme

Hello there,

I am modifying our theme for OJS 3.2.1. I want to add an option with several checkboxes (for example to select several issues which will be shown on the front page), but I’ve noticed that if I check one of the checkboxes, all are selected. Is this normal? It seems strange to me, because there are other uses of checkboxes, which I can select individually.

Kind regards
Daniela

Hi @UBWolf,

It it possible that they all share the same element ID?

Regards,
Alec Smecher
Public Knowledge Project Team

Hi @asmecher,

thanks for your reply. Do you mean the name?

I’ve used this code:

    $this->addOption('test', 'FieldOptions', [
           'label' => __('manager.setup.contextSummary'),
           'options' => [
                   [
                          'value' => '0',
                           'label' => __('plugins.themes.ubSiteTheme01.option.0'),
                   ],
                   [
                           'value' => '1',
                           'label' => __('plugins.themes.ubSiteTheme01.option.1'),
                   ],
                   [
                           'value' => '2',
                           'label' => __('plugins.themes.ubSiteTheme01.option.2'),

                   ],
                   [
                           'value' => '3',
                           'label' => __('plugins.themes.ubSiteTheme01.option.3'),
                   ],
              ],

           'default' => '0',
   ]);

The theming guide (https://docs.pkp.sfu.ca/pkp-theming-guide/en/theme-options-api) only mentions one checkbox, but I had hoped I could just add another few. Clearly I was wrong :wink:

Edit: I have also another question: I want to add a textarea field in the theme settings, but struggle to make it usable in several languages. How can I do that?

Kind regards
Daniela

Hi @UBWolf,

In a FieldOptions field type where the input is a checkbox (not radio), the value needs to be an array. Try setting the default to [0]. Also, it may cause problems for you if the values are strings and not integers. Each value should be 0 not '0'.

(If a pre-existing value is in the database and it is not an array, this might cause a problem. I’d recommend clearing any pre-existing value for this theme setting from the plugin_settings table.)

I’m not 100% sure that a theme option accepts a multilingual input, but it should. You can do this with:

$this->addOption('test', 'FieldOptions', [
	'label' => __('manager.setup.contextSummary'),
	'isMultingual' => true,
	'options' => [
		...
	],
]);

Hello @NateWr,

thank you so much for your help with the checkboxes! Especially the hint about deleting the theme settings from the plugin_settings table was very helpful. It almost works, I can select only one option without selecting the others and it also saves only the one option. However, once I’ve saved, all checkboxes are selected and can only be deselected together (so it’s the exact same behaviour as before). It can be selected individually once I have deleted the entry from the database.
This is the entry I get in the database for selecting only one option: s:18:“a:1:{i:0;s:1:“2”;}”;
I also had to set the default value tu [] instead of [0], because otherwise the “0” would be saved as well, but I don’t have any ID with this value.
I’m very sorry to trouble you with this.

As for the other question about using text area fields with different languages in the theme settings it doesn’t work (unfortunately). I have added “‘isMultingual’ => true,” but I don’t get the option to change the language. At the top of the page where all languages should be listed, there are none. So I guess it isn’t implemented for the appearance settings.

Kind regards
Daniela

However, once I’ve saved, all checkboxes are selected and can only be deselected together

Ahh, you’re right. The value is not unserialized from the database. I’ve filed an issue to fix this: Can't use array values in theme options · Issue #6184 · pkp/pkp-lib · GitHub.

As for the other question about using text area fields with different languages in the theme settings it doesn’t work (unfortunately).

I forgot to mention that you need to provide localized options. So your option set should look like this (depending on your locales):

		$this->addOption('test', 'FieldOptions', [
			'label' => 'test',
			'isMultilingual' => true,
			'options' => [
				'en_US' => [
					[
						'value' => 1,
						'label' => 'test1'
					],
					[
						'value' => 2,
						'label' => 'test2'
					],
				],
				'fr_CA' => [
					[
						'value' => 1,
						'label' => 'test1fr'
					],
					[
						'value' => 2,
						'label' => 'test2fr'
					],
				],
			],
			'default' => [],
		]);

This is not used very much, and there are going to be some issues with getting it to be generic no matter what languages are available. If you’re working with a constricted language set, you may be able to just specify them manually.

Otherwise, it might be worth thinking about an alternate approach. That’s because the multilingual feature was really designed for entering different text, rather than choosing different options. For example, you could add a separate option for each language (eg - my-option-en_US and my-option-fr_CA) and that approach might work better.

1 Like

Thank you very much for helping with the checkboxes. I’ll wait until you’ve fixed it.

As for the languages I don’t want to do it für the options but for a textfield. To be specific: Because there is no announcements tool for the site page I want to add a texfield where the admin can write important messages which are shown at the top of the page. It would be better if I could provide these messages in different languages.

Kind regards
Daniela

Ah, I see what you’re saying. The field can be made multilingual and the form responds, but the data isn’t saved properly. Sorry about that! Theme options still haven’t been widely used. I’ll try to get this sorted alongside the array issue with the FieldOptions.

1 Like

Thank you very much :slight_smile: