Skip to content

Commit

Permalink
🐛 Added fix for casting/resolving layouts
Browse files Browse the repository at this point in the history
  • Loading branch information
wesselperik committed Nov 15, 2022
1 parent 44eebde commit b110feb
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 20 deletions.
17 changes: 0 additions & 17 deletions src/Concerns/HasFlexible.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use Whitecube\NovaFlexibleContent\Layouts\Layout;
use Whitecube\NovaFlexibleContent\Layouts\Collection;
use Illuminate\Support\Collection as BaseCollection;
use Laravel\Nova\NovaServiceProvider;
use Whitecube\NovaFlexibleContent\Value\FlexibleCast;

trait HasFlexible {
Expand All @@ -32,22 +31,6 @@ public function flexible($attribute, $layoutMapping = [])
* @return \Whitecube\NovaFlexibleContent\Layouts\Collection
*/
public function cast($value, $layoutMapping = [])
{
if(app()->getProvider(NovaServiceProvider::class) && !app()->runningInConsole() && !app()->environment('testing')) {
return $value;
}

return $this->toFlexible($value ?: null, $layoutMapping);
}

/**
* Parse a Flexible Content from value
*
* @param mixed $value
* @param array $layoutMapping
* @return \Whitecube\NovaFlexibleContent\Layouts\Collection
*/
public function toFlexible($value, $layoutMapping = [])
{
$flexible = $this->getFlexibleArrayFromValue($value);

Expand Down
13 changes: 10 additions & 3 deletions src/Value/Resolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace Whitecube\NovaFlexibleContent\Value;

use Illuminate\Support\Collection;
use Whitecube\NovaFlexibleContent\Layouts\Collection as LayoutsCollection;
use Whitecube\NovaFlexibleContent\Layouts\Layout;

class Resolver implements ResolverInterface
{
Expand Down Expand Up @@ -39,11 +41,14 @@ public function get($resource, $attribute, $layouts)
$value = $this->extractValueFromResource($resource, $attribute);

return collect($value)->map(function($item) use ($layouts) {
$layout = $layouts->find($item->layout);
$layout = $layouts->find($item instanceof Layout ? $item->name() : $item->layout);

if(!$layout) return;

return $layout->duplicateAndHydrate($item->key, (array) $item->attributes);
$key = $item instanceof Layout ? $item->key() : $item->key;
$attributes = $item instanceof Layout ? $item->getAttributes() : (array) $item->attributes;

return $layout->duplicateAndHydrate($key, $attributes);
})->filter()->values();
}

Expand All @@ -58,7 +63,9 @@ protected function extractValueFromResource($resource, $attribute)
{
$value = data_get($resource, str_replace('->', '.', $attribute)) ?? [];

if($value instanceof Collection) {
if ($value instanceof LayoutsCollection) {
$value = $value->all();
} elseif ($value instanceof Collection) {
$value = $value->toArray();
} else if (is_string($value)) {
$value = json_decode($value) ?? [];
Expand Down

0 comments on commit b110feb

Please sign in to comment.