civicrm

Storing CiviCRM extension specific configuration in database


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.


Solution

  • 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.