<p><strong>Solution</strong></p>

<p>In my <code>src/template/index</code> file I added this to make the form work</p>

<pre><code>{% extends "_layouts/cp" %}
{% import "_includes/forms" as forms %}

&lt;form action="" method="post"&gt;
{{ csrfInput() }}

{{ forms.hidden({
    name: 'action',
    value: 'craft-terms-of-service/default/save-tos'
}) }}

{{ forms.hidden({
    name: 'tosCurrentVersion',
    value: now.timestamp
}) }}

{{ forms.lightswitchField({
    label: 'Enable TOS?'|t,
    id: 'tosEnabled',
    name: 'tosEnabled',
    on: craft.craftTermsOfService.get('tosEnabled'),
    first: true
}) }}

{{ forms.textField({
    label: 'Headline'|t('craft-terms-of-service'),
    instructions: 'Enter the headline of your TOS here.'|t('craft-terms-of-service'),
    id: 'tosHeadline',
    name: 'tosHeadline',
    value: craft.craftTermsOfService.get('tosHeadline')
}) }}

{{ craft.craftTermsOfService.redactorField({
    label: 'Terms of Service'|t('craft-terms-of-service'),
    instructions: 'Enter the TOS every user must accept before using the site below.'|t('craft-terms-of-service'),
    name: 'tosBody',
    rows: 4,
    value: craft.craftTermsOfService.get('tosBody')|raw
})|raw }}

&lt;input type="submit" class="submit btn" value="{{ 'Save'|t }}"&gt;
</code></pre>

<p></p>

<p>Using the <code>actions/craft-terms-of-service/default/save-tos</code> controller I took the data from the request and updated the data in the <code>plugins</code> table. Also in the form in my <code>index</code> template I used the <code>craft.craftTermsOfService.get</code> variable to fetch the settings from the <code>plugins</code> table and give each form element its appropriate value.</p>

<p>For details, see the <a href="https://github.com/stianmandarin/crafttermsofservice/blob/master/src/controllers/DefaultController.php" rel="nofollow noreferrer">controller on github</a> and the <a href="https://github.com/stianmandarin/crafttermsofservice/blob/master/src/variables/CraftTermsOfServiceVariable.php" rel="nofollow noreferrer">variable</a> on github. Note this is the first time I make a plugin, so it's probably a mess, sorry about that.</p>

CC BY-SA 4.0
2018-11-30T00:28:26.863Z
2018-11-30T00:28:26.863Z
Stian Karlsen
<p>I've setup a plugin like so <code>http://domain/admin/plugin-handle</code> (cp section) and a settings page here <code>http://domain/admin/settings/plugins/plugin-handle</code></p>

<p>I can edit my plugin settings under the latter URL, however I would like non admins to be able to do so as well, but under <code>http://domain/admin/plugin-handle</code></p>

<p>How can I go about this? Adding e.g.</p>

<pre><code>{{ forms.textField({
    label: 'Headline'|t('craft-terms-of-service'),
    instructions: 'Enter the headline of your TOS here.'|t('craft-terms-of-service'),
    id: 'tosHeadline',
    name: 'tosHeadline',
    value: settings['tosHeadline']})
}}
</code></pre>

<p>to the <code>http://domain/admin/plugin-handle</code> template (<code>src/templates/index.twig</code>) (cp section) non-admins have access to, I get <code>Variable "settings" does not exist.</code> I can't figure out how to make it accessible/exist.</p>

<p>Thanks in advance</p>

<p><strong>Edit</strong></p>

<p>Figured out how to add fields to the cp section through the "Getting Started with Craft Plugin Development" course on craftquest.io, however the way I'm saving the settings to the db is rather crude. I'm just doing <code>Craft::$app-&gt;db-&gt;createCommand(....</code> to update the <code>plugins</code> table. It works, but I'm sure there are much more elegant ways of doing it?</p>

CC BY-SA 4.0
2018-11-29T02:45:04.497Z
Glad you figured it out, Stian... would you mind adding your solution to the original question as an answer, then post your new settings saving question as a new one?
Brad Bell
<plugin-development><craft3>
Add settings to plugins cp section instead of settings page
# Add settings to plugins cp section instead of settings page

(Asked by: username_0 on 2018-11-28)

I've setup a plugin like so http://domain/admin/plugin-handle (cp section) and a settings page here http://domain/admin/settings/plugins/plugin-handle
I can edit my plugin settings under the latter URL, however I would like non admins to be able to do so as well, but under http://domain/admin/plugin-handle
How can I go about this? Adding e.g.
{{ forms.textField({
    label: 'Headline'|t('craft-terms-of-service'),
    instructions: 'Enter the headline of your TOS here.'|t('craft-terms-of-service'),
    id: 'tosHeadline',
    name: 'tosHeadline',
    value: settings['tosHeadline']})
}}

to the http://domain/admin/plugin-handle template (src/templates/index.twig) (cp section) non-admins have access to, I get Variable "settings" does not exist. I can't figure out how to make it accessible/exist.
Thanks in advance
Edit
Figured out how to add fields to the cp section through the "Getting Started with Craft Plugin Development" course on craftquest.io, however the way I'm saving the settings to the db is rather crude. I'm just doing Craft::$app->db->createCommand(.... to update the plugins table. It works, but I'm sure there are much more elegant ways of doing it?

**Tags:** plugin-development, craft3

> **Comments:**
> **username_1**: Glad you figured it out, Stian... would you mind adding your solution to the original question as an answer, then post your new settings saving question as a new one?  

---

## 1 answers

### Answer by username_0 on 2018-11-30. Score: 1 | Accepted answer

Solution
In my src/template/index file I added this to make the form work
{% extends "_layouts/cp" %}
{% import "_includes/forms" as forms %}


{{ csrfInput() }}

{{ forms.hidden({
    name: 'action',
    value: 'craft-terms-of-service/default/save-tos'
}) }}

{{ forms.hidden({
    name: 'tosCurrentVersion',
    value: now.timestamp
}) }}

{{ forms.lightswitchField({
    label: 'Enable TOS?'|t,
    id: 'tosEnabled',
    name: 'tosEnabled',
    on: craft.craftTermsOfService.get('tosEnabled'),
    first: true
}) }}

{{ forms.textField({
    label: 'Headline'|t('craft-terms-of-service'),
    instructions: 'Enter the headline of your TOS here.'|t('craft-terms-of-service'),
    id: 'tosHeadline',
    name: 'tosHeadline',
    value: craft.craftTermsOfService.get('tosHeadline')
}) }}

{{ craft.craftTermsOfService.redactorField({
    label: 'Terms of Service'|t('craft-terms-of-service'),
    instructions: 'Enter the TOS every user must accept before using the site below.'|t('craft-terms-of-service'),
    name: 'tosBody',
    rows: 4,
    value: craft.craftTermsOfService.get('tosBody')|raw
})|raw }}




Using the actions/craft-terms-of-service/default/save-tos controller I took the data from the request and updated the data in the plugins table. Also in the form in my index template I used the craft.craftTermsOfService.get variable to fetch the settings from the plugins table and give each form element its appropriate value.
For details, see the controller on github and the variable on github. Note this is the first time I make a plugin, so it's probably a mess, sorry about that.


---

