I have the name of a field in an array and I want to turn it back into a field i can work with.
This wont work obviously because its a string now:
$att['field']->toDate($format)
How can i turn $att['field']
into something i can then chain stuff onto like I would directly on a field?
moeli
February 23, 2019, 8:51pm
2
I don’t think you can “turn” a string into a field but you should be able to call it by name.
Assuming you are in a page context (untested):
if($field = $page->$att['field']()->exists()) {
echo $field->toDate($format);
}
Thanks for trying, but it doesnt work, it still sees it as a string. I’m doing this in a plugin.
moeli
February 23, 2019, 9:03pm
4
It is difficult to find out what the problem is without more information like error messages and your code.
It’s a big plugin, to much to post, but the problem really is just that - I know the name of the field in an array and i want get it as a proper field so i can then chain field methods on to it, if the field exists.
I got the following error with the code you posted…
**Fatal error** : Uncaught Whoops\Exception\ErrorException: Array to string conversion
I did this in a function…
$att = json_decode(json_encode($tag['attributes']), true);
if($field = page()->$att['field']()->exists()) {
return $field->toDate($format);
}
texnixe
February 23, 2019, 9:17pm
6
This doesn’t make sense because $field
will now be a boolean, not a field object.
But
if(page()->{$att['field']}()->exists()) {
return page()->{$att['field']}()->toDate($format);
}
1 Like
Thanks That did the trick… expect an awesome plugin sometime soon