From 8a6ff4b3ccc9d52b2f8f6c35de4fbf187bd2280c Mon Sep 17 00:00:00 2001 From: Stefanie Gevaert Date: Mon, 23 Sep 2024 14:00:03 +0200 Subject: [PATCH] fix: #133 / #127 --- CHANGELOG.md | 5 +++++ composer.json | 2 +- src/fields/ColourSwatches.php | 10 ++++++++-- src/models/Settings.php | 13 ++++++++----- src/templates/input.twig | 2 +- 5 files changed, 23 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ea79058..d7c4b9b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,11 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/). +## 5.0.3 - 2024-09-23 +### Fixed +- Undefined Array Key 0 Error in Craft 5.36 Colour Swatches 5.0.2 #133 +- Error occurs when entering colors directly in the CMS field #127 + ## 5.0.2 - 2024-03-08 ### Fixed - Fixed the default fetch if the default isn't the first one in the config list diff --git a/composer.json b/composer.json index e23fa31..c928ad8 100644 --- a/composer.json +++ b/composer.json @@ -2,7 +2,7 @@ "name": "craftpulse/craft-colour-swatches", "description": "Let clients choose from a predefined set of colours and utilise associated colour codes and class names in your templates.", "type": "craft-plugin", - "version": "5.0.2", + "version": "5.0.3", "keywords": [ "craft", "cms", diff --git a/src/fields/ColourSwatches.php b/src/fields/ColourSwatches.php index 17033e5..2d4a12f 100644 --- a/src/fields/ColourSwatches.php +++ b/src/fields/ColourSwatches.php @@ -20,6 +20,7 @@ use craft\gql\TypeLoader; use craft\helpers\Json; +use craft\services\Fields; use GraphQL\Type\Definition\ObjectType; use GraphQL\Type\Definition\ResolveInfo; use GraphQL\Type\Definition\Type; @@ -108,6 +109,11 @@ public function rules(): array return array_merge($rules, [['options', 'each', 'rule' => ['required']], ]); } + public function beforeSave(bool $isNew): bool + { + $this->options = $this->settings['options'] ?? []; + return parent::beforeSave($isNew); // TODO: Change the autogenerated stub + } /** * @return array|string @@ -125,7 +131,6 @@ public function getContentColumnType(): array|string */ public function normalizeValue(mixed $value, ?ElementInterface $element = null): ?ColourSwatchesModel { - if ($value instanceof ColourSwatchesModel) { return $value; } @@ -139,6 +144,7 @@ public function normalizeValue(mixed $value, ?ElementInterface $element = null): // if useConfigFile is set, fetch the objects from that file if ($this->useConfigFile) { +// if (ColorSwatches::$plugin->settings->palettes[$this->palette] ?? false) { // if the palette with the value exists, return this as the settings palette $this->options = ColorSwatches::$plugin->settings->palettes[$this->palette]; @@ -203,7 +209,7 @@ public function serializeValue(mixed $value, ?ElementInterface $element = null): $default = array_filter($settingsPalette, function($option) {return $option['default'] == true;}); if (!is_null($default) && count($default) > 0) { - $this->default = $default[0]['label']; + $this->default = array_values($default)[0]['label']; } } diff --git a/src/models/Settings.php b/src/models/Settings.php index 50bbed9..128519e 100644 --- a/src/models/Settings.php +++ b/src/models/Settings.php @@ -31,7 +31,7 @@ class Settings extends Model protected function defineRules(): array { return [ - [['colors', 'palettes'], 'required'], +// [['colors', 'palettes'], 'required'], [['colors', 'palettes'], function ($attribute, $params) { if (!is_array($this->colors)) { $this->addError('colors', Craft::t('colour-swatches', 'colors is not array!')); @@ -43,13 +43,16 @@ protected function defineRules(): array }], [['palettes'], 'filter', 'filter' => function ($palettes) { foreach ($palettes as &$palette) { - foreach ($palette as &$color) { - if (is_string($color['color'])) { - $color['color'] = json_decode($color['color'], true); + if (!is_string($palette)) { + foreach ($palette as &$color) { + if (is_string($color['color'])) { + $color['color'] = json_decode($color['color'], true); + } } + } else { + $palette = []; } } - return $palettes; }] ]; diff --git a/src/templates/input.twig b/src/templates/input.twig index 090ec85..58f68ef 100644 --- a/src/templates/input.twig +++ b/src/templates/input.twig @@ -31,7 +31,7 @@ {# : field.options %}#} {% if options|length == 0 %} -

{{ "The selected colour palette for this field isn\'t defined in the `colour-swatches.php` config file"|t('colour-swatches') }}

+

{{ "The selected colour palette for this field isn\'t defined in the `colour-swatches.php` config file or settings."|t('colour-swatches') }}

{% endif %}