1

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?

  • 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? – Brad Bell Nov 29 '18 at 2:45
1

Solution

In my src/template/index file I added this to make the form work

{% extends "_layouts/cp" %}
{% import "_includes/forms" as forms %}

<form action="" method="post">
{{ 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 }}

<input type="submit" class="submit btn" value="{{ 'Save'|t }}">

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.

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.