-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathKirbyEditorGalleryBlock.php
87 lines (75 loc) · 2.47 KB
/
KirbyEditorGalleryBlock.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
<?php
namespace Kirby\Editor;
use Throwable;
class GalleryBlock extends Block
{
public function controller(): array
{
$data = parent::controller();
$arrayData = $this->toArray();
$data['images'] = $arrayData['attrs']['images'];
return $data;
}
public function image($filename)
{
try {
return $this->parent()->file($filename);
} catch (Throwable $e) {
return null;
}
}
public function isEmpty(): bool
{
$data = $this->toArray();
$images = $data['attr']['images'];
return count($images) > 0;
}
public function toArray(bool $toStorage = false): array
{
$data = parent::toArray();
$attrs = $data['attrs'] ?? [];
$images = $attrs['images'] ?? [];
$newImages = [];
foreach ($images as $imageData) {
$image = null;
$altText = isset($imageData['altText']) ? $imageData['altText'] : '';
$imageClass = isset($imageData['imageClass']) ? $imageData['imageClass'] : '';
if (isset($imageData['filename'])) {
$image = $this->image($imageData['filename']);
} else {
$image = $this->imageGuid($imageData['guid']);
}
if ($image) {
$imageRecord = [
'filename' => $image->filename(),
'guid' => $image->panelUrl(true),
'ratio' => $image->ratio(),
'height' => $image->height(),
'width' => $image->width(),
'id' => $image->id(),
'src' => $image->resize(1000)->url(),
'image' => $image,
'altText' => $altText,
'imageClass' => $imageClass,
];
if ($toStorage === true) {
unset($imageRecord['ratio']);
unset($imageRecord['src']);
unset($imageRecord['width']);
unset($imageRecord['height']);
unset($imageRecord['id']);
unset($imageRecord['image']);
}
array_push($newImages, $imageRecord);
}
}
$data['attrs'] = array_merge($data['attrs'] ?? [], [
'images' => $newImages
]);
return $data;
}
public function toStorage(): array
{
return $this->toArray(true);
}
}