I have a Joomla 4 plugin that uses a ServiceProvider which basically works fine. It's just that I can't access the plugin's configuration parameters.
namespace <Domain>\Plugin\System\Replacer\Extension;
// no direct access
defined('_JEXEC') or die;
use Joomla\CMS\Plugin\CMSPlugin;
use Joomla\Event\Event;
use Joomla\Event\SubscriberInterface;
/**
*
*/
class Replacer extends CMSPlugin implements SubscriberInterface
{
protected $app;
public static function getSubscribedEvents(): array
{
return [
'onAfterRender' => 'replaceStrings',
];
}
public function replaceStrings(Event $event)
{
echo $this->params->get('is_debug'); // Call to a member function get() on null!!!
$body = $this->app->getBody();
....
}
}
The configuration xml:
<?xml version="1.0" encoding="utf-8"?>
<extension method="upgrade" type="plugin" group="system">
...
<files>
<folder plugin="replacer">services</folder>
<folder>src</folder>
<folder>language</folder>
</files>
<config>
<fields name="params">
<fieldset name="basic">
<field name="is_debug" class="btn-group" type="radio" layout="joomla.form.field.radio.switcher" label="Debug" default="0" description="Debug Description">
<option value="0">JNO</option>
<option value="1">JYES</option>
</field>
</fieldset>
</fields>
</config>
</extension>
The ServiceProvider:
use Joomla\CMS\Extension\PluginInterface;
use Joomla\CMS\Factory;
use Joomla\CMS\Plugin\PluginHelper;
use Joomla\DI\Container;
use Joomla\DI\ServiceProviderInterface;
use Joomla\Event\DispatcherInterface;
use <Domain>\Plugin\System\Replacer\Extension\Replacer;
return new class() implements ServiceProviderInterface
{
public function register(Container $container)
{
$container->set(
PluginInterface::class,
function (Container $container) {
$config = (array) PluginHelper::getPlugin('system', 'replacer');
$subject = $container->get(DispatcherInterface::class);
$app = Factory::getApplication();
$plugin = new Replacer($subject, $config);
$plugin->setApplication($app);
return $plugin;
}
);
}
};
This is the error message:
0
Call to a member function get() on null
What am I missing?