Skip to content

Commit 7fa6c03

Browse files
Enable preview images for layout field
1 parent e258d3a commit 7fa6c03

File tree

5 files changed

+83
-41
lines changed

5 files changed

+83
-41
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"description": "Kirby Visual Block Selector",
44
"license": "MIT",
55
"type": "kirby-plugin",
6-
"version": "3.0.1",
6+
"version": "3.0.2",
77
"authors": [
88
{
99
"name": "JUNO",

index.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,16 @@
88
throw new Exception('The visual block selector plugin requires Kirby 5');
99
}
1010

11-
// Auto-load the custom BlocksField class
11+
// Auto-load custom field classes
1212
load([
13+
'JunoHamburg\VisualBlockSelector\BlocksFieldPreviewTrait' => __DIR__ . '/src/BlocksFieldPreviewTrait.php',
1314
'JunoHamburg\VisualBlockSelector\BlocksField' => __DIR__ . '/src/BlocksField.php',
15+
'JunoHamburg\VisualBlockSelector\LayoutField' => __DIR__ . '/src/LayoutField.php',
1416
]);
1517

1618
Kirby::plugin('junohamburg/visual-block-selector', [
1719
'fields' => [
18-
'blocks' => 'JunoHamburg\VisualBlockSelector\BlocksField'
20+
'blocks' => 'JunoHamburg\VisualBlockSelector\BlocksField',
21+
'layout' => 'JunoHamburg\VisualBlockSelector\LayoutField',
1922
],
2023
]);

src/BlocksField.php

Lines changed: 4 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace JunoHamburg\VisualBlockSelector;
44

5-
use Kirby\Filesystem\Dir;
65
use Kirby\Form\Field\BlocksField as KirbyBlocksField;
76

87
/**
@@ -11,45 +10,12 @@
1110
*/
1211
class BlocksField extends KirbyBlocksField
1312
{
14-
/**
15-
* Collects all block preview images
16-
* in the assets directory
17-
*
18-
* @return array<string, string>
19-
*/
20-
protected function previewImages(): array
21-
{
22-
$kirby = $this->kirby();
23-
$previews = [];
24-
$dir = 'block-previews';
25-
$path = $kirby->root('assets') . '/' . $dir;
26-
$images = Dir::files($path);
27-
28-
$relativePath = str_replace($kirby->root('index') . '/', '', $kirby->root('assets'));
29-
30-
foreach ($images as $image) {
31-
$asset = asset($relativePath . '/' . $dir . '/' . $image);
32-
$previews[$asset->name()] = $asset->url();
33-
}
13+
use BlocksFieldPreviewTrait;
3414

35-
return $previews;
36-
}
37-
38-
/**
39-
* Adds the preview images to the fieldsets
40-
* if they exist
41-
*/
4215
public function props(): array
4316
{
44-
$props = parent::props();
45-
$previewsImages = $this->previewImages();
46-
47-
return [
48-
...$props,
49-
'fieldsets' => array_map(fn (array $fieldset) => [
50-
...$fieldset,
51-
'previewImage' => $previewsImages[$fieldset['type']] ?? null
52-
], $props['fieldsets'])
53-
];
17+
$props = parent::props();
18+
$props['fieldsets'] = $this->addPreviewImagesToFieldsets($props['fieldsets']);
19+
return $props;
5420
}
5521
}

src/BlocksFieldPreviewTrait.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
3+
namespace JunoHamburg\VisualBlockSelector;
4+
5+
use Kirby\Filesystem\Dir;
6+
7+
/**
8+
* Trait that adds preview images to block fieldsets
9+
*/
10+
trait BlocksFieldPreviewTrait
11+
{
12+
/**
13+
* Collects all block preview images
14+
* in the assets directory
15+
*
16+
* @return array<string, string>
17+
*/
18+
protected function previewImages(): array
19+
{
20+
$kirby = $this->kirby();
21+
$previews = [];
22+
$dir = 'block-previews';
23+
$path = $kirby->root('assets') . '/' . $dir;
24+
25+
if (!is_dir($path)) {
26+
return [];
27+
}
28+
29+
$images = Dir::files($path);
30+
$relativePath = str_replace($kirby->root('index') . '/', '', $kirby->root('assets'));
31+
32+
foreach ($images as $image) {
33+
$asset = asset($relativePath . '/' . $dir . '/' . $image);
34+
$previews[$asset->name()] = $asset->url();
35+
}
36+
37+
return $previews;
38+
}
39+
40+
/**
41+
* Adds the preview images to the fieldsets
42+
*/
43+
protected function addPreviewImagesToFieldsets(array $fieldsets): array
44+
{
45+
$previewImages = $this->previewImages();
46+
47+
return array_map(fn (array $fieldset) => [
48+
...$fieldset,
49+
'previewImage' => $previewImages[$fieldset['type']] ?? null
50+
], $fieldsets);
51+
}
52+
}

src/LayoutField.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace JunoHamburg\VisualBlockSelector;
4+
5+
use Kirby\Form\Field\LayoutField as KirbyLayoutField;
6+
7+
/**
8+
* Custom LayoutField that adds preview images to fieldsets
9+
* within layout columns
10+
*/
11+
class LayoutField extends KirbyLayoutField
12+
{
13+
use BlocksFieldPreviewTrait;
14+
15+
public function props(): array
16+
{
17+
$props = parent::props();
18+
$props['fieldsets'] = $this->addPreviewImagesToFieldsets($props['fieldsets']);
19+
return $props;
20+
}
21+
}

0 commit comments

Comments
 (0)