Problems showing fields with Sonata Admin - Symfony 2.0

心不动则不痛 提交于 2019-12-04 21:20:28

I guess your best way to do this is to make custom setters and getters in your entity.

For example you have the entity user:

private $customState; // NOTE -> NO ORM MAPPING because you don't want an actual column

public function setCustomState()   {
         if($this->state){
            $this->customState = 'yup!';
            return $this;
         }
         $this->customState = 'nope!';
         return $this;
    }

public function getCustomState()   {
             return $this->customState;
        }

Thanks for the answer. However, I've been looking for information on Internet and I've found other way to do this. You can render a specific template for your field. In your Admin class, in the configureListFields function you could do this:

public function configureListFields(ListMapper $listMapper)
{
    $listMapper
        ->addIdentifier('name')
        ->add('notice')
        ->add('start')
        ->add('end')
        ->add('status', 'string', array('template' => 'YourBundle:YourController:status_field.html.twig'))
        ->add('Resources', 'string', array('template' => 'YourBundle:YourController:resources_field.html.twig'))
}

For the "status" field, the status_field.html.twig template will be rendered as below:

{% extends 'SonataAdminBundle:CRUD:base_list_field.html.twig' %}

{% block field %}
    <div>
        {% if object.status == 0 %} 
            <strong>Inactivo</strong>
        {% elseif object.status == 1 %}
            <strong>Activo</strong>
        {% endif %}
    </div>
{% endblock %}

And for my second question, the same solution is valid. The template rendered for the "resources" field would be as below:

{% extends 'SonataAdminBundle:CRUD:base_list_field.html.twig' %}

{% block field %}
    <div>
        <strong>{{ object.getResources|length }}</strong>
    </div>
{% endblock %}

In this way, the object is passed to the template, and you can use its methods to get the information that you need. In this case, the method getResources() is used to display its length.

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