Skip to content

Commit

Permalink
Merge pull request #138 from craftpulse/v5-develop
Browse files Browse the repository at this point in the history
feat: added collection method
  • Loading branch information
michtio authored Oct 28, 2024
2 parents 44a5a35 + 2b549f0 commit 1e33c1d
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 15 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.1.0 - 2024-10-28
### Added
- Added a `collection` function to the field that will return a recursive laravel collection for easier use in twig templates and to do manipulations.

## 5.0.3 - 2024-09-23
### Fixed
- Undefined Array Key 0 Error in Craft 5.36 Colour Swatches 5.0.2 #133
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.3",
"version": "5.1.0",
"keywords": [
"craft",
"cms",
Expand Down
6 changes: 3 additions & 3 deletions src/ColourSwatches.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<?php
/**
* color-swatches plugin for Craft CMS 3.x.
* color-swatches plugin for Craft CMS 5.x.
*
* Let clients choose from a predefined set of colours.
*
* @link https://percipio.london
* @link https://craftpulse.com
*
* @copyright Copyright (c) 2020 Percipio Global Ltd.
* @copyright Copyright (c) 2024 CraftPulse.
*/

namespace percipiolondon\colourswatches;
Expand Down
22 changes: 11 additions & 11 deletions src/fields/ColourSwatches.php
Original file line number Diff line number Diff line change
Expand Up @@ -331,16 +331,16 @@ public function getInputHtml(mixed $value, ?ElementInterface $element = null): s
// Render the input template
return Craft::$app->getView()
->renderTemplate('colour-swatches/input',
[
'name' => $this->handle,
'fieldValue' => $value,
'field' => $this,
'id' => $id,
'namespacedId' => $namespacedId,
'configOptions' => ColorSwatches::$plugin->settings->colors,
'palettes' => ColorSwatches::$plugin->settings->palettes,
]
);
[
'name' => $this->handle,
'fieldValue' => $value,
'field' => $this,
'id' => $id,
'namespacedId' => $namespacedId,
'configOptions' => ColorSwatches::$plugin->settings->colors,
'palettes' => ColorSwatches::$plugin->settings->palettes,
]
);
}

/**
Expand Down Expand Up @@ -429,4 +429,4 @@ public function getPreviewHtml(mixed $value, ElementInterface $element): string
}
return '<div class="color small static"><div class="color-preview" style="' . $style . '"></div></div>';
}
}
}
23 changes: 23 additions & 0 deletions src/models/ColourSwatches.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Craft;
use craft\base\Model;
use craft\helpers\Json;
use Illuminate\Support\Collection;

/**
* Class ColourSwatches
Expand Down Expand Up @@ -51,6 +52,16 @@ public function __construct(?string $value)
$this->class = $colorData['class'] ?? '';
}
}

Collection::macro('recursive', function () {
return $this->map(function ($value) {
if (is_array($value) || is_object($value)) {
return collect($value)->recursive();
}

return $value;
});
});
}

// making sure we have json data, returns boolean(true) if this is the case
Expand All @@ -69,6 +80,18 @@ public function validateJson(?string $value): bool
return false;
}

/**
* @return Collection|null
*/
public function collection(): ?Collection
{
if ($this) {
return collect($this['color'])->recursive();
}

return null;
}

/**
* @return string
*/
Expand Down

0 comments on commit 1e33c1d

Please sign in to comment.