2

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?

2

One option is to create settings using Setting Reference

How I would do through extension is

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

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? – ErikH - CiviCooP Dec 15 '18 at 18:24
  • 1
    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. – Jon G - Megaphone Tech Dec 15 '18 at 20:05
1

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.

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.