-
-
Notifications
You must be signed in to change notification settings - Fork 80
/
EntriesField.php
57 lines (45 loc) · 2.25 KB
/
EntriesField.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
<?php
declare(strict_types=1);
/**
* Flextype (https://flextype.org)
* Founded by Sergey Romanenko and maintained by Flextype Community.
*/
use Atomastic\Arrays\Arrays;
if (registry()->get('flextype.settings.entries.fields.entries.fetch.enabled')) {
emitter()->addListener('onEntriesFetchSingleHasResult', static function (): void {
if (entries()->registry()->has('fetch.data.entries.fetch')) {
// Get fetch.
$original = entries()->registry()->get('fetch');
$data = [];
switch (registry()->get('flextype.settings.entries.fields.entries.fetch.result')) {
case 'toArray':
$resultTo = 'toArray';
break;
case 'toObject':
default:
$resultTo = 'copy';
break;
}
// Modify fetch.
foreach (entries()->registry()->get('fetch.data.entries.fetch') as $field => $body) {
if (isset($body['options']['method']) &&
strpos($body['options']['method'], 'fetch') !== false &&
is_callable([entries(), $body['options']['method']])) {
$fetchFromCallbackMethod = $body['options']['method'];
} else {
$fetchFromCallbackMethod = 'fetch';
}
$result = isset($body['result']) && in_array($body['result'], ['toArray', 'toObject']) ? $body['result'] : $resultTo;
$data[$field] = entries()->{$fetchFromCallbackMethod}($body['id'],
isset($body['options']) ?
$body['options'] :
[]);
$data[$field] = ($data[$field] instanceof Arrays) ? $data[$field]->{$result}() : $data[$field];
}
// Save fetch.
entries()->registry()->set('fetch.id', $original['id']);
entries()->registry()->set('fetch.options', $original['options']);
entries()->registry()->set('fetch.data', arrays($original['data'])->merge($data)->toArray());
}
});
}