phpjquerysymfonytemplatessonata-admin

Custom sonata-admin template for an entityAdmin formMapper


I have an 'map' entity with fields 'latitude' and 'longitude'. These fields are completed from this jQuery plugin adapted.

While generating a Symfony2 standard CRUD works perfect (obviously modifying the default generated code) in sonata admin I can not integrate it. I have some problems with the template inheritance and with the template variables.

My plan is insert this admin in another EntityAdmin inline but I can not do it even for a single entity (MapAdmin for this case)

this is my MapAdmin

namespace Acme\MapBundle\Admin;

use Sonata\AdminBundle\Admin\Admin;
use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\AdminBundle\Datagrid\DatagridMapper;
use Sonata\AdminBundle\Form\FormMapper;

class MapAdmin extends Admin {

// Fields to be shown on create/edit forms
protected function configureFormFields(FormMapper $formMapper) {
    $formMapper

            ->add('descripcion', 'acme_map')
            ->add('latitude', 'acme_map')
            ->add('longitude', 'acme_map')
            ->add('zoom', 'acme_map')
    ;
}

protected function configureDatagridFilters(DatagridMapper $datagridMapper) {
    $datagridMapper
            ->add('descripcion', null, array('label' => 'Mapa'))
    ;
}

// Fields to be shown on lists
protected function configureListFields(ListMapper $listMapper) {
    $listMapper
            ->addIdentifier('descripcion')
    ;
}

public function getFormTheme() {

    return array_merge(
            parent::getFormTheme(), array(
        'AcmePlacesBundle:Resources:Views:Mapa:admincreate.html.twig')
    );
}}

...My registered as service form type... (alias: acme_map)

namespace Acme\PlacesBundle\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

class MapType extends AbstractType {
public function getParent()
    {
        return 'text';
    }


/**
 * @return string
 */
public function getName() {
    return 'acme_map';
}

}

...And my twig template

{% extends '::SonataAdminBundle:Form:form_admin_fields.html.twig' %}
{% block stylesheets %}
    {{ parent() }}

    <link rel="stylesheet" type="text/css" href="{{ asset('css/jquery-gmaps-latlon-picker.css') }}"/>

{% endblock %}

{% block javascripts %}
    {{ parent() }} #}
<script src="http://maps.googleapis.com/maps/api/js?sensor=false"></script>
<script src="{{ asset('js/jquery-gmaps-latlon-picker.js') }}"></script>
{% endblock %}

{% block acme_map_widget %}


<fieldset class="gllpLatlonPicker" id="custom_id">
    {# <input type="text" class="gllpSearchField"/> #}

    {{ form_errors(form.busqueda) }}
    {{ form_widget(form.busqueda) }}
    <input type="button" class="gllpSearchButton" value="search"/>
    <br/><br/>
    <div class="gllpMap">Google Maps</div>
    {# <input type="text" disabled class="gllpLatitude" value="-20"/> #}
    {{ form_errors(form.latitude) }}
    {{ form_widget(form.latitude) }}

    {# <input type="text" disabled class="gllpLongitude" value="-30"/> #}
    {{ form_errors(form.longitude) }}
    {{ form_widget(form.longitude) }}

    {#<input type="text" disabled class="gllpZoom" value="13"/>#}
    {{ form_errors(form.zoom) }}
    {{ form_widget(form.zoom) }}
</fieldset>
<br>


{% endblock %}

The errors are:

Template "SonataAdminBundle:Form:form_admin_fields.html.twig" cannot be used as a trait in AcmePlacesBundle:Mapa:admincreate.html.twig at line 1.

and i dont know how access to the fields cariables defined in MapAdmin.php in the twig template. {{ form.latitude }} don't work

Help Please!


Solution

  • You can't use extends in your custom template, extends only support single inheritance. The template form_admin_fields.html.twig of SonataAdmin already uses extends, so you must change extends by use in your twig template :

    {% use '::SonataAdminBundle:Form:form_admin_fields.html.twig' %}
    

    You should be able to access your form variables with {{ form.latitude }}.

    UPDATE

    For some reason the use doesn't work, to fix this you have to do simply :

    {% extends 'SonataAdminBundle:Form:form_admin_fields.html.twig' %}
    

    Your custom form will be used when you use your new type (acme_map).