Answers.Body: <p>Thanks @Jon G, that makes sense :-) So summarizing the recommended way is:</p>
  
  <ul>
  <li>Register metadata for setting using <code>hook_civicrm_alterSettingsMetaData</code> or create <code>myfile.setting.php</code> in my extension</li>
  <li>Use either Civi::settings()->set to set the value of the setting and Civi::settings()->get to retrieve the value OR the Setting API.</li>
  </ul>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-12-13T07:40:44.930Z
Answers.Text: 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](https://github.com/artfulrobot/chasse/blob/master/chasse.php#L216)) 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.
Answers.UserDisplayName: artfulrobot
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-12-17T09:26:30.700Z
Answers.LastActivityDate: 2018-12-17T09:26:30.700Z
Answers.UserDisplayName: ErikH - CiviCooP
Answers.Body: <p>One option is to create settings using <a href="https://docs.civicrm.org/dev/en/latest/framework/setting/" rel="nofollow noreferrer">Setting Reference</a></p>
  
  <p>How I would do through extension is</p>
  
  <ul>
  <li><p>Register metadata for setting using <a href="https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_alterSettingsMetaData/" rel="nofollow noreferrer">hook_civicrm_alterSettingsMetaData</a></p></li>
  <li><p>To set the setting using civicrm api 
  code:</p>
  
  <p>civicrm_api3('Setting', 'create', [
    'setting_name_goes_here' => 123,
  ]);</p></li>
  <li><p>To get the setting using civicrm api</p>
  
  <p>civicrm_api3('Setting', 'getvalue', [
        'name' =>'setting_name_goes_here',
      ]);</p></li>
  <li><p>If you want to add this setting on one of the Admin config form then below is a good example on how you can do it.(New version of CiviCRM provides more flexibility to add settings to UI by appending settings in $form->_settings in preprocess hook)</p>
  
  <ul>
  <li><p><a href="https://github.com/JMAConsulting/biz.jmaconsulting.easybatch/blob/master/easybatch.php#L140" rel="nofollow noreferrer">https://github.com/JMAConsulting/biz.jmaconsulting.easybatch/blob/master/easybatch.php#L140</a></p></li>
  <li><p><a href="https://github.com/JMAConsulting/biz.jmaconsulting.easybatch/blob/master/easybatch.php#L116" rel="nofollow noreferrer">https://github.com/JMAConsulting/biz.jmaconsulting.easybatch/blob/master/easybatch.php#L116</a></p></li>
  <li><p><a href="https://github.com/JMAConsulting/biz.jmaconsulting.easybatch/blob/master/easybatch.php#L304" rel="nofollow noreferrer">https://github.com/JMAConsulting/biz.jmaconsulting.easybatch/blob/master/easybatch.php#L304</a></p></li>
  <li><p><a href="https://github.com/JMAConsulting/biz.jmaconsulting.easybatch/blob/master/templates/CRM/EasyBatch/Form/Admin.tpl" rel="nofollow noreferrer">https://github.com/JMAConsulting/biz.jmaconsulting.easybatch/blob/master/templates/CRM/EasyBatch/Form/Admin.tpl</a></p></li>
  </ul></li>
  </ul>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-12-15T18:23:51.940Z
Answers.Text: I know this is an alternative, but why would I go through all that trouble if Civi::settings->set suffices to create my setting?
Answers.UserDisplayName: ErikH - CiviCooP
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-12-13T22:46:33.280Z
Answers.LastActivityDate: 2018-12-13T22:46:33.280Z
Answers.UserDisplayName: Pradeep Nayak
Answers.Body: <p>Instead of <code>hook_civicrm_alterSettingsMetaData</code> you can also create a <code>myfile.setting.php</code> file in your extension.</p>
  
  <p>See also issue <a href="https://lab.civicrm.org/dev/core/issues/495" rel="nofollow noreferrer">core#495</a> for recent work that allows you to add settings to the UI based SOLELY on metadata - very exciting!</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-12-15T18:24:52.797Z
Answers.Text: 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?
Answers.UserDisplayName: ErikH - CiviCooP
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-12-15T20:05:33.987Z
Answers.Text: 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.
Answers.UserDisplayName: Jon G - Megaphone Tech
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-12-14T18:14:58.330Z
Answers.LastActivityDate: 2018-12-14T18:14:58.330Z
Answers.UserDisplayName: Jon G - Megaphone Tech
Body: <p>In this post on StackExchange <a href="https://civicrm.stackexchange.com/questions/3458/how-do-i-add-a-new-settings-page-for-my-extension">How do I add a new settings page for my extension?</a> 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.</p>
  
  <p>I always use Civi::settings()->set and Civi::settings()->get. What is the recommended way?</p>
  
Tags: <settings><coding-standards>
Title: Should I use Civi::settings()->get and set or
ThreadText: # Should I use Civi::settings()->get and set or
  
  (Asked by: username_0 on 2018-12-13)
  
  In this post on StackExchange [How do I add a new settings page for my extension?](https://civicrm.stackexchange.com/questions/3458/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?
  
  **Tags:** settings, coding-standards
  
  
  ---
  
  ## 3 answers
  
  ### Answer by username_0 on 2018-12-17. Score: 2
  
  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.
  
  > **Comments:**
  > **username_1**: 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](https://github.com/username_1/chasse/blob/master/chasse.php#L216)) 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.  
  
  ---
  
  ### Answer by username_2 on 2018-12-13. Score: 1
  
  One option is to create settings using [Setting Reference](https://docs.civicrm.org/dev/en/latest/framework/setting/)
  
  How I would do through extension is
  
  * Register metadata for setting using [hook\_civicrm\_alterSettingsMetaData](https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_alterSettingsMetaData/)
  * To set the setting using civicrm api
    code:
  
    civicrm\_api3('Setting', 'create', [
    'setting\_name\_goes\_here' => 123,
    ]);
  * To get the setting using civicrm api
  
    civicrm\_api3('Setting', 'getvalue', [
    'name' =>'setting\_name\_goes\_here',
    ]);
  * If you want to add this setting on one of the Admin config form then below is a good example on how you can do it.(New version of CiviCRM provides more flexibility to add settings to UI by appending settings in $form->\_settings in preprocess hook)
  
    + <https://github.com/JMAConsulting/biz.jmaconsulting.easybatch/blob/master/easybatch.php#L140>
    + <https://github.com/JMAConsulting/biz.jmaconsulting.easybatch/blob/master/easybatch.php#L116>
    + <https://github.com/JMAConsulting/biz.jmaconsulting.easybatch/blob/master/easybatch.php#L304>
    + <https://github.com/JMAConsulting/biz.jmaconsulting.easybatch/blob/master/templates/CRM/EasyBatch/Form/Admin.tpl>
  
  > **Comments:**
  > **username_0**: I know this is an alternative, but why would I go through all that trouble if Civi::settings->set suffices to create my setting?  
  
  ---
  
  ### Answer by username_3 on 2018-12-14. Score: 1
  
  Instead of `hook_civicrm_alterSettingsMetaData` you can also create a `myfile.setting.php` file in your extension.
  
  See also issue [core#495](https://lab.civicrm.org/dev/core/issues/495) for recent work that allows you to add settings to the UI based SOLELY on metadata - very exciting!
  
  > **Comments:**
  > **username_0**: 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?  
  > **username_3**: 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.  
  
  ---
  
  