I've got this entity, which contains entityName
property and entityId
property:
/**
* @var string
*
* @ORM\Column(name="entityName", type="string", length=255)
*/
private $entityName;
/**
* @var integer
* @ORM\Column(name="entityId", type="integer")
*/
private $entityId;
Instead of showing this entity using __toString()
function, I wanted to actually return the entity with name and id. and show that in sonata admin list view.
for now, here is __toString
:
public function __toString()
{
return $this->entityName . ":" . $this->entityId;
}
which should return something like:
public function __toString()
{
return $em->getRepository($this->entityName)->find($this->entityId);
}
I hope that I've described my problem well. tnx
One workaround is to use custom list block for sonata.
create a new twig filter called entityFilter
, this filter will convert FQCN of an sonata admin object to a readable route name generated by sonata. like admin_blablabla_show
:
public function entityFilter($entityName)
{
$str = str_replace('\\', '_', $entityName);
$str = str_replace('Bundle', '', $str);
$str = str_replace('_Entity', '', $str);
$str = 'Admin' . $str . '_Show';
return strtolower($str);
}
public function getName()
{
return 'my_extension';
}
in you admin class, set the template of the desired field to a new twig template:
->add('orderItems', null, array(
'template' => 'AcmeBundle::order_items_list.html.twig'
))
And in your new twig template (order_items_list.html.twig):
{% extends 'SonataAdminBundle:CRUD:base_list_field.html.twig' %}
{% block field %}
<div>
{% for item in object.orderItems %}
{% if entity(item.entityName) == 'admin_first_entity_show' %}
{% set foo = 'Apple ID' %}
{% elseif entity(item.entityName) == 'admin_second_entity_show' %}
{% set foo = 'Device Accessory' %}
{% else %}
{% set foo = 'Not defiend' %}
{% endif %}
<a target="_blank" class="btn btn-default btn-xs" href="{{ path(entity(item.entityName), {'id': item.entityId}) }}"><i class="fa fa-external-link"></i> {{ foo }}</a>
{% endfor %}
</div>
{% endblock %}