nova-flexible-content
nova-flexible-content copied to clipboard
Values are wrongly parsed as groups
In some cases the recursive parsing of Flexible Values will wrongly assume that an array is always an array of groups.
For example, a nested Flexible Layout with an Boolean Group Field, will result in an Boolean Group Field value parsed as an group. This, in return, will cause the Boolean Group field to trigger an error.
Structure: Flexible Field > Flexible Field > Boolean Group Field
I'm not sure what the correct way of fixing this issue is, but something like this should work:
/**
* Apply JSON decode and recursively check for nested values
*
* @param mixed $value
* @return array
*/
protected function getParsedFlexibleValue($value)
{
if(is_string($value)) {
$raw = json_decode($value, true);
} else {
$raw = $value;
}
if(!is_array($raw)) return $value;
$groups = array_filter($raw, function ($item) {
return isset($item['layout']);
});
if (count($groups) !== count($raw)) {
return $value;
}
return array_map(function($group) use ($value) {
return $this->getParsedFlexibleGroup($group, $value);
}, $raw);
}