Answers.Body: <p>You'll want to start with the <a href="http://wiki.civicrm.org/confluence/display/CRMDOC/Create+a+Module+Extension#CreateaModuleExtension-Addabasicwebform" rel="noreferrer">civix generate:form</a> command. Alter the generated code to have your form class extend <code>CRM_Admin_Form_Setting</code> instead of <code>CRM_Core_Form</code> and then follow the example of other such forms by declaring the settings that form is supposed to manage.</p>
  
  <p>You may also wish to insert a link to this form into the navigation menu.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-06-28T07:45:09.267Z
Answers.Text: I have added the settings using a normal form, what are the major differences?
Answers.UserDisplayName: Siddharth Gupta
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-06-28T13:51:31.190Z
Answers.Text: Using `CRM_Admin_Form_Setting` as a base class will require less code - it will automatically add your setting fields to the form, and automatically store them upon form submission.
Answers.UserDisplayName: Coleman
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-06-28T01:38:57.710Z
Answers.LastActivityDate: 2015-06-28T01:38:57.710Z
Answers.UserDisplayName: Coleman
Answers.Body: <p>This took me way longer than I anticipated, so I thought I'd pen a fuller answer for the most minimal settings form. It assumes you're building a modern extension, generated with <a href="https://github.com/totten/civix/" rel="nofollow noreferrer">civix</a>.</p>
  <h2>Step 1: define your settings</h2>
  <p>Create a <strong>settings</strong> dir in your extension's base directory, and within that create a <code>whateveryouwant.setting.php</code>. Gotcha 1 is that while the directory is called setting<strong>s</strong> this file must be use the singlular &quot;setting&quot; or it won't get picked up. But, yes, it can have multiple settings in it.</p>
  <p>That file contains a <code>return [ 'favourite_fruit' =&gt; [...], 'your_other_setting' =&gt; [...], ... ];</code> statement - see the <a href="https://docs.civicrm.org/dev/en/latest/framework/setting/#settings-definition" rel="nofollow noreferrer">docs</a></p>
  <p>Gotcha 2: these <code>*.setting.php</code> files seem to be only sourced as <em>part of</em> a cache clear. i.e. I learnt that using the &quot;Flush Caches&quot; button (or running via <code>cv api system.flush</code>) not only flushes the caches, but also recreates some of them. This is different to Drupal, for instance.</p>
  <h2>Step 2: create a form for it</h2>
  <p>As <a href="https://civicrm.stackexchange.com/a/3580/35">Coleman suggests</a>:</p>
  <pre><code>civix generate:form Settings civicrm/path/to/your/form
  </code></pre>
  <p>Note that the use of <code>Settings</code> as the form class name is up to you.</p>
  <p>Then edit the <code>CRM/YourMod/Form/Settings.php</code> file. You can strip this right down to just</p>
  <pre><code>class CRM_YourMod_Form_Settings extends CRM_Admin_Form_Setting
  {
     protected $_settings = [
       'favourite_fruit' =&gt; CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME,
     ];
     
     public function buildQuickForm() {
       parent::buildQuickForm();
       $this-&gt;assign('elementNames', array_keys($this-&gt;_settings));
     }
  }
  </code></pre>
  <p>The <code>$_settings</code> array is keyed by the names you define in your <code>*.setting.php</code>. In the <code>buildQuickForm()</code> function we just call the one we inherited, but then assign all our settings to the <code>elementNames</code> array, because the default form created by <code>civix</code> loops this array to output the elements; if you don't set this your form is empty.</p>
  <p>You can then alter it as you need to, but thought it would be useful to have this minimal example posted, hope it helps a googler (or a duckduckgo-er).</p>
  <p>For completeness' sake (as I use this as a reference myself!), in code you can access settings with:</p>
  <pre><code>$fruit = Civi::settings()-&gt;get('favourite_fruit');
  </code></pre>
  <p>And, should you need to</p>
  <pre><code>$fruit = Civi::settings()-&gt;set('favourite_fruit', 'lychee');
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-07-12T15:20:29.217Z
Answers.Text: Thanks for posting this detailed answered. I really appreciate it.
Answers.UserDisplayName: Sleewok
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-07-12T19:49:34.430Z
Answers.Text: I'm having issues with the  protected $_settings = [
       'yourmod_your_setting_name' => CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME,
     ]; This is not retrieving my setting information. Any ideas?
Answers.UserDisplayName: Sleewok
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-07-12T20:04:07.387Z
Answers.Text: Just to clarify. SYSTEM_PREFERENCES_NAME is set to a constant value, this results in my settings value being set to ""CiviCRM Preferences""
Answers.UserDisplayName: Sleewok
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-07-16T08:23:00.373Z
Answers.Text: That's the group name for the setting. The default form values should be loaded from your database by `CRM_Admin_Form_Setting::setDefaultValues()` which used to use the API to call `setting`.`getvalue` with the group set to the `SYSTEM_PREFERENCES_NAME` and the `name` set to your `$_settings` key. As of writing that code now uses a [different internal process](https://github.com/civicrm/civicrm-core/blob/master/CRM/Admin/Form/Setting.php#L46) to do the same thing. Are you sure you're extending `CRM_Admin_Form_Setting`?
Answers.UserDisplayName: artfulrobot
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-07-18T15:42:17.910Z
Answers.Text: Yes, I am extending CRM_Admin_Form_Setting. I see now that the custom setting is retrieved and the value is stored in _defaults variable. However, there are no fields on the form (the elements are not being populated). All I see are the Save/Cancel buttons.
Answers.UserDisplayName: Sleewok
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-07-18T15:48:10.213Z
Answers.Text: I am calling "CRM_Admin_Form_Setting::setDefaultValues();" within the buildQuickForm() function. Should I be calling the setDefaultValues() from somewhere else?
Answers.UserDisplayName: Sleewok
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-07-19T07:56:48.363Z
Answers.Text: And you have a `something.setting.php` file defining the fields? [example](https://github.com/artfulrobot/uk.artfulrobot.civicrm.autogroup/blob/master/settings/autogroup.setting.php)
Answers.UserDisplayName: artfulrobot
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-07-19T18:07:39.647Z
Answers.Text: Yes, the fields are defined. However, looking at your example I was missing the critical field of quick_form_type and all that follows it. Thank you so much for posting this example! That was the key!
Answers.UserDisplayName: Sleewok
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-07-20T08:06:22.153Z
Answers.Text: @Sleewok fab, well done.
Answers.UserDisplayName: artfulrobot
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2017-11-30T16:20:20.620Z
Answers.LastActivityDate: 2021-05-07T15:06:33.130Z
Answers.LastEditDate: 2021-05-07T15:06:33.130Z
Answers.UserDisplayName: artfulrobot
Answers.Body: <p>If your work is targeting versions 4.7+, you may not need to create a form like Coleman suggests.  Check out the work done to overhaul the CiviCRM settings administration - it's referred to in <a href="https://civicrm.org/blogs/dave-greenberg/road-map-and-release-schedule-47" rel="nofollow">this post</a> as "Administer Settings improvements", and a team developed it during the Colorado sprint.  You may want to ask a core team member the status of that work.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-05-24T07:01:20.733Z
Answers.Text: The link is dead.
Answers.UserDisplayName: Dilip Raj Baral
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-05-24T23:15:00.223Z
Answers.Text: @DilipRajBaral That's fine - this work is also dead and unfinished.  There was some recent discussion on finishing it, but it's not going anywhere at the moment.
Answers.UserDisplayName: Jon G - Megaphone Tech
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-06-28T04:06:20.093Z
Answers.LastActivityDate: 2015-06-28T04:06:20.093Z
Answers.UserDisplayName: Jon G - Megaphone Tech
Answers.Body: <p>I use <code>Civi::settings()-&gt;set</code> and <code>Civi::settings()-&gt;get</code> to create and retrieve CiviCRM settings.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-12-13T16:06:19.267Z
Answers.LastActivityDate: 2018-12-13T16:06:19.267Z
Answers.UserDisplayName: ErikH - CiviCooP
Body: <p>I am trying to develop a module extension which needs to store some settings, I went through the documentation and found <a href="https://github.com/eileenmcnaughton/org.civicrm.multisite/blob/master/settings/Multisite.setting.php" rel="noreferrer">this</a> to be the way to create settings. </p>
  
  <p>Now when I put default as NULL the setting is not reflected in the table, if I put any value as default the setting is added to <code>civicrm_setting</code> table.</p>
  
  <p>Also, though the settings have option to specify attributes for admin form, I couldn't figure out a way to present a new settings form for my module.</p>
  
  <p>Is there anything else which I need to do apart from adding the setting folder and the following code to extension.php</p>
  
  <pre><code>function multisite_civicrm_alterSettingsFolders(&amp;$metaDataFolders = NULL){
       _multisite_civix_civicrm_alterSettingsFolders($metaDataFolders);
  }
  </code></pre>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2015-06-23T00:45:16.750Z
Comments.Text: Hi Siddharth! I'm not sure whether you're asking how to add new fields to an existing form, or to add a new settings page to the admin menu. Can you rephrase your question for clarity? Perhaps some existing extensions might provide an example for what you're after?
Comments.UserDisplayName: Chris Burgess
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2015-06-23T07:17:08.983Z
Comments.Text: I am looking for adding a new settings page in the admin menu. Just like there is for CiviEvent, etc.
Comments.UserDisplayName: Siddharth Gupta
Tags: <extension><gsoc><settings>
Title: How do I add a new settings page for my extension?
ThreadText: # How do I add a new settings page for my extension?
  
  (Asked by: username_0 on 2015-06-22)
  
  I am trying to develop a module extension which needs to store some settings, I went through the documentation and found [this](https://github.com/eileenmcnaughton/org.civicrm.multisite/blob/master/settings/Multisite.setting.php) to be the way to create settings.
  
  Now when I put default as NULL the setting is not reflected in the table, if I put any value as default the setting is added to `civicrm_setting` table.
  
  Also, though the settings have option to specify attributes for admin form, I couldn't figure out a way to present a new settings form for my module.
  
  Is there anything else which I need to do apart from adding the setting folder and the following code to extension.php
  
  ```
  function multisite_civicrm_alterSettingsFolders(&$metaDataFolders = NULL){
       _multisite_civix_civicrm_alterSettingsFolders($metaDataFolders);
  }
  
  ```
  
  **Tags:** extension, gsoc, settings
  
  > **Comments:**
  > **username_1**: Hi username_0! I'm not sure whether you're asking how to add new fields to an existing form, or to add a new settings page to the admin menu. Can you rephrase your question for clarity? Perhaps some existing extensions might provide an example for what you're after?  
  > **username_0**: I am looking for adding a new settings page in the admin menu. Just like there is for CiviEvent, etc.  
  
  ---
  
  ## 4 answers
  
  ### Answer by username_2 on 2015-06-28. Score: 7 | Accepted answer
  
  You'll want to start with the [civix generate:form](http://wiki.civicrm.org/confluence/display/CRMDOC/Create+a+Module+Extension#CreateaModuleExtension-Addabasicwebform) command. Alter the generated code to have your form class extend `CRM_Admin_Form_Setting` instead of `CRM_Core_Form` and then follow the example of other such forms by declaring the settings that form is supposed to manage.
  
  You may also wish to insert a link to this form into the navigation menu.
  
  > **Comments:**
  > **username_0**: I have added the settings using a normal form, what are the major differences?  
  > **username_2**: Using `CRM_Admin_Form_Setting` as a base class will require less code - it will automatically add your setting fields to the form, and automatically store them upon form submission.  
  
  ---
  
  ### Answer by username_3 on 2017-11-30. Score: 3
  
  This took me way longer than I anticipated, so I thought I'd pen a fuller answer for the most minimal settings form. It assumes you're building a modern extension, generated with [civix](https://github.com/totten/civix/).
  
  ## Step 1: define your settings
  
  Create a **settings** dir in your extension's base directory, and within that create a `whateveryouwant.setting.php`. Gotcha 1 is that while the directory is called setting**s** this file must be use the singlular "setting" or it won't get picked up. But, yes, it can have multiple settings in it.
  
  That file contains a `return [ 'favourite_fruit' => [...], 'your_other_setting' => [...], ... ];` statement - see the [docs](https://docs.civicrm.org/dev/en/latest/framework/setting/#settings-definition)
  
  Gotcha 2: these `*.setting.php` files seem to be only sourced as *part of* a cache clear. i.e. I learnt that using the "Flush Caches" button (or running via `cv api system.flush`) not only flushes the caches, but also recreates some of them. This is different to Drupal, for instance.
  
  ## Step 2: create a form for it
  
  As [username_2 suggests](https://civicrm.stackexchange.com/a/3580/35):
  
  ```
  civix generate:form Settings civicrm/path/to/your/form
  
  ```
  
  Note that the use of `Settings` as the form class name is up to you.
  
  Then edit the `CRM/YourMod/Form/Settings.php` file. You can strip this right down to just
  
  ```
  class CRM_YourMod_Form_Settings extends CRM_Admin_Form_Setting
  {
     protected $_settings = [
       'favourite_fruit' => CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME,
     ];
     
     public function buildQuickForm() {
       parent::buildQuickForm();
       $this->assign('elementNames', array_keys($this->_settings));
     }
  }
  
  ```
  
  The `$_settings` array is keyed by the names you define in your `*.setting.php`. In the `buildQuickForm()` function we just call the one we inherited, but then assign all our settings to the `elementNames` array, because the default form created by `civix` loops this array to output the elements; if you don't set this your form is empty.
  
  You can then alter it as you need to, but thought it would be useful to have this minimal example posted, hope it helps a googler (or a duckduckgo-er).
  
  For completeness' sake (as I use this as a reference myself!), in code you can access settings with:
  
  ```
  $fruit = Civi::settings()->get('favourite_fruit');
  
  ```
  
  And, should you need to
  
  ```
  $fruit = Civi::settings()->set('favourite_fruit', 'lychee');
  
  ```
  
  > **Comments:**
  > **username_4**: Thanks for posting this detailed answered. I really appreciate it.  
  > **username_4**: I'm having issues with the  protected $_settings = [      'yourmod_your_setting_name' => CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME,    ]; This is not retrieving my setting information. Any ideas?  
  > **username_4**: Just to clarify. SYSTEM_PREFERENCES_NAME is set to a constant value, this results in my settings value being set to ""CiviCRM Preferences""  
  > **username_3**: That's the group name for the setting. The default form values should be loaded from your database by `CRM_Admin_Form_Setting::setDefaultValues()` which used to use the API to call `setting`.`getvalue` with the group set to the `SYSTEM_PREFERENCES_NAME` and the `name` set to your `$_settings` key. As of writing that code now uses a [different internal process](https://github.com/civicrm/civicrm-core/blob/master/CRM/Admin/Form/Setting.php#L46) to do the same thing. Are you sure you're extending `CRM_Admin_Form_Setting`?  
  > **username_4**: Yes, I am extending CRM_Admin_Form_Setting. I see now that the custom setting is retrieved and the value is stored in _defaults variable. However, there are no fields on the form (the elements are not being populated). All I see are the Save/Cancel buttons.  
  > **username_4**: I am calling "CRM_Admin_Form_Setting::setDefaultValues();" within the buildQuickForm() function. Should I be calling the setDefaultValues() from somewhere else?  
  > **username_3**: And you have a `something.setting.php` file defining the fields? [example](https://github.com/username_3/uk.username_3.civicrm.autogroup/blob/master/settings/autogroup.setting.php)  
  > **username_4**: Yes, the fields are defined. However, looking at your example I was missing the critical field of quick_form_type and all that follows it. Thank you so much for posting this example! That was the key!  
  > **username_3**: @username_4 fab, well done.  
  
  ---
  
  ### Answer by username_5 on 2015-06-28. Score: 2
  
  If your work is targeting versions 4.7+, you may not need to create a form like username_2 suggests. Check out the work done to overhaul the CiviCRM settings administration - it's referred to in [this post](https://civicrm.org/blogs/dave-greenberg/road-map-and-release-schedule-47) as "Administer Settings improvements", and a team developed it during the Colorado sprint. You may want to ask a core team member the status of that work.
  
  > **Comments:**
  > **username_6**: The link is dead.  
  > **username_5**: @username_6 That's fine - this work is also dead and unfinished.  There was some recent discussion on finishing it, but it's not going anywhere at the moment.  
  
  ---
  
  ### Answer by username_7 on 2018-12-13. Score: 0
  
  I use `Civi::settings()->set` and `Civi::settings()->get` to create and retrieve CiviCRM settings.
  
  
  ---
  
  