From 647aaadf6a2c176db1645c42527f50c0d9c6abbb Mon Sep 17 00:00:00 2001 From: Jason Varga Date: Fri, 8 May 2026 14:34:43 -0400 Subject: [PATCH 1/2] Simplify crop aspect ratio config formats Drops the keyed-string and key-as-label fallback forms in favor of two supported shapes: a plain "W:H" string or an array with explicit label and ratio. Labels are now passed through __() at resolve time so they can be translation keys (config-cache safe). Co-Authored-By: Claude Opus 4.7 (1M context) --- config/assets.php | 4 ++-- src/Assets/CropAspectRatios.php | 7 +++---- tests/Assets/CropAspectRatiosTest.php | 14 +++++++++----- .../Http/View/Composers/JavascriptComposerTest.php | 4 ++-- 4 files changed, 16 insertions(+), 13 deletions(-) diff --git a/config/assets.php b/config/assets.php index 9cdb6af4be9..632359abf7a 100644 --- a/config/assets.php +++ b/config/assets.php @@ -188,8 +188,8 @@ |-------------------------------------------------------------------------- | | Configure the aspect ratio presets available in the Control Panel image - | crop editor. Ratios may be provided as "W:H" strings, keyed values, or - | arrays with custom labels and ratio values. + | crop editor. Each entry may be a "W:H" string (e.g. "16:9") or an array + | with a custom label and ratio: ['label' => 'Wide', 'ratio' => '16:9']. | */ diff --git a/src/Assets/CropAspectRatios.php b/src/Assets/CropAspectRatios.php index b6642fa7a7a..e7b7047c1ff 100644 --- a/src/Assets/CropAspectRatios.php +++ b/src/Assets/CropAspectRatios.php @@ -39,7 +39,6 @@ private static function parse(mixed $entry, mixed $key): ?array return null; } - $label = is_string($key) ? $key : (string) $entry; $value = self::ratioToFloat($entry); if ($value === null) { @@ -49,7 +48,7 @@ private static function parse(mixed $entry, mixed $key): ?array } return [ - 'label' => $label, + 'label' => __((string) $entry), 'value' => $value, ]; } @@ -60,7 +59,7 @@ private static function parse(mixed $entry, mixed $key): ?array */ private static function parseArrayEntry(array $entry, mixed $key): ?array { - $label = $entry['label'] ?? (is_string($key) ? $key : null); + $label = $entry['label'] ?? null; $ratio = $entry['ratio'] ?? null; $labelIsStringable = is_string($label) || is_numeric($label) || $label instanceof \Stringable; @@ -80,7 +79,7 @@ private static function parseArrayEntry(array $entry, mixed $key): ?array } return [ - 'label' => (string) $label, + 'label' => __((string) $label), 'value' => $value, ]; } diff --git a/tests/Assets/CropAspectRatiosTest.php b/tests/Assets/CropAspectRatiosTest.php index 6163527be4b..fcf41c4022b 100644 --- a/tests/Assets/CropAspectRatiosTest.php +++ b/tests/Assets/CropAspectRatiosTest.php @@ -38,16 +38,20 @@ public function it_supports_custom_labels_and_fractional_ratio_strings() } #[Test] - public function it_supports_keyed_ratio_entries() + public function it_translates_labels() { + app('translator')->addLines([ + 'crop.wide' => 'Wide Screen', + ], 'en'); + config()->set('statamic.assets.crop_aspect_ratios', [ - 'Portrait' => '9:16', - 'A4' => '210:297', + ['label' => 'crop.wide', 'ratio' => '16:9'], + ['label' => 'Square', 'ratio' => '1:1'], ]); $this->assertSame([ - ['label' => 'Portrait', 'value' => 9 / 16], - ['label' => 'A4', 'value' => 210 / 297], + ['label' => 'Wide Screen', 'value' => 16 / 9], + ['label' => 'Square', 'value' => 1.0], ], CropAspectRatios::all()); } diff --git a/tests/Http/View/Composers/JavascriptComposerTest.php b/tests/Http/View/Composers/JavascriptComposerTest.php index 7419571e247..f37ab390211 100644 --- a/tests/Http/View/Composers/JavascriptComposerTest.php +++ b/tests/Http/View/Composers/JavascriptComposerTest.php @@ -17,7 +17,7 @@ class JavascriptComposerTest extends TestCase public function it_provides_crop_aspect_ratios_to_script_config() { config()->set('statamic.assets.crop_aspect_ratios', [ - 'Portrait' => '9:16', + '9:16', ['label' => 'US Letter', 'ratio' => '8.5:11'], ]); @@ -33,7 +33,7 @@ public function it_provides_crop_aspect_ratios_to_script_config() $json = Statamic::jsonVariables(request()); $this->assertSame([ - ['label' => 'Portrait', 'value' => 9 / 16], + ['label' => '9:16', 'value' => 9 / 16], ['label' => 'US Letter', 'value' => 8.5 / 11], ], $json['cropAspectRatios']); } From 8c49a44f7efda1ee43540441cb4b444c5381d1e0 Mon Sep 17 00:00:00 2001 From: Jason Varga Date: Fri, 8 May 2026 14:39:00 -0400 Subject: [PATCH 2/2] Import Statamic\trans as __ in CropAspectRatios Required by scripts/check-trans-import.sh (per #14610) for any file making unqualified __() calls. Co-Authored-By: Claude Opus 4.7 (1M context) --- src/Assets/CropAspectRatios.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Assets/CropAspectRatios.php b/src/Assets/CropAspectRatios.php index e7b7047c1ff..e9905c34d0d 100644 --- a/src/Assets/CropAspectRatios.php +++ b/src/Assets/CropAspectRatios.php @@ -4,6 +4,8 @@ use Illuminate\Support\Facades\Log; +use function Statamic\trans as __; + class CropAspectRatios { /**