Closed kupoback closed 1 year ago
Have you taken a look at https://github.com/roots/acorn/blob/main/src/Roots/Acorn/View/Composers/Concerns/AcfFields.php ?
Have you taken a look at https://github.com/roots/acorn/blob/main/src/Roots/Acorn/View/Composers/Concerns/AcfFields.php ?
Yeah, I see it now, it's a lot cleaner, something I could adapt to instead of what I migrated above. I don't like the idea of get_fields() in an acf block, when I know what those field names are.
So I took a look at the above Class again, and played around with a bit more, and ended up with this. However, I will admit I am not super familiar with Laravel Class and methods, so I would love to know if this is the right route? I had to actually call to the toJson()
method, and decode it to get the results i was looking for.
public function getFields()
:array
{
return collect($this->data)
->mapWithKeys(fn($value) => [$value => get_field($value)])
->mapWithKeys(function ($value, $key) {
$value = is_array($value)
? json_decode((new Fluent($value))->toJson())
: $value;
$method = Str::camel($key);
return [$key => method_exists($this, $method) ? $this->{$method}($value) : $value];
})
->all();
}
Sorry for the late reply. That looks good. You could maybe do (object) $value
instead of the json_decode()
but I don't think that will handle objects recursively.
Going to close this for now as it's not entirely in scope for ACF Composer at the moment – although what you have does look useful. 😄
In Sage 9, in the Controller Files, the
protected $acf
array returns each instance of a field into an object. I really liked working with that, and was wondering if there was a way, either as an option, or just natively, to do the same.I am working with your current package, but with Sage 10, made an
AcfNestedFields.php
Class, and ported over most of the functionality. I was wondering if you would maybe consider using this as inspiration to implement into this great package.Acf Block Class
AcfNestedFields.php