I'm trying to write a plugin that needs to use the same subform twice, and I want to set different default values for these two subforms. The config part of my XML file looks like this:
<config> <fields name="params"> <fieldset name="basic" addrulepath="/path/to/rules"> <field name="first" type="subform" formsource="/path/to/subform.xml" label="First" default='{"length_max":"380", "width_max":"265", "height_max":"32", "weight_max":"2000", "rate":"4.85"}' /> <field name="second" type="subform" formsource="/path/to/subform.xml" label="Second" default='{"length_max":"1000", "width_max":"500", "height_max":"500", "weight_max":"10000", "rate":"7.65"}' /> </fieldset> </fields> </config> These fields are displaying properly on the plugin settings page and also showing the default values I specified.
However, if I check the database after installing the plugin, the params field in the *_extensions table looks like this (note the extra slashes):
{"first":"{\"length_max\":\"380\", \"width_max\":\"265\", \"height_max\":\"32\", \"weight_max\":\"2000\", \"rate\":\"4.85\"}","second":"{\"length_max\":\"1000\", \"width_max\":\"500\", \"height_max\":\"500\", \"weight_max\":\"10000\", \"rate\":\"7.65\"}"} When I then try to access the "first" parameter from my plugin php file using $this->params->get('first') it returns a string:
string(94) "{"length_max":"380", "width_max":"265", "height_max":"32", "weight_max":"2000", "rate":"4.85"}" If I now go to the plugin settings page and don't change anything but click Save, the database params value changes to the following (note the slashes are now gone):
{"first":{"length_max":"380","width_max":"265","height_max":"32","weight_max":"2000","rate":"4.85"},"second":{"length_max":"1000","width_max":"500","height_max":"500","weight_max":"10000","rate":"7.65"}} And if I now get the "first" parameter (again using $this->params->get('first')), it returns an object:
object(stdClass)#1321 (5) { ["length_max"]=> string(3) "380" ["width_max"]=> string(3) "265" ["height_max"]=> string(2) "32" ["weight_max"]=> string(4) "2000" ["rate"]=> string(4) "4.85" } Problem:
I want to be able to consistantly get either a string or an object for the parameter, not one or the other depending on whether the user has gone to the plugin settings page and clicked Save.
Am I doing something wrong in setting the default values for the fields, or in getting the parameter from the database? Is there another function I should be using to get the value from the database instead of $this->params->get(...)?
(Edit: using Joomla version 4.4.1 with php 7.4.28)