-
-
Notifications
You must be signed in to change notification settings - Fork 80
/
MediaField.php
108 lines (83 loc) · 4.43 KB
/
MediaField.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
<?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.media.files.fetch.enabled')) {
emitter()->addListener('onEntriesFetchSingleHasResult', static function (): void {
if (entries()->registry()->has('fetch.data.media.files.fetch')) {
// Get fetch.
$original = entries()->registry()->get('fetch');
$data = [];
switch (registry()->get('flextype.settings.entries.fields.media.files.fetch.result')) {
case 'toArray':
$resultTo = 'toArray';
break;
case 'toObject':
default:
$resultTo = 'copy';
break;
}
// Modify fetch.
foreach (entries()->registry()->get('fetch.data.media.files.fetch') as $field => $body) {
if (isset($body['options']['method']) &&
strpos($body['options']['method'], 'fetch') !== false &&
is_callable([flextype('media')->files(), $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] = flextype('media')->files()->{$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());
}
});
}
if (registry()->get('flextype.settings.entries.fields.media.folders.fetch.enabled')) {
emitter()->addListener('onEntriesFetchSingleHasResult', static function (): void {
if (entries()->registry()->has('fetch.data.media.folders.fetch')) {
// Get fetch.
$original = entries()->registry()->get('fetch');
$data = [];
switch (registry()->get('flextype.settings.entries.fields.media.folders.fetch.result')) {
case 'toArray':
$resultTo = 'toArray';
break;
case 'toObject':
default:
$resultTo = 'copy';
break;
}
// Modify fetch.
foreach (entries()->registry()->get('fetch.data.media.folders.fetch') as $field => $body) {
if (isset($body['options']['method']) &&
strpos($body['options']['method'], 'fetch') !== false &&
is_callable([flextype('media')->folders(), $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] = flextype('media')->folders()->{$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());
}
});
}