1
votes

I use "Knp Doctrine2 Behaviors" for entity translations and "A2LiX Translation Form" to translate entities in Sonata Admin form. Everything work fine. But I can’t realize what I should put in "configureDatagridFilters" and "configureListFields" methods of Admin class?

Naturally, for example, $datagridMapper->add('title'); doesn’t work because it is translatable field of my "Post" entity, and it is located in PostTranslation class, but Admin class is for Post entity. The __call method in Post entity doesn’t help, it seems it doesn’t work in admin class.

$datagridMapper->add('translations.title'); doesn't cause errors and there is row with entity, but there is nothing between <a> </a> tags:

enter image description here

Just in case, here is my Post, PostTranslation and PostAdmin classes:

/**
 * Blog post
 *
 * @ORM\Table(name="post")
 * @ORM\Entity(repositoryClass="AppBundle\Repository\PostRepository")
 */
class Post
{
    use ORMBehaviors\Translatable\Translatable;

    public function __call($method, $arguments)
    {
        return $this->proxyCurrentLocaleTranslation($method, $arguments);
    }

    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;


    /**
     * @ORM\ManyToOne(targetEntity="Category", inversedBy="posts", fetch="EAGER")
     * @ORM\JoinColumn(name="category_id", referencedColumnName="id")
     */
    private $category;


    public function __construct() {}

    /**
     * Get id
     *
     * @return integer
     */
    public function getId()
    {
        return $this->id;
    }


    public function setCategory(Category $category)
    {
        $category->addPost($this);
        $this->category = $category;
    }

    public function getCategory()
    {
        return $this->category;
    }
} 

/**
 * Post Translation
 * 
 * @ORM\Entity
 */
class PostTranslation
{

    use ORMBehaviors\Translatable\Translation;

    /**
     * @var string
     *
     * @ORM\Column(name="title", type="string", length=255)
     */
    private $title;

    /**
     * @var string
     *
     * @ORM\Column(name="content", type="string", length=255)
     */
    private $content;

    public function __construct() {}

    /**
     * Set title
     *
     * @param string $title
     *
     * @return post
     */
    public function setTitle($title)
    {
        $this->title = $title;

        return $this;
    }

    /**
     * Get title
     *
     * @return string
     */
    public function getTitle()
    {
        return $this->title;
    }

    public function setContent($content)
    {
        $this->content = $content;
    }

    public function getContent()
    {
        return $this->content;
    }

}

/**
 * Class PostAdmin
 */
class PostAdmin extends Admin
{
    protected function configureFormFields(FormMapper $formMapper)
    {
        $formMapper
            ->add('translations', 'a2lix_translations')
            ->add('category', 'entity', [
                'class' => 'AppBundle\Entity\Category',
                'property' => 'name'
            ])
        ;
    }

    protected function configureDatagridFilters(DatagridMapper $datagridMapper)
    {
        $datagridMapper->add('translations.title');
    }

    protected function configureListFields(ListMapper $listMapper)
    {
        $listMapper->addIdentifier('translations.title');
    }

}
1

1 Answers

0
votes

You need to add this code to your entity (not the translation one):

public function __call($method, $arguments)
{
    return \Symfony\Component\PropertyAccess\PropertyAccess::createPropertyAccessor()->getValue($this->translate(), $method);
}

Then on the configureListFields you can put 'title' or 'translate.title'.

There is more information here https://github.com/KnpLabs/DoctrineBehaviors#proxy-translations