easyadminsymfony6

In Symfony EasyAdmin PAGE_INDEX can we pass value of one field to another


I'm trying to pass alternative text to image field.

if (Crud::PAGE_INDEX === $pageName) {
            return [$id, $image->setTemplatePath('easy_admin/block/_images_list.html.twig')->setCustomOptions(['alt' => (String) $title, 'width' => 50, 'action' => 'detail']),
                $entityVal, $mimeType, $size];
        }

Hint:- Here $title is another field such as $id,$mimeType etc.

ERROR

Object of class EasyCorp\Bundle\EasyAdminBundle\Field\TextField could not be converted to string

Question:- Can we pass value of one field to another in EASYADMIN PAGE_INDEX or do we have to hack the twig?


Solution

  • Yes I done this in twig.
    I have two entity Page and User.
    From PageEntity:-

        #[ORM\ManyToOne]
        #[ORM\JoinColumn(nullable: false)]
        private ?User $author = null;
    
        #[ORM\Column(length: 255, unique: true)]
        #[Assert\NotBlank()]
        #[Assert\Length(min: 3, max: 255)]
        private ?string $title = null;
    
        #[ORM\Column(length: 255)]
        #[Gedmo\Slug(fields: ["title"], updatable: false, separator: "-")]
        private ?string $slug = null;
    

    From UserEntity:-

        #[ORM\Column(length: 180, unique: true)]
        private ?string $email = null;
    
        #[ORM\Column(length: 45, unique: true)]
        #[Assert\NotBlank()]
        #[Assert\Length(min: 3, max: 45)]
        private ?string $username = null;
    
        #[ORM\Column(length: 64, unique: true)]
        #[Gedmo\Slug(fields: ['username'])]
        private ?string $slug = null;
    

    AdminCrud PAGE:-

    public function configureFields(string $pageName): iterable {
      $author = AssociationField::new('author');
      $slug = TextField::new('slug')->setRequired(false);
      $title = TextField::new('title');
      if (Crud::PAGE_INDEX === $pageName) {
        return [$title->setTemplatePath('easy_admin/block/_pages_list_title.html.twig'), $slug, $author]
      }
    }
    

    _pages_list_title.html.twig

    {% set editUrl = ea_url()
                            .setController(ea.crud.controllerFqcn)
                            .setAction('detail')
                            .setEntityId(entity.primaryKeyValue) %}
    
    <a href="{{ editUrl }}" id="Edit_{{ entity.primaryKeyValueAsString }}">    
        {{ '('~entity.primaryKeyValueAsString ~') '~field.formattedValue }}
    </a>
    // More details
    {{ field.label }} // This prints the current field label
    {{ entity.fields.getByProperty('author').value.email }}  // Prints author email
    

    For field refer EasyCorp\Bundle\EasyAdminBundle\Dto\FieldDto class and for entity refer EasyCorp\Bundle\EasyAdminBundle\Dto\EntityDto class.

    MORE DETAILS
    This entity.fields.getByProperty('author') is again EasyCorp\Bundle\EasyAdminBundle\Dto\FieldDto. Here 'author' is AssociationField so to get its field values just use entity.fields.getByProperty('author').value.username