From 2ba37a2efa7c1bddd18b5a678ec2b0f11eff6a38 Mon Sep 17 00:00:00 2001 From: Awilum Date: Thu, 7 Apr 2022 14:47:33 +0300 Subject: [PATCH] feat(fields): improve `EntriesField` logic --- .../Entries/Fields/Default/EntriesField.php | 42 +++++++++---------- 1 file changed, 20 insertions(+), 22 deletions(-) diff --git a/src/flextype/core/Entries/Fields/Default/EntriesField.php b/src/flextype/core/Entries/Fields/Default/EntriesField.php index 3efaeee0..23b011de 100644 --- a/src/flextype/core/Entries/Fields/Default/EntriesField.php +++ b/src/flextype/core/Entries/Fields/Default/EntriesField.php @@ -14,14 +14,14 @@ declare(strict_types=1); * Redistributions of files must retain the above copyright notice. */ -use Glowy\Arrays\Arrays; +use Glowy\Arrays\Arrays as Collection; emitter()->addListener('onEntriesFetchSingleHasResult', static function (): void { - + dump(entries()->registry()->get('methods.fetch.collection')); if (! entries()->registry()->get('methods.fetch.collection.fields.entries.enabled')) { return; } - + if (entries()->registry()->has('methods.fetch.result.entries.fetch')) { // Get @@ -34,35 +34,34 @@ emitter()->addListener('onEntriesFetchSingleHasResult', static function (): void break; case 'toObject': - default: $resultTo = 'copy'; break; + + default: + if (entries()->registry()->has('methods.fetch.collection.fields.entries.fetch.result')) { + if (in_array(entries()->registry()->get('methods.fetch.collection.fields.entries.fetch.result'), ['toArray', 'toObject'])) { + $resultTo = entries()->registry()->get('methods.fetch.collection.fields.entries.fetch.result'); + + if ($resultTo == 'toObject') { + $resultTo = 'copy'; + } + } + } else { + $resultTo = 'copy'; + } + break; } // Modify foreach (entries()->registry()->get('methods.fetch.result.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]; + $data[$field] = entries()->fetch($body['id'], isset($body['options']) ? $body['options'] : []); + $data[$field] = ($data[$field] instanceof Collection) ? $data[$field]->{$result}() : $data[$field]; } $result = collection($original['result'])->merge($data)->toArray(); + // Remove entries field if (boolval(entries()->registry()->get('methods.fetch.collection.fields.entries.dump')) === false) { unset($result['entries']); } @@ -71,6 +70,5 @@ emitter()->addListener('onEntriesFetchSingleHasResult', static function (): void entries()->registry()->set('methods.fetch.params.id', $original['params']['id']); entries()->registry()->set('methods.fetch.params.options', $original['params']['options']); entries()->registry()->set('methods.fetch.result', $result); - } }); \ No newline at end of file