1

In a system plugin, I am using the onAfterRoute event to change a single parameter from a 3rd party component before being loaded.

public function onAfterRoute()
{
    if ('com_abc' == JFactory::getApplication()->input->get('option'))
    {       
        $params = JComponentHelper::getParams('com_abc');
        $params->set('param_to_change', 'new_value');
    }
}

Although it is working I would like to know if this is the right way to achieve it or if there is a better method.

5
  • Do you mean with using a plugin or you do not want to use a plugin for this but you have another extension and within that you want to change a param value of another component?
    – Zollie
    Feb 18, 2019 at 13:21
  • @Zollie It must be done from a plugin, and within it, I need to change a parameter value of a component. Thanks.
    – n.h.
    Feb 18, 2019 at 13:32
  • I understand that you need to change the parameter of a component, but the question is that do you need to change it dinamically, on the fly always and based on what?, or you need to change it permanently from your ‘any extension’ based on the params of your other extension (not from a system plugin). So your question is a bit unclear on what is your situation and what is the purpose of changing a component parameter. Thus the “better method” has no real meaning in your question. Is there a better method for what?
    – Zollie
    Feb 20, 2019 at 6:50
  • So, parameters are in registry object when the component is called, or parameters are in database. Which one you want to change and based on what and what is the purpose?
    – Zollie
    Feb 20, 2019 at 8:03
  • @Zollie Thank you for your comments. I need to change dynamically based on my plugin parameter. Component parameters are in registry object and I don't need to save it to the database, just change it before component loading. Thank you.
    – n.h.
    Feb 20, 2019 at 9:44

1 Answer 1

0

You can change the params of a component with an added plugin as you did. I would only change the sequence of the conditional statement of yours like:

if (JFactory::getApplication()->input->get('option') == 'com_abc') {...}

That's not that relevant change, it just looks a bit more logical to me. And if you want to be a little more precise/critical then the $app as a property is already loaded with the system plugin, so you can simply use it and you get the same result:

if ($this->app->input->get('option') == 'com_abc') { ... }

But what you would really like to do (probably) is changing a third party component's params in your plugin or other component, without creating/using another system plugin, (and you do not have to change the value on the fly) if I understood you correctly. This is how you could formulate this:

On the top of your plugin file use two newer Joomla classes:

use \Joomla\CMS\Component\ComponentHelper;
use \Joomla\CMS\Table\Table;

And you can use the following code to get, change, set and save any component's params:

// getting the component params registry object
$comp_params = ComponentHelper::getParams('com_content');
$comp_params->set('my_new_value', (string) 42);
$comp_params->set('show_title', (string) 0);
// var_dump($comp_params); // you can check the values

// saving params to database
$component_id = ComponentHelper::getComponent('com_content')->id;
$table = Table::getInstance('extension');
$table->load($component_id);
$table->bind(array('params' => $comp_params->toString()));

// check for error
if (!$table->check()) {
    // echo $table->getError();
    return false;
}
// saving to database
if (!$table->store()) {
    // echo $table->getError();
    return false;
}

Nothing new in this, I just refreshed that a bit. I hope that with the above you can get what you are looking for.

Your Answer

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

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