user101289
user101289

Reputation: 10422

setting persistent plugin parameters in Joomla 3

I'm developing a Joomla 3.x plugin, and want to be able to change the plugin parameter set in the plugin's manifest file programmatically. I believe I need to use a JRegistry object, but I'm not sure about the syntax.

Here's the issue:

// token A is set in plugin params as defined in plugin's XML manifest
var_dump($this->params->get('token')); // prints token "A" as expected

// do some stuff to get a fresh access token, called token "B"
$tokenB = $function_to_get_fresh_token();

// set the new token
if ($tokenB) $this->params->set('token', $tokenB);

var_dump($this->params->get('apptoken')); // prints token "B" as expected

the problem is that on subsequent page reloads, the token reverts to tokenA rather than what I assumed would be the stored value of tokenB.

How do I store the tokenB value in the plugin's parameters in the database?

Upvotes: 1

Views: 2059

Answers (3)

John Linhart
John Linhart

Reputation: 1816

This is a working example of how to change plugin params from within the plugin (J! 3.4):

// Load plugin called 'plugin_name'
$table = new JTableExtension(JFactory::getDbo());
$table->load(array('element' => 'plugin_name'));

// Params can be changed like this
$this->params->set('new_param', 'new value'); // if you are doing change from a plugin
$table->set('params', $this->params->toString());

// Save the change
$table->store();

Note: If new params are added by plugin dynamically and the plugin is saved afterwards, these new params gets deleted. So one way to deal with it is to add those params as hidden fields to plugin's config XML.

Upvotes: 3

Elin
Elin

Reputation: 6755

This is just an outline, but something along these lines

$extensionTable = new  JtableExtension();
$pluginId = $extensionTable->find('element', 'my_plugin');
$pluginRow = $extensionTable->load($pluginId);

// Do the jregistry work that is needed
// do some stuff to get a fresh access token, called token "B"
$tokenB = $function_to_get_fresh_token();

// set the new token
if ($tokenB) $this->params->set('token', $tokenB);

// more stuff

$extensionTable->save($pluginRow);

Upvotes: 0

user101289
user101289

Reputation: 10422

I spent a lot of time googling and reading and found no real answer to this. Oddly enough this doesn't seem to have been provided for in Joomla. So here's what I ended up doing:

1) build a function to get your plugin ID, since it will change from one installation to another

private function getPlgId(){
    // stupid hack since there doesn't seem to be another way to get plugin id
    $db = JFactory::getDBO();
    $sql = 'SELECT `extension_id` FROM `#__extensions` WHERE `element` = "my_plugin" AND `folder` = "my_plugin_folder"'; // check the #__extensions table if you don't know your element / folder
    $db->setQuery($sql);
    if( !($plg = $db->loadObject()) ){
        return false;
    } else {
        return (int) $plg->extension_id;
    }
}

2) use the plugin id to load the table object:

$extension  = new JTableExtension($db);
$ext_id = $this->getPlgId(); 

// get the existing extension data
$extension->load($ext_id);

3) when you're ready to store the value, add it to the params, then store it:

$this->params->set('myvalue', $newvalue);
$extension->bind( array('params' => $this->params->toString()) );

// check and store 
if (!$extension->check()) {
    $this->setError($extension->getError());
    return false;
}
if (!$extension->store()) {
    $this->setError($extension->getError());
    return false;
}

If anyone knows a better way to do this please let me know!

Upvotes: 0

Related Questions