Category: Joomla FAQ
Hits: 8158
Star InactiveStar InactiveStar InactiveStar InactiveStar Inactive

Image-missing.svgI needed to update a plugin settings upon an extension installation. Here is a sample of my code.

The problem was a little harder to solve, because of the nested objects in the params object.

The screenshot is hear for better understanding of my data structure.

// Get extension table class
$extensionTable = JTable::getInstance('extension');
// Find plugin id, in my case it was plg_ajax_ajaxhelpary
$pluginId = $extensionTable->find( array('element' => 'ajaxhelpary', 'type' => 'plugin') );
$extensionTable->load($pluginId); // Get joomla default object $params = new JRegistry; $params->loadString($extensionTable->params, 'JSON'); // Load my plugin params. $list_templates = json_decode($params->get('list_templates')); // list_templates is an object if array, so I need to encode it once more here
if (!empty($params->get('list_templates')) && $list_templates === NULL) { // The default joomla installation procedure doesn't store defaut params into the DB in the correct way
    $params_tmp = str_replace("'",'"',$params->get('list_templates'));
    $list_templates = json_decode($params_tmp);
} // Some my special code simply to illustrate and help to understand $update_ajaxhelpary = true; foreach ($list_templates->plg_name as $key=>$plg_name) { if ($plg_name == 'notifyarticlesubmit') { unset($list_templates->plg_name[$key]); unset($list_templates->plg_type[$key]); unset($list_templates->scope[$key]); unset($list_templates->function[$key]); } if ($plg_name == $this->plg_name) { $update_ajaxhelpary = false; } } foreach ($list_templates as $k=>$array) { $list_templates->$k = array_values($array); } if ($update_ajaxhelpary) { $list_templates->plg_type[] = $this->plg_type; $list_templates->plg_name[] = $this->plg_name; $list_templates->function[] = '_ajaxRun'; $list_templates->scope[] = 'both'; } // By here my object is ready to be saved $list_templates = json_encode($list_templates); // Prepare it to the database $params->set('list_templates',$list_templates); // Set to parameters $extensionTable->bind( array('params' => $params->toString()) ); // Bind to extension table // check and store if (!$extensionTable->check()) { $this->setError($extensionTable->getError()); //~ return false; } if (!$extensionTable->store()) { $this->setError($extensionTable->getError()); //~ return false; }
Found a typo? Please select it and press Ctrl + Enter.
FaLang translation system by Faboba