In this post on StackExchange How do I add a new settings page for my extension? the way to create settings in CiviCRM is stated as : Create a settings dir in your extension's base directory, and within that create a whateveryouwant.setting.php.

I always use Civi::settings()->set and Civi::settings()->get. What is the recommended way?


One option is to create settings using Setting Reference

How I would do through extension is

  • 1
    I know this is an alternative, but why would I go through all that trouble if Civi::settings->set suffices to create my setting? Dec 15 '18 at 18:23

Thanks @Jon G, that makes sense :-) So summarizing the recommended way is:

  • Register metadata for setting using hook_civicrm_alterSettingsMetaData or create myfile.setting.php in my extension
  • Use either Civi::settings()->set to set the value of the setting and Civi::settings()->get to retrieve the value OR the Setting API.
  • I'd +1 for using Civi::settings() methods. The API is a terrible pattern (security risk, unless you know about it and mitigate this - example) and if we can all stop using it then we might be able to improve it without breaking stuff. Also, the API includes lots of params that aren't(/don't seem to be) needed any more, like settings group. This too can lead you to mistakes like naming a setting "api_key" because you thought your settings group was a namespace; it's not. Dec 13 '20 at 7:40

Instead of hook_civicrm_alterSettingsMetaData you can also create a myfile.setting.php file in your extension.

See also issue core#495 for recent work that allows you to add settings to the UI based SOLELY on metadata - very exciting!

  • I know I can do this @JonG, can you tell me why I would need to create such a file and metadata? If I just include Civi::settings->set my setting is created? Dec 15 '18 at 18:24
  • 2
    If you have the metadata (either via the hook or settings.php) then a) it will appear in the API Explorer, b) you can put it on a settings page with no code. Dec 15 '18 at 20:05

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.