1

I created a system plugin with the onContentPrepareForm() method to load an entire module options form, and the form loads correctly, but will not save the entered data.

The coding

/plugins/system/dropmods/dropmods.php

public function onContentPrepareForm($form, $data) 
{
  if( $form->getName() == 'com_modules.module' && $data->module == 'mod_stuff' ) {
    JForm::addFormPath(__DIR__.'/mods/');
    $form->loadFile('mod_stuff', false);
  }
}

The override form mod_stuff.xml

<?xml version="1.0" encoding="UTF-8"?>
<form>
<fields name="params">
    <fieldset name="basic">
        <field type="textarea" name="thetextarea" label="Write Stuff" />
        <field type="text" name="thetext" label="Just Write" />
    </fieldset>
</fields>
</form>

The module's manifest XML mod_stuff.xml

modules/mod_stuff

The manifest just serves as an item to register the module but all files are sourced from the plugin. The installation process of the plugin creates the module folder and registers to the DB _extensions table

<?xml version="1.0" encoding="utf-8"?>
<extension type="module" version="3.1" client="site" method="upgrade">
    <name>mod_stuff</name>
    <author>The author</author>
    <creationDate>July</creationDate>
    <copyright>Copyright here</copyright>
    <license>GNU General Public License stuff</license>
    <authorEmail></authorEmail>
    <authorUrl></authorUrl>
    <version>1.0</version>
    <description>the detail</description>
</extension>

mod_stuff.php

modules/mod_stuff

<?php defined('_JEXEC') or exit('stuff');

include JPATH_PLUGINS.'/system/dropmods/mods/mod_stuff.php';

What has worked

If I use a subform method in the module's manifest file modules/mod_stuff/mod_stuff.xml to load the override file, the data saves correctly, however the subform process create additional parent object name settings and it poses a bit of challenge.

<config>
    <fields name="params">
        <fieldset name="basic">
            <field 
            type="subform" 
            name="settings" 
            formsource="plugins/system/dropmods/mods/mod_stuff.xml" 
            hiddenLabel="true" 
            />
        </fieldset>
    </fields>
</config>

What's required to allow the plugin override form to save the data?

1 Answer 1

2

There's a quirk when using onContentPrepareForm event. Data may not always be available and sometimes it can be an array instead of an object. You can confirm this by inspecting PHP error log. You should find entries like these:

PHP Warning: Attempt to read property "module" on array in C:\wamp\www\joomla-cms-3\plugins\system\example\example.php on line 31

PHP Warning: Undefined property: stdClass::$module in C:\wamp\www\joomla-cms-3\plugins\system\example\example.php on line 31

To workaround the issue, get data from input if it's not set and cast it to object:

$data = (object) ($data ?: $this->app->input->get($form->getFormControl(), [], 'array'));
1
  • There is no error thrown and no error logs to see. The work-around didn't deliver the data, still an empty object. It seems $data is only scanning the plugin's scope on save
    – Nadal
    Commented Jul 24, 2021 at 22:31

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.