From cc043733752a5e1e660a1df761267e4d008a8332 Mon Sep 17 00:00:00 2001 From: iglocska Date: Wed, 17 Nov 2021 15:47:32 +0100 Subject: [PATCH] new: [crud component] fixes - add hidden option - fix afterfind --- src/Controller/Component/CRUDComponent.php | 29 +++++++++++++++++++--- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/src/Controller/Component/CRUDComponent.php b/src/Controller/Component/CRUDComponent.php index 64b5777..45df6d5 100644 --- a/src/Controller/Component/CRUDComponent.php +++ b/src/Controller/Component/CRUDComponent.php @@ -61,11 +61,25 @@ class CRUDComponent extends Component } if ($this->Controller->ParamHandler->isRest()) { $data = $query->all(); + if (isset($options['hidden'])) { + $data->each(function($value, $key) use ($options) { + $hidden = is_array($options['hidden']) ? $options['hidden'] : [$options['hidden']]; + $value->setHidden($hidden); + return $value; + }); + } if (isset($options['afterFind'])) { + $function = $options['afterFind']; if (is_callable($options['afterFind'])) { - $data = $options['afterFind']($data); + $function = $options['afterFind']; + $data->each(function($value, $key) use ($function) { + return $function($value); + }); } else { - $data = $this->Table->{$options['afterFind']}($data); + $t = $this->Table; + $data->each(function($value, $key) use ($t, $function) { + return $t->$function($value); + }); } } $this->Controller->restResponsePayload = $this->RestResponse->viewData($data, 'json'); @@ -73,10 +87,17 @@ class CRUDComponent extends Component $this->Controller->loadComponent('Paginator'); $data = $this->Controller->Paginator->paginate($query); if (isset($options['afterFind'])) { + $function = $options['afterFind']; if (is_callable($options['afterFind'])) { - $data = $options['afterFind']($data); + $function = $options['afterFind']; + $data->each(function($value, $key) use ($function) { + return $function($value); + }); } else { - $data = $this->Table->{$options['afterFind']}($data); + $t = $this->Table; + $data->each(function($value, $key) use ($t, $function) { + return $t->$function($value); + }); } } $this->setFilteringContext($options['contextFilters'] ?? [], $params);