Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implementing resolveUsing, displayUsing and fillUsing #235

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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, $requestAttribute) {
$value = $request->{$requestAttribute};

$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.
Expand Down
28 changes: 20 additions & 8 deletions src/Flexible.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -243,6 +241,12 @@ public function resolve($resource, $attribute = null)
{
$attribute = $attribute ?? $this->attribute;

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);
Expand All @@ -261,11 +265,13 @@ public function resolveForDisplay($resource, $attribute = null)
{
$attribute = $attribute ?? $this->attribute;

$this->registerOriginModel($resource);
if ($this->displayCallback && is_callable($this->displayCallback)) {
$this->value = call_user_func($this->displayCallback, $this, $resource, $attribute);

$this->buildGroups($resource, $attribute);
return;
}

$this->value = $this->resolveGroupsForDisplay($this->groups);
$this->resolve($resource, $attribute);
}

/**
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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);
Expand Down