I have an admin class of an object of class Car. This object is in relation with Person or Organisation.
I know how to add a column for Person and Organisation, and i have a link to the edit object
$listMapper
->add('person', null, array('admin_code' => 'appli.admin.person'))
->add('factory', null, array('admin_code' => 'appli.admin.factory'))
This create a link (
However i want just one column. I make this :
$listMapper->add('name',null,array('label'=>'Name','template'=>'AcmeBundle:Admin/Car/list_name.html.twig'))
But in my template i want something like this but i don't understand how make the link to the edit object :
{% extends 'SonataAdminBundle:CRUD:base_list_field.html.twig' %}
{% block field%}
{% if object.getPerson %}
{% set urlObject = 'linkToAdminEditPerson' %}
{% elseif object.getFactory %}
{% set urlObject = 'linkToAdminEditFactory' %}
{% endif %}
<a href="{{ urlObject }}">{{ object.name }}</a>
{% endblock %}
Thank You
Assuming both objects have a name
property:
{% extends 'SonataAdminBundle:CRUD:base_list_field.html.twig' %}
{% block field %}
{% if object.getPerson is defined %}
{% set objectRoute = 'person_edit' %}
{% elseif object.getFactory is defined %}
{% set objectRoute = 'factory_edit' %}
{% endif %}
<a href="{{ path(objectRoute, { id: object.id }) }}">{{ object.name }}</a>
{% endblock %}
Note that sonata admin bundle creates the edit route based on the $baseRouteName
property of the admin class, appending the action name.
Firstly use a custom template for your field:
$listMapper ->add('_action', 'actions', array( 'label' => 'Person', 'actions' => array('usage' => array('template' =>'MyBundleBundle:CRUD:edit_url.html.twig')) ) );
In admin class create a function that generates a sonata edit url:
public function generateEditUrl($name, $object, $parameters = array(), $absolute = false){ $classname = $object->getNode()->getPropertyValue('phpcr:class'); $admin = $this->getConfigurationPool()->getAdminByClass($classname); $url = $admin->generateObjectUrl($name, $object, $parameters, $absolute); return $url; }
Create "edit_url.html.twig" template
{% if object.person is defined and object.person is not empty %} <a href="{{ admin.generateEditUrl('edit', object.person, {}, true) }}" target="_blank" title="Edit {{ object.person.title }}"> <i class="icon-edit"></i>{{ object.person.title }} </a> {% endif %}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.