diff --git a/src/Flexible.php b/src/Flexible.php index ceba13b1..35fe2cc9 100644 --- a/src/Flexible.php +++ b/src/Flexible.php @@ -404,13 +404,31 @@ protected function extractValue(NovaRequest $request, $attribute) return; } - if (! is_array($value)) { - throw new \Exception('Unable to parse incoming Flexible content, data should be an array.'); + if (!is_array($value)) { + return $this->mapPolymorphicRelationships($value); } return $value; } + /** + * Try decoding value to array to make it work with Polymorphic Relationships + * @param string $value + * @return \Illuminate\Support\Collection + * @throws \Exception + */ + protected function mapPolymorphicRelationships(string $value) { + if (!$value = @json_decode($value, true)) { + throw new \Exception('Unable to parse incoming Flexible content, data should be an array.'); + } + return collect($value)->map(function ($item) { + $item['attributes'] = collect($item['attributes'])->mapWithKeys(function ($layout, $key) use ($item) { + return [str_replace("{$item['key']}__", "", $key) => $layout]; + })->toArray(); + return $item; + }); + } + /** * Resolve all contained groups and their fields *