2

I made a small plugin, which uses a repeatable subform param element. It doesn't really matter what the plugin does:

<config>
    <fields name="params">
        <fieldset name="basic">
            <field name="events" type="subform" hiddenLabel="true" label="Event" multiple="true" buttons="add,remove,move" default='{"events0":{"event":"myevent1","receiver":["site","contact","customer"],"active":"1"},"events1":{"event":"myevent2","receiver":["site"],"active":"1"},"events2":{"event":"myevent3","receiver":["site","contact","customer"],"active":"1"},"events3":{"event":"myevent4","receiver":["site","contact","customer"],"active":"1"}}'>
                <form>
                    <field name="event" type="list" label="Event">
                        <option value="myevent4">myevent4</option>
                        <option value="myevent1">myevent1</option>
                        <option value="myevent3">myevent3</option>
                        <option value="myevent2">myevent2</option>
                    </field>
                    <field name="receiver" type="list" multiple="true" label="RECEIVERS" default="site" validate="options" layout="joomla.form.field.list-fancy-select">
                        <option value="site">JSITE</option>
                        <option value="contact">CONTACT</option>
                        <option value="customer">CUSTOMER</option>
                    </field>
                    <field name="active" type="radio" default="0" label="ACTIVE" class="btn-group btn-group-yesno" layout="joomla.form.field.radio.switcher">
                        <option value="0">JNO</option>
                        <option value="1">JYES</option>
                    </field>
                </form>
            </field>
        </fieldset>
    </fields>
</config>

The problem is, when installing the plugin, the param row in the #__extensions column gets prefilled with the default values defined here. But they are getting escaped from the installer.

This is a part of the escaped string in the params column:

{"events":"{\"events0\":{\"event\":\"myevent1\",\"receiver\":[\"site\",\"contact\",\"customer\"] ...

Such escaped params cannot be accessed in code via:

$this->params->get('events')

I wrote the default values like seen here:
https://github.com/joomla/joomla-cms/pull/31332

When opening the plugin options in the backend, the default value works. By doing nothing other than saving the plugin again, the default values are no longer escaped and also work when accessing them.

I would like the user to be able to use the plugin right after installing, without the need to go and save it once.

What needs to be done to get default subform values NOT escaped in the database when installing or discovering the plugin the first time?

Update

By doing this I can avoid the issue, but it's very ugly:

$params = (array) (\is_string($this->params->get('events')) ? json_decode($this->params->get('events')) : $this->params->get('events'));
2
  • As I see it, your code is not ugly, it is just how it is working at subform field. As I remember, the default value can only be a ‘string’ otherwise it is going to be nulled, and then values can be strings or arrays, but not as default. So you at the moment do not have other choice.
    – Zollie
    May 24, 2023 at 9:47
  • I am sorry I did not have time to copy the corresponding codes to this, that is why I am just short commenting on this. (I’ll try it later if needed).
    – Zollie
    May 24, 2023 at 9:51

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.