From e3d27f1cfae0cb6221baf654a2391663407c01e5 Mon Sep 17 00:00:00 2001 From: Jeffrey Zant Date: Mon, 2 Nov 2020 11:21:48 +0100 Subject: [PATCH 1/3] Implementing resolveUsing, displayUsing and fillUsing --- src/Flexible.php | 38 +++++++++++++++++++++++++------------- 1 file changed, 25 insertions(+), 13 deletions(-) diff --git a/src/Flexible.php b/src/Flexible.php index 26ef5e4e..d33aeb69 100644 --- a/src/Flexible.php +++ b/src/Flexible.php @@ -2,13 +2,11 @@ namespace Whitecube\NovaFlexibleContent; -use Illuminate\Support\Str; use Laravel\Nova\Fields\Field; use Laravel\Nova\Http\Requests\NovaRequest; use Whitecube\NovaFlexibleContent\Http\ScopedRequest; use Whitecube\NovaFlexibleContent\Value\Resolver; use Whitecube\NovaFlexibleContent\Value\ResolverInterface; -use Whitecube\NovaFlexibleContent\Layouts\Preset; use Whitecube\NovaFlexibleContent\Layouts\Layout; use Whitecube\NovaFlexibleContent\Layouts\LayoutInterface; use Whitecube\NovaFlexibleContent\Layouts\Collection as LayoutsCollection; @@ -243,11 +241,17 @@ public function resolve($resource, $attribute = null) { $attribute = $attribute ?? $this->attribute; - $this->registerOriginModel($resource); + if (! $this->resolveCallback) { + $this->registerOriginModel($resource); - $this->buildGroups($resource, $attribute); + $this->buildGroups($resource, $attribute); - $this->value = $this->resolveGroups($this->groups); + $this->value = $this->resolveGroups($this->groups); + } elseif (is_callable($this->resolveCallback)) { + tap($this->resolveAttribute($resource, $attribute), function ($value) use ($resource, $attribute) { + $this->value = call_user_func($this->resolveCallback, $this, $value, $resource, $attribute); + }); + } } /** @@ -261,11 +265,13 @@ public function resolveForDisplay($resource, $attribute = null) { $attribute = $attribute ?? $this->attribute; - $this->registerOriginModel($resource); - - $this->buildGroups($resource, $attribute); - - $this->value = $this->resolveGroupsForDisplay($this->groups); + if (! $this->displayCallback) { + $this->resolve($resource, $attribute); + } elseif (is_callable($this->displayCallback)) { + tap($this->value ?? $this->resolveAttribute($resource, $attribute), function ($value) use ($resource, $attribute) { + $this->value = call_user_func($this->displayCallback, $this, $value, $resource, $attribute); + }); + } } /** @@ -305,7 +311,13 @@ protected function fillAttribute(NovaRequest $request, $requestAttribute, $model $callbacks = collect($this->syncAndFillGroups($request, $requestAttribute)); - $this->value = $this->resolver->set($model, $attribute, $this->groups); + $request->{$attribute} = $this->resolver->set($model, $attribute, $this->groups); + + if (isset($this->fillCallback)) { + return call_user_func( + $this->fillCallback, $request, $model, $attribute, $requestAttribute + ); + } if($callbacks->isEmpty()) { return; @@ -399,7 +411,7 @@ protected function extractValue(NovaRequest $request, $attribute) * @param Illuminate\Support\Collection $groups * @return Illuminate\Support\Collection */ - protected function resolveGroups($groups) + public function resolveGroups($groups) { return $groups->map(function($group) { return $group->getResolved(); @@ -428,7 +440,7 @@ protected function resolveGroupsForDisplay($groups) * @param string $attribute * @return Illuminate\Support\Collection */ - protected function buildGroups($resource, $attribute) + public function buildGroups($resource, $attribute) { if(!$this->resolver) { $this->resolver(Resolver::class); From 79b3379f612953a5e142df836df0655a79d19db2 Mon Sep 17 00:00:00 2001 From: Jeffrey Zant Date: Mon, 2 Nov 2020 11:43:44 +0100 Subject: [PATCH 2/3] Small improvements + updated readme --- README.md | 26 ++++++++++++++++++++++++++ src/Flexible.php | 8 ++------ 2 files changed, 28 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index b07fe394..4f9663c6 100644 --- a/README.md +++ b/README.md @@ -151,6 +151,32 @@ By default, the field takes advantage of a **JSON column** on your model's table Tell the field how to store and retrieve its content by creating your own Resolver class, which basically just contains two simple methods: `get` and `set`. [See the docs for more information on this](https://whitecube.github.io/nova-flexible-content/#/?id=custom-resolver-classes). +### resolveUsing, displayUsing, fillUsing + +This package implements the default nova methods to alter data on resolving and updating. + +The resolveUsing/displayUsing needs to perform retrieve the data and format it. This differs from the default usage of these methods. This is why the flexible instance is added to the callback. + +```php +Flexible::make('Content', 'content') + ->resolveUsing(function ($flexible, $resource, $attribute) { + $value = $flexible->resolveGroups($flexible->buildGroups($resource, $attribute)); + + return $value; + }) +``` + +The fillUsing method allows you to alter the data or store the data on a different model/column. + +```php +Flexible::make('Content', 'content') + ->fillUsing(function ($request, $model, $attribute, $requestAttribte) { + $value = $request->{$requestAttribte}; + + $model->{$attribute} = $value; + }) +``` + ### Usage with nova-page Maybe you heard of one of our other packages, [nova-page](https://github.com/whitecube/nova-page), which is a Nova Tool that allows to edit static pages such as an _"About"_ page (or similar) without having to declare a model for it individually. More often than not, the Flexible Content Field comes in handy. Don't worry, both packages work well together! First create a [nova page template](https://github.com/whitecube/nova-page#creating-templates) and add a [flexible content](https://github.com/whitecube/nova-flexible-content#adding-layouts) to the template's fields. diff --git a/src/Flexible.php b/src/Flexible.php index d33aeb69..c262309c 100644 --- a/src/Flexible.php +++ b/src/Flexible.php @@ -248,9 +248,7 @@ public function resolve($resource, $attribute = null) $this->value = $this->resolveGroups($this->groups); } elseif (is_callable($this->resolveCallback)) { - tap($this->resolveAttribute($resource, $attribute), function ($value) use ($resource, $attribute) { - $this->value = call_user_func($this->resolveCallback, $this, $value, $resource, $attribute); - }); + $this->value = call_user_func($this->resolveCallback, $this, $resource, $attribute); } } @@ -268,9 +266,7 @@ public function resolveForDisplay($resource, $attribute = null) if (! $this->displayCallback) { $this->resolve($resource, $attribute); } elseif (is_callable($this->displayCallback)) { - tap($this->value ?? $this->resolveAttribute($resource, $attribute), function ($value) use ($resource, $attribute) { - $this->value = call_user_func($this->displayCallback, $this, $value, $resource, $attribute); - }); + $this->value = call_user_func($this->displayCallback, $this, $resource, $attribute); } } From 4e7b19e3d978cee3125a44653994636fc4a619fe Mon Sep 17 00:00:00 2001 From: Jeffrey Zant Date: Wed, 9 Dec 2020 12:14:26 +0100 Subject: [PATCH 3/3] Early return and typo fix --- README.md | 4 ++-- src/Flexible.php | 24 ++++++++++++++---------- 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 4f9663c6..9e476870 100644 --- a/README.md +++ b/README.md @@ -170,8 +170,8 @@ The fillUsing method allows you to alter the data or store the data on a differe ```php Flexible::make('Content', 'content') - ->fillUsing(function ($request, $model, $attribute, $requestAttribte) { - $value = $request->{$requestAttribte}; + ->fillUsing(function ($request, $model, $attribute, $requestAttribute) { + $value = $request->{$requestAttribute}; $model->{$attribute} = $value; }) diff --git a/src/Flexible.php b/src/Flexible.php index c262309c..e3f558dd 100644 --- a/src/Flexible.php +++ b/src/Flexible.php @@ -241,15 +241,17 @@ public function resolve($resource, $attribute = null) { $attribute = $attribute ?? $this->attribute; - if (! $this->resolveCallback) { - $this->registerOriginModel($resource); - - $this->buildGroups($resource, $attribute); - - $this->value = $this->resolveGroups($this->groups); - } elseif (is_callable($this->resolveCallback)) { + if ($this->resolveCallback && is_callable($this->resolveCallback)) { $this->value = call_user_func($this->resolveCallback, $this, $resource, $attribute); + + return; } + + $this->registerOriginModel($resource); + + $this->buildGroups($resource, $attribute); + + $this->value = $this->resolveGroups($this->groups); } /** @@ -263,11 +265,13 @@ public function resolveForDisplay($resource, $attribute = null) { $attribute = $attribute ?? $this->attribute; - if (! $this->displayCallback) { - $this->resolve($resource, $attribute); - } elseif (is_callable($this->displayCallback)) { + if ($this->displayCallback && is_callable($this->displayCallback)) { $this->value = call_user_func($this->displayCallback, $this, $resource, $attribute); + + return; } + + $this->resolve($resource, $attribute); } /**