Skip to content

Commit b1503d1

Browse files
committed
Add support for API options. Closes #8.
1 parent f59f9fa commit b1503d1

File tree

3 files changed

+95
-3
lines changed

3 files changed

+95
-3
lines changed

lib/fields/imageboxes.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?php
22

3+
require_once dirname(__DIR__) . '/options/imageboxes-optionsapi.php';
34
require_once dirname(__DIR__) . '/options/imageboxes-optionsquery.php';
45
require_once dirname(__DIR__) . '/options/imageboxes-options.php';
56

@@ -49,4 +50,4 @@
4950
));
5051

5152

52-
return $base;
53+
return $base;

lib/options/imageboxes-options.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22

33
use Kirby\Form\Options;
4+
use Kirby\Cms\App;
45

56
class ImageBoxesOptions extends Options {
67
public static function api($api, $model = null): array {
@@ -18,14 +19,16 @@ public static function api($api, $model = null): array {
1819
} else {
1920
$url = $api;
2021
}
21-
$optionsApi = new OptionsApi([
22+
23+
$optionsApi = new ImageBoxesOptionsApi([
2224
'data' => static::data($model),
2325
'fetch' => $fetch,
2426
'url' => $url,
2527
'text' => $text,
2628
'value' => $value,
2729
'image' => $image
2830
]);
31+
2932
return $optionsApi->options();
3033
}
3134

@@ -74,7 +77,7 @@ public static function factory($options, array $props = [], $model = null): arra
7477
$option = [
7578
'value' => $option['value'],
7679
'text' => $option['text'],
77-
'image' => $option['image'],
80+
'image' => $option['image'],
7881
];
7982
}
8083
// translate the option text
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
<?php
2+
3+
use Kirby\Cms\Nest;
4+
use Kirby\Form\OptionsApi;
5+
use Kirby\Toolkit\Query;
6+
7+
class ImageBoxesOptionsApi extends OptionsApi {
8+
/**
9+
* @var string
10+
*/
11+
protected $image = '{{ item.image }}';
12+
13+
/**
14+
* @return array
15+
* @throws \Exception
16+
* @throws \Kirby\Exception\InvalidArgumentException
17+
*/
18+
public function options(): array
19+
{
20+
if (is_array($this->options) === true) {
21+
return $this->options;
22+
}
23+
24+
if (Url::isAbsolute($this->url()) === true) {
25+
// URL, request via cURL
26+
$data = Remote::get($this->url())->json();
27+
} else {
28+
// local file, get contents locally
29+
30+
// ensure the file exists before trying to load it as the
31+
// file_get_contents() warnings need to be suppressed
32+
if (is_file($this->url()) !== true) {
33+
throw new Exception('Local file ' . $this->url() . ' was not found');
34+
}
35+
36+
$content = @file_get_contents($this->url());
37+
38+
if (is_string($content) !== true) {
39+
throw new Exception('Unexpected read error'); // @codeCoverageIgnore
40+
}
41+
42+
if (empty($content) === true) {
43+
return [];
44+
}
45+
46+
$data = json_decode($content, true);
47+
}
48+
49+
if (is_array($data) === false) {
50+
throw new InvalidArgumentException('Invalid options format');
51+
}
52+
53+
$result = (new Query($this->fetch(), Nest::create($data)))->result();
54+
$options = [];
55+
56+
foreach ($result as $item) {
57+
// dump($item);
58+
$data = array_merge($this->data(), ['item' => $item]);
59+
60+
$options[] = [
61+
'text' => $this->field('text', $data),
62+
'value' => $this->field('value', $data),
63+
// added this ↓
64+
'image' => $this->field('image', $data),
65+
];
66+
}
67+
68+
return $options;
69+
}
70+
71+
/**
72+
* @param string $text
73+
* @return $this
74+
*/
75+
protected function setImage(?string $image = null)
76+
{
77+
$this->image = $image;
78+
return $this;
79+
}
80+
81+
/**
82+
* @return string
83+
*/
84+
public function image(): string
85+
{
86+
return $this->image;
87+
}
88+
}

0 commit comments

Comments
 (0)