How to push params into an admin form

30 views
Skip to first unread message

Paul van Haren

unread,
Nov 26, 2017, 11:02:23 AM11/26/17
to Joomla! General Development
I'm developing a component that has a lot of item parameters. I'd like to store these as params in the database. In the XML file for the form definition, these parameters are grouped into a fields section:

<fields name="params">
<fieldset name="parameters">
<field name="key" type="text" default="" label="MY_KEY" />
<field name="logo" type="filelist" default="default" label="LOGO_LABEL" 
directory="administrator/components/com_mycomponent/assets/images/logos" hide_none="1" hide_default="1" />

This works fine for creating the form and saving the data. I can see in the database that in the params data the JSON string is containing the values for "key" and "logo".  
When loading the form and item data, I can find that the this takes place as expected:

public function display($tpl = null)
{
// Get the Data
$this->form = $this->get('Form');
$this->item = $this->get('Item');
parent::display($tpl);
        }

When rendering the form, the renderer calls 

       <?php echo $this->form->renderFieldSet("parameters"); ?>

Again, this works fine apart from the fact that the values of the parameters are not in the form. These parameters are accessible as $this->item->params['data']. What is needed to have the form make use of these loaded values?

Your help is appreciated.

Paul van Haren

unread,
Nov 27, 2017, 1:36:20 PM11/27/17
to Joomla! General Development
FIXED.

In the model code (com_mycomponent->models->mycomponent.php) , there is the implementation of loadFormData(). Typically, it looks like this:

protected function loadFormData()
{
// Check the session for previously entered form data.
$data = JFactory::getApplication()->getUserState('com_mycomponent.edit.mycomponent.data',array());

if (empty($data))
{
$data = $this->getItem();
}
return $data;
}

Adding the lines just before the return statement

$this->preprocessData('com_mycomponent.mycomponent', $data);
$params = $data->params['data'];
$data->params = $params;
solves the issue.

Reply all
Reply to author
Forward
0 new messages