1

I'm building a CiviCRM extension, which would also have an admin section with UI for setting various configuration specific to the extension. I'm looking for a recommended approach for storing the configuration in the database.

One way is to create a new table in the database specifically for this purpose, but this seems like an overkill if there are only a couple of options to be saved.

Another way could be to use the civicrm_setting table, which kind of makes sense at first, but I'm not sure if this table is meant to be used for this purpose.

Any advice would be appreciated.

robinmitra
  • 604
  • 8
  • 16

2 Answers2

3

Yes, you can and should definitively use civicrm_setting.

civicrm_setting has a column group_name that should contain a unique identifier for your extension. I usually put the full name of the extension, like org.example.extension but it could be any string, and in core they use label name (e.g., Preference settings).

To interact with those settings, you can do the following :

// save the setting
CRM_Core_BAO_Setting::setItem($value, 'My group name', 'my_setting_name');

// get the setting
$setting = CRM_Core_BAO_Setting::getItem('My group name', 'my_setting_name');

// get all the setting for you extension
$settings = CRM_Core_BAO_Setting::getItem('My group name');

There seems to be an API for Setting but it doesn't seem to work well in CiviCRM 4.4.x. Don't know if it is better in CiviCRM 4.5.

robinmitra
  • 604
  • 8
  • 16
samuelsov
  • 395
  • 2
  • 10
  • 1
    Great answer! I'm thinking along the lines of having the ```group_name``` column as ```Extension```, and ```name``` column as the extension's name, followed by a colon and the key for the setting like ```org.example.extension:show_date```. This is how CiviCRM stores extension versions (i.e., ```org.example.extension:version```). – robinmitra Nov 11 '14 at 12:19
  • there is a documentation on best practice now which help you have a nice admin UI on top of your declared settings : https://docs.civicrm.org/dev/en/latest/framework/setting/#creating-a-new-setting-in-an-extension – samuelsov Nov 17 '17 at 16:21
0

What you could also do (our current practice) is store your configuration logic in a special class using the singleton pattern (as CiviCRM does itslef). If you want to see an example check this: https://github.com/CiviCooP/no.maf.oppgavexml/blob/master/CRM/Oppgavexml/Config.php