0

I am working on a system plugin which uses its own custom field. The plugin has an XML looking like this:

 <config>
        <fields name="params" addfieldprefix="Joomla\Plugin\System\MyPlugin\Field">
            <fieldset name="basic">
                <field name="api-key" label="api-key" type="password" />

Now i need to use the API key in the custom field. The custom field is working inside the plugin, but how to access the config param from the plugin inside the custom field?

namespace Joomla\Plugin\System\MyPlugin\Field;

use Joomla\CMS\Factory;
use Joomla\CMS\Form\Field\ListField;

\defined('_JEXEC') or die;

class MyCustomField extends ListField
{
    protected $type = 'MyCustom';

    public function getOptions()
    {
        // Trying to access the API Key from the Plugin config
        var_dump($this->form->getField('api-key', 'params'));

When using this i only get the data from the custom field, nothing about the plugin config embeding it:

echo $this->form->getData()

1 Answer 1

1

From within the field, you can get the plugin parameters from the PluginHelper

$plugin = PluginHelper::getPlugin('system','myplugin');
$params = new Registry($plugin->params);
$apiKey = $params->get('api-key');
2
  • But the field is allready called and embeded inside the plugin. Why get the plugin and its parameters again inside the field? Seams like bad redundancy.
    – maidan
    May 1, 2023 at 14:15
  • Although the field is defined and called from within the plugin, it is available to be used by other extensions outside of the plugin, which may or may not have the plugin parameters loaded. The field object doesn't have any specific knowledge of the extension that it's being used in. The parameters will only actually be loaded once as they will be held in a static variable within the PluginHelper and and only loaded from the database if required May 2, 2023 at 15:11

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.