Skip to content

ColorData::$label #16492

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG-WIP.md
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
# Release Notes for Craft CMS 5.7 (WIP)

### Extensibility
- Added `craft\fields\data\ColorData::$label`. ([#16492](https://github.com/craftcms/cms/pull/16492))
10 changes: 9 additions & 1 deletion src/fields/Color.php
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,15 @@ public function normalizeValue(mixed $value, ?ElementInterface $element): mixed
}

$value = ColorValidator::normalizeColor($value);
return new ColorData($value);
$value = new ColorData($value);

// set the label on the value too?
$option = Arr::first($this->palette, fn(array $color) => $color['color'] === $value->getHex());
if (isset($option['label']) && $option['label'] !== '') {
$value->label = $option['label'];
}

return $value;
}

/**
Expand Down
6 changes: 6 additions & 0 deletions src/fields/data/ColorData.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@
*/
class ColorData extends BaseObject implements Serializable
{
/**
* @var string|null The human-facing label for the color.
* @since 5.7.0
*/
public ?string $label = null;

/**
* @var string The color’s hex value
*/
Expand Down