Skip to content

Commit

Permalink
Allow config file overrides for options, display override warnings.
Browse files Browse the repository at this point in the history
  • Loading branch information
Tim Kelty committed Nov 14, 2015
1 parent b8cd5ac commit 1ee7e48
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 12 deletions.
22 changes: 20 additions & 2 deletions usermanual/UserManualPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ protected function defineSettings()
{
return [
'pluginNameOverride' => AttributeType::String,
'templateOverride' => AttributeType::String,
'section' => [AttributeType::Mixed, 'default' => ''],
'templateOverride' => AttributeType::Template,
'section' => AttributeType::Number,
];
}

Expand Down Expand Up @@ -83,4 +83,22 @@ public function onAfterInstall()
{
craft()->request->redirect(UrlHelper::getCpUrl('settings/plugins/usermanual/'));
}

public function getSettings()
{
$settings = parent::getSettings();
foreach ($settings as $name => $value) {
$configValue = craft()->config->get($name, 'usermanual');
$settings->$name = is_null($configValue) ? $value : $configValue;
}

// Allow handles from config
if (!is_numeric($settings->section)) {
$section = craft()->sections->getSectionByHandle('homepage');
if ($section) {
$settings->section = $section->id;
}
}
return $settings;
}
}
6 changes: 6 additions & 0 deletions usermanual/config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php
return [
'pluginNameOverride' => null,
'templateOverride' => null,
'section' => null,
];
43 changes: 33 additions & 10 deletions usermanual/templates/settings.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
{% import "_includes/forms" as forms %}

{% macro configWarning(setting, file) -%}
{{ "This is being overridden by the {setting} config setting."|t({
setting: '<a href="https://github.com/hillholliday/Craft-User-Manual/config-settings#'~setting~'" target="_blank">'~setting~'</a>'
})|raw }}
{%- endmacro %}

{% from _self import configWarning %}

{% set configOverride = craft.config.get('pluginNameOverride', 'usermanual') is not null %}
{{ forms.textField({
label: "Plugin Name"|t,
id: 'pluginNameOverride',
Expand All @@ -8,28 +17,42 @@
value: settings.pluginNameOverride,
autofocus: true,
first: true,
errors: ''
errors: '',
warning: configOverride ? configWarning('pluginNameOverride'),
disabled: configOverride,
readonly: configOverride,
})}}

{{ forms.selectField({
label: "User Manual Section"|t,
id: 'section',
name: 'section',
instructions: 'Entries in this section must have associated urls.'|t,
value: settings.section,
options: options,
})}}
{% set configOverride = craft.config.get('section', 'usermanual') is not null %}
{% set inputMacro = configOverride ? 'textField' : 'selectField' %}
{% set opts = {
label: "User Manual Section"|t,
id: 'section',
name: 'section',
instructions: 'Entries in this section must have associated urls.'|t,
value: settings.section,
options: options,
warning: configOverride ? configWarning('pluginNameOverride'),
disabled: configOverride,
readonly: configOverride,
} %}
{{ configOverride ? forms.textField(opts) : forms.selectField(opts) }}

{% set instructions %}
For more control over the output, you may optionally override the default template.<br>
Path is relative to <code>{{ siteTemplatesPath }}</code>.
{% endset %}
{% set configOverride = craft.config.get('templateOverride', 'usermanual') is not null %}

{{ forms.textField({
label: "Template Override"|t,
id: 'templateOverride',
name: 'templateOverride',
placeholder: '_includes/userManualContent.twig',
instructions: instructions|raw,
value: settings.templateOverride,
errors: ''
errors: '',
warning: configOverride ? configWarning('pluginNameOverride'),
disabled: configOverride,
readonly: configOverride,
})}}

0 comments on commit 1ee7e48

Please sign in to comment.