How to print translatable data in sonata admin with DoctrineBehaviors from kpnlabs

为君一笑 提交于 2019-11-28 00:32:53

How to use Knp\DoctrineBehaviors magic proxy translations

Given you have MyClass and MyClassTranslation following the naming convention ( translation class suffixed with Translation ).

Only the properties which don't need to be translated live in MyClass and all translatable properties live in MyClassTranslation.

Let's say the translatable property shall be description.


MyClass.php

Attention: Neither property description nor getters/setters for description in MyClass .... otherwise __call() won't be invoked properly!

class MyClass
{

    use \Knp\DoctrineBehaviors\Model\Translatable\Translatable;

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

    protected $nonTranslatableProperty;

    // ...

MyClassTranslation.php

use Doctrine\ORM\Mapping as ORM;

class MyClassTranslation
{
    use \Knp\DoctrineBehaviors\Model\Translatable\Translation;


    /**
     * @var string
     */
    protected $description;

    /**
     * @return string
     */
    public function getDescription()
    {
        return $this->description;
    }

    /**
     * @param string $description
     *
     * @return MyClassTranslation
     */
    public function setDescription($description)
    {
        $this->description = $description;

        return $this;
    }

Now calling MyClass::getDescription() will invoke the magic method __call() which will return the translation using the current locale because there is no getDescription() method in MyClass .

Solution:

You should remove all the translatable getters/setters/properties present in SportTranslation from your Sport class and instead add the magic __call() method.

Tabzou

At this time it's the only way to print value in the admin list template.

If you want all translations you can simply add in your admin class of sport :

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

That way the result will depends on __toString function of SportTranslation class.

Otherwise, if you want to print the current translation, I guess you should use a custom template. For instance I will remove the getNom method in Sport.

Then in your admin class of sport :

protected function configureListFields(ListMapper $listMapper)
{
   $listMapper
       ->addIdentifier('translations', null, array(
           'template' => 'YourAdminBundle:CRUD:translatable.html.twig'
       ));
}

In your template

{% extends 'SonataAdminBundle:CRUD:base_list_field.html.twig' %}
{% block field%}
{{ object }}
{% endblock %}

That way it will call the __toString of your sport Class and it works without the getNom method.

Unfortunately, it does not correct my problem link to yours : How to sort translatable data in sonata admin using knplabs doctrine behavior

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!