1

I'm trying to display a json array on the EasyAdmin detail page. I read here Is there a way to represent a JSON field in EasyAdmin 3? that you can use ArrayField in EasyAdmin 3 to display a json array, which would make sense to me as that is the only field in EasyAdmin that could display it so I added it like this:

public function configureFields(string $pageName): iterable
{
    return [
        ArrayField::new('status', $this->translator->trans('form.label.status'))->onlyOnDetail(),
    ];
}

But it gives me this error: An exception has been thrown during the rendering of a template ("Notice: Array to string conversion"). Do I need to add something else to it or does it not work at all?

madebyjo_l
  • 28
  • 8
  • 1
    Do you use the ArrayField from 'EasyCorp\Bundle\EasyAdminBundle\Field\ArrayField;' ? – alessandro_podo Sep 29 '21 at 05:40
  • @alessandro_podo yes I do – madebyjo_l Sep 29 '21 at 08:14
  • can you try ArrayField::new('status'); if that works? – alessandro_podo Sep 29 '21 at 19:09
  • 1
    @madebyjo_l, you specified at the end `...->onlyOnDetail()` so if you're not on the detail page you should still get the error. I hope I'm wrong by assuming you're not on the detail page that's why the error is still showing. Let me know by trying just `ArrayField::new('status');` – sylvery Dec 04 '21 at 16:45
  • Not only the ArrayField can process arrays: the ChoiceField can do it too. To this: "Do I need to add something else to it or does it not work at all?", the answer is yes. That should work for sure. You have done a mistake somewhere. You can either check your 'translator' function, your Entity's getter and setter for this propery, try to add the toString() method to your entity, try to use the "yield" syntax instead of returning an array,... – Edouard Apr 21 '22 at 14:40

2 Answers2

1

I found a workaround that resolved the issue in my situation where I wanted just to show JSON on details page

So in your entity add a get function as an unmapped field as indicated in the official documentaiton https://symfony.com/bundles/EasyAdminBundle/current/fields.html#unmapped-fields

for example

public function getJsonField() {
  return json_encode($this->yourEntityJsonAttribute);
}

then in configureFields function in your CrudController add your field like this

TextAreaField::new('jsonField')->onlyOnDetail()

You can also read the json attribute and generate an HTML string in the getJsonField function then in configure field just add renderAsHtml in th field like this

TextAreaField::new('jsonField')->onlyOnDetail()->renderAsHtml()

Wish this suits your case too, Good luck

Ahmed Ghiloubi
  • 341
  • 2
  • 6
0

change "status" to a multiplicity "statuses" Worked for me with changing "print" to "prints"