From 2c321889e74ee65c7cec422ffe642eaa2574f60d Mon Sep 17 00:00:00 2001 From: Thomas Schulze Date: Fri, 17 Sep 2021 09:22:36 +0200 Subject: [PATCH] add default value --- CHANGELOG.md | 9 +++++++++ composer.json | 3 ++- src/fields/Buttons.php | 22 ++++++++++++++++++++-- src/translations/de/buttons.php | 3 +++ 4 files changed, 34 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2ae77c8..dce326c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,8 +1,16 @@ # Customizable Radio Buttons Field Changelog +## 1.0.3 - 2021-09-16 + +### Added + +- Add a simple way to set a default value to every field. +- Add some instructions to the field settings. + ## 1.0.2 - 2021-09-01 ### Fixed + - Add wrap and gap to the buttons (thanks to @emsuiko). - Fixed background color of icon. - Fixed a bug when two or more same configurations were used in one tab/ matrix block. @@ -10,6 +18,7 @@ ## 1.0.1 - 2019-09-15 ### Fixed + - Publishing images to cpresources. ## 1.0.0 - 2019-04-06 diff --git a/composer.json b/composer.json index f4bd65b..d9df303 100644 --- a/composer.json +++ b/composer.json @@ -1,7 +1,7 @@ { "name": "codemonauts/craft-customizable-radio-buttons-field", "description": "Craft CMS plugin to add a radio buttons field, customizable with CSS classes, icons, images and text.", - "version": "1.0.2", + "version": "1.0.3", "type": "craft-plugin", "keywords": [ "craft", @@ -36,6 +36,7 @@ "class": "codemonauts\\buttons\\Buttons", "name": "Customizable Radio Buttons Field", "description": "Radio buttons field, customizable with CSS classes, icons, images and text.", + "changelogUrl": "https://github.com/codemonauts/craft-customizable-radio-buttons-field/blob/master/CHANGELOG.md", "hasCpSection": false, "hasSettings": false } diff --git a/src/fields/Buttons.php b/src/fields/Buttons.php index 5c369df..351043a 100644 --- a/src/fields/Buttons.php +++ b/src/fields/Buttons.php @@ -29,6 +29,11 @@ class Buttons extends Field implements PreviewableFieldInterface */ public $configHandle = ''; + /** + * @var string Default value + */ + public $defaultValue = ''; + /** * @inheritdoc */ @@ -59,15 +64,28 @@ public function getSettingsHtml() ]; } - return Craft::$app->getView()->renderTemplateMacro('_includes/forms', 'selectField', [ + $html = Craft::$app->getView()->renderTemplateMacro('_includes/forms', 'selectField', [ [ 'label' => Craft::t('buttons', 'Button group'), + 'instructions' => Craft::t('buttons', 'The button group from the config file to use.'), 'id' => 'configHandle', 'name' => 'configHandle', 'options' => $options, 'value' => $this->configHandle, ], ]); + + $html .= Craft::$app->getView()->renderTemplateMacro('_includes/forms', 'textField', [ + [ + 'label' => Craft::t('buttons', 'Default value'), + 'instructions' => Craft::t('buttons', 'The default value of the field. If empty, no button will be preselected.'), + 'id' => 'defaultValue', + 'name' => 'defaultValue', + 'value' => $this->defaultValue, + ], + ]); + + return $html; } /** @@ -96,7 +114,7 @@ public function getInputHtml($value, ElementInterface $element = null): string return Craft::$app->getView()->renderTemplate('buttons/input', [ 'name' => $this->handle, - 'value' => $value, + 'value' => $value ?? $this->defaultValue, 'field' => $this, 'buttons' => $buttons, ]); diff --git a/src/translations/de/buttons.php b/src/translations/de/buttons.php index 2bc7b9b..aaa8493 100644 --- a/src/translations/de/buttons.php +++ b/src/translations/de/buttons.php @@ -3,4 +3,7 @@ return [ 'Customizable Radio Buttons' => 'Individuelle Radio-Buttons', 'Button group' => 'Schalterkonfiguration', + 'Default value' => 'Standardwert', + 'The button group from the config file to use.' => 'Die Button-Gruppe aus der Konfigurationsdatei die verwendet werden soll.', + 'The default value of the field. If empty, no button will be preselected.' => 'Der Standardwert des Feldes. Ist dieser leer, wird kein Button vorausgewählt.' ];