Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EasyAdmin 3.X - How to see related entities `toString` instead of the number of association in the list?

I have an entity Product with a ManyToMany relation to an entity Category

/**
 * @ORM\ManyToMany(targetEntity="App\Domain\Category", inversedBy="stalls")
 */
private $categories;

//...

/**
 * @return Collection|Category[]
 */
public function getCategories(): Collection
{
    return $this->categories;
}

In the ProductCrudController class I have the following configureFields method:

public function configureFields(string $pageName): iterable
{
    return [
        Field::new('name'),
        Field::new('description'),
        AssociationField::new('categories'),
    ];
}

When creating/editing a Product everything works as expected in the relation, but in the list of products instead of showing the related categories I see the number of categories the product has. How can I change this behaviour?

In the following image the first product has 1 category and the second one in the list has 2 different categories. I would like the name of the categories to be shown here.

enter image description here

As a side note: Category class has a __toString method returning the name of the category.

EDIT:

The behaviour I am looking for is the same as the Tags column in the following image:

enter image description here

like image 295
hosseio Avatar asked Dec 03 '22 17:12

hosseio


2 Answers

You can make a template for that like so:

// somewhere here templates/admin/field/category.html.twig
{% for category in field.value %}
  {%- set url = ea_url()
    .setController('Path\\To\\Your\\CategoryCrudController')
    .setAction('detail')
    .setEntityId(category.id)
  -%}
  <a href="{{ url }}">
    {{ category.name }}{% if not loop.last %}, {% endif %}
  </a>
{% else %}  
  <span class="badge badge-secondary">None</span>
{% endfor %}

And just add it to the field

// in ProductCrudController
AssociationField::new('categories')->setTemplatePath('admin/field/category.html.twig'),
like image 171
Flash Avatar answered Dec 06 '22 05:12

Flash


You can format the value using the method formatValue like this :

->formatValue(function ($value, $entity) {
                $str = $entity->getCategories()[0];
                for ($i = 1; $i < $entity->getCategories()->count(); $i++) {
                    $str = $str . ", " . $entity->getCategories()[$i];
                }
                return $str;
              })
like image 31
br-dev Avatar answered Dec 06 '22 07:12

br-dev