0

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?

3
  • You should post the contents of the service provider.
    – Sharky
    Commented Aug 15, 2023 at 9:26
  • Just added the service provider
    – cappu
    Commented Aug 15, 2023 at 10:53
  • I coded what you wrote and it worked ok for me. Have you got <folder plugin="replacer">services</folder> within the <files> section of your manifest file? Commented Aug 15, 2023 at 19:22

2 Answers 2

1

If you add a call to the parent constructor in your plugin then it will retrieve the plugins configuration and prime the property of $this->param with the values for you.

public function __construct(&$subject, $config)
{
    parent::__construct($subject, $config);

    $fred = $this->params->get('is_debug',false);

}

You will need to edit and save the Plugin at least once after it has been Installed or Discover'ed to save the Params to the field in the record for your plugin in the #__extensions table.

You can check the #__extensions table and see if there is anything saved in the params field of the record for your plugin. If it is empty then you will see the symptoms you are describing.

5
  • When I do, the error message is still there and the same.
    – cappu
    Commented Aug 15, 2023 at 8:40
  • Answer updated.
    – Irata
    Commented Aug 15, 2023 at 9:53
  • I did check the database, too. The parameter is set {"is_debug":"1"}, but it still shows the error message.
    – cappu
    Commented Aug 15, 2023 at 10:49
  • Can you put the error message in your original post please.
    – Irata
    Commented Aug 15, 2023 at 11:31
  • Just added the error message
    – cappu
    Commented Aug 15, 2023 at 12:05
0

Finally I found the error: In my Plugin the plugin attribute in the configuration xml <files><folder plugin="**replacer**">services</folder> did not match the 2nd parameter of the getPlugin call in the service provider: $config = (array) PluginHelper::getPlugin('system', '**replacer**');

For debugging, I found it useful to temporarily print the $config array in the service provider.

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.