Skip to content

Commit

Permalink
Merge pull request #135 from craftpulse/v5-develop
Browse files Browse the repository at this point in the history
5.0.3
  • Loading branch information
cookie10codes committed Sep 23, 2024
2 parents 1c6137f + 8a6ff4b commit 44a5a35
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 9 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
10 changes: 8 additions & 2 deletions src/fields/ColourSwatches.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -125,7 +131,6 @@ public function getContentColumnType(): array|string
*/
public function normalizeValue(mixed $value, ?ElementInterface $element = null): ?ColourSwatchesModel
{

if ($value instanceof ColourSwatchesModel) {
return $value;
}
Expand All @@ -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];
Expand Down Expand Up @@ -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'];
}
}

Expand Down
13 changes: 8 additions & 5 deletions src/models/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -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!'));
Expand All @@ -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;
}]
];
Expand Down
2 changes: 1 addition & 1 deletion src/templates/input.twig
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
{# : field.options %}#}

{% if options|length == 0 %}
<p>{{ "The selected colour palette for this field isn\'t defined in the `colour-swatches.php` config file"|t('colour-swatches') }}</p>
<p>{{ "The selected colour palette for this field isn\'t defined in the `colour-swatches.php` config file or settings."|t('colour-swatches') }}</p>
{% endif %}

<div id="{{ id }}" class="color-swatches">
Expand Down

0 comments on commit 44a5a35

Please sign in to comment.