As I understand it, the current values of a component configuration are stored as JSON in the params field of the #__extensions database table, in the entry that matches the component name as element. Presumably the recommended way to obtain the current configuration is:

$params = ComponentHelper::getParams($element)

(although the user side application object has a method for obtaining the configuration without specifying the element, this seems not to exist on the admin side).

The configuration can be changed in software using:

$params->set($name, $value);

The issue then is how to store the new configuration. Ideally one might use a method in Joomla, but I cannot find a suitable one. Does one exist?

Assuming the storing has to be done by a database update, I find (by experiment) that the $params object converted to string cannot be used in a SQL statement because of problems of character escaping. Although it makes legal SQL, the result is that Joomla can no longer handle the configuration. In order to get storage to work, I have derived the following:

    $sql = "UPDATE #__extensions SET params = '%s' WHERE type = 'component' AND element = 'com_example'";
    $json = str_replace('\\\\"', '\\\\\\"', addcslashes($this->params, '"'));
    $sql = sprintf($sql, $json);

This works for the fairly complex configuration I am using. But my question is, does it work for all possible configurations? And is there an easier way?

  • 1
    It is not recommended to roll your own db-escaping technique. Please use Joomla's query building helper functions and associated escaping methods. – mickmackusa May 1 at 9:07
  • Yes thanks, of course, I should have used the database escape. I don't like the query builder though. And I'm still surprised there appears to be no Joomla method available for doing this. – mbrampton May 1 at 9:32

I suggest you take a look at the LibraryHelper class. Looking at the source for this, it gives access to the extensions table and contains the saveParams method.

Class reference is: https://api.joomla.org/cms-3/classes/Joomla.CMS.Helper.LibraryHelper.html

Source code: libraries/src/Helper/LibraryHelper.php

  • Thanks very much for making a suggestion. Sadly, I don't think it will work. The code for the saveParams method includes SQL with a WHERE that requires the extension type to be a library. One might have thought that the ComponentHelper class would have a saveParams method, but it seems not to. – mbrampton May 3 at 12:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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