AnswerBun.com

From outside of a component how to change a parameter

Joomla Asked by n.h. on December 3, 2020

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.

One Answer

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 JoomlaCMSComponentComponentHelper;
use JoomlaCMSTableTable;

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.

Answered by Zollie on December 3, 2020

Add your own answers!

Related Questions

Disabling cache on specific part of page from above comment

2  Asked on September 14, 2020 by ruchika

 

Custom Category Field

1  Asked on August 18, 2020 by grant-g

     

How do I pass _JEXEC to a php file?

1  Asked on August 6, 2020 by firefighter

   

Ask a Question

Get help from others!

© 2023 AnswerBun.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP