Sonata Admin modify data in list view - symfony

I am trying to show/edit postgis point type. I am using creof/doctrine2-spatial package which provides some neat functions to get X and Y values for a point. The following works fine in edit/new form so the point is listed as 'Y X' (in this case "latitude longitude").
I am not sure if this is the correct way to accomplish what I need, but it works.
protected function configureFormFields(FormMapper $formMapper)
{
$formMapper
->add('name', 'text')
->add('coords', 'text', array(
'data'=>
$this->getSubject()->getCoords()->getLatitude() . ' ' .
$this->getSubject()->getCoords()->getLongitude()
));
}
However the problem is the list views. Because point is converted to string as "X Y" it prints the latitude and longitude in wrong order in list view. It prints as "longitude latitude" I am very new to sonata so I am not exactly sure how to solve the issue in list view.
Any ideas?
Update: Thanks to #kunicmarko20 I have resolved the issue:
So the file goes to app/Resources/views/SonataAdmin/CRUD/geography_point_list.html.twig
I decided to put the file to a reasonable folder.
The contents of the template is:
{% extends 'SonataAdminBundle:CRUD:base_list_field.html.twig' %}
{% block field %}
<div>
{{ object.coords.getLatitude }} {{ object.coords.getLongitude }}
</div>
{% endblock %}
The code for using the template was:
->add('coords', null, ['template' => 'SonataAdmin/CRUD/geography_point_list.html.twig']);
For some reason I couldn't get the : type path to work?

For your list view field, you can create custom template as explained here.
Example:
protected function configureListFields(ListMapper $listMapper)
{
$listMapper
->add('yourfiled', null, ['template' => 'AppBundle:Admin:your_field_list.html.twig'])
;
}
And your template would look like:
{% extends 'SonataAdminBundle:CRUD:base_list_field.html.twig' %}
{% block field %}
<div>
{{ object.longitude }} {{ object.latitude }}
</div>
{% endblock %}
The object here is your entity with all values.

Related

Concat field on custom column in Sonata Admin view

I'm trying to add a custom field to the list view in Sonata Admin where it will concat a fixed string (http://www.example.com) and the field slug as seen below, so that I can access that specific product url. The column shows in the table and the link is created, but I can't figure out how to pass the slug variable to the template so that it works.
I have the following configuration:
#ProductAdmin.php
protected function configureListFields(ListMapper $listMapper)
{
$listMapper->addIdentifier('id')
->addIdentifier('name')
->add('date')
->add('slug', 'text', [
'editable' => true
])
->add('link', 'string', [
'template' => 'default/admin-link.html.twig',
])
#default/admin-link.html.twig
{% extends 'SonataAdminBundle:CRUD:base_list_field.html.twig' %}
{% block field %}
Product link
{% endblock %}
In template you can access object which holds data of current iteration from loop just call {{object.slug}} in template to access slug for particular row like
{% extends 'SonataAdminBundle:CRUD:base_list_field.html.twig' %}
{% block field %}
Product link
{% endblock %}
Or it would be better if you generate your URLs using route/path method

Sonata Admin List Field Template is Ignored

I'm using Symfony 4.1.1 and Sonata Admin Bundle 3.35.2.
I want to use a custom template for a field in an admin's list view. The template is ignored. I am using Twig as my templating engine.
In the admin:
# /src/Admin/ImageAdmin.php
protected function configureListFields(ListMapper $listMapper) {
$listMapper
->add('filename', 'string', ['template' => 'list_image.html.twig'])
;
}
The template:
# /templates/list_image.html.twig
{% extends 'SonataAdminBundle:CRUD:base_list_field.html.twig' %}
{% block field %}
<img src="{{ value }}" style="width: 200px" />
{% endblock %}
Should be
# /templates/list_image.html.twig
{% extends 'SonataAdminBundle:CRUD:base_list_field.html.twig' %}
{% block field %}
<div>
<img src="{{ object.filename }}" style="width: 200px" />
</div>
{% endblock %}
SRC will be just filename - not full path for file, so image will not be printed. Fix that problem also.
The other problem is that, you accesed some mystical value? I don't see where you assign value to it.
You can access getters of object by writing object.fieldname. This one works as printing getter function of your current object.
I've had same problem (Symfony 4.1), try solution from here Use custom column in Sonata Admin list
so change:
{% extends 'SonataAdminBundle:CRUD:base_list_field.html.twig' %}
to:
{% extends '#SonataAdmin/CRUD/base_list_field.html.twig' %}
it did work for me. The problem is that even if your location is right (i got to it after some experiments) and you extend the wrong template you wont get any error.
Might be a bit late but for everyone who comes across with the same problem like me - I´ve solved it by creating a specific path within twig.yaml for admin-templates, simply create a subfolder _admin or try to use 'templates/': 'admin' to keep your files where they are (haven't tested this possibility)
# /config/packages/twig.yaml
twig:
paths:
'templates/_admin/': 'admin'
# /src/Admin/ImageAdmin.php
protected function configureListFields(ListMapper $listMapper) {
$listMapper
->add('filename', 'string', ['template' => '#admin/list_image.html.twig'])
;
}

Sonata admin bundle preview image from some entity in list mapper without sonata media bundle

I was wondering how should i approach this problem. In sonata admin dashboard i have users and i would like to preview users profile pictures in thumbnails of a ListMapper. I'm new to symfony and still a bit confused trying to wrap my head around these concepts.
You need to create a custom template where you display the image of your user, i will assume that your Entity User as a Picture Entity which has a path method that gives the image URL :
picture.html.twig
{% extends 'SonataAdminBundle:CRUD:base_list_field.html.twig' %}
{% block field %}
<div>
{% if object.picture != null %}
<img src="{{ object.picture.path }}">
{% else %}
<span>No picture</span>
{% endif %}
</div>
{% endblock %}
You know have to use this template in your list
class UserAdmin extends Admin
{
protected function configureListFields(ListMapper $listMapper)
{
$listMapper
->addIdentifier('id')
->add('picture', null, array(
'template' => 'ApplicationSonataAdminBundle:User:picture.html.twig'
));
}
}
Documentation is available here : http://sonata-project.org/bundles/doctrine-orm-admin/master/doc/reference/list_field_definition.html#custom-template
one separated entity, -no user-, where I have added one media, this for me worked
in admin configureListFields
->add('media', 'string', array('template' => 'SonataMediaBundle:MediaAdmin:list_image.html.twig'))

Printing all errors in one go within twig

I want to print all errors in one go for all the fields however {{ form_errors(form) }} doesn't print anything so because of that I have to use if not form.vars.valid statement to print errors however all the individual error messages are being wrapped within <ul><li>Message</li></ul> which is annoying. I know that 'error_bubbling' => true solves the issue but creates another issue which is making field borders red.
How can I solve this issue? I simply want to print error without tags.
Note: I can use {{ form_errors(form.name)|striptags }} but it adds overheads cos my form is massive.
FORM TYPE
class BrandsType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->setAction($options['action'])
->setMethod('POST')
->add('name', 'text', array('label' => 'Name'))
->add('button', 'submit', array('label' => 'Submit'))
;
}
}
TWIG
{% extends '::base.html.twig' %}
{% block body %}
{{ form_start(form, {attr: {novalidate:'novalidate'}}) }}
{% if not form.vars.valid %}
<div class="global_form_errors">
{{ form_errors(form.name) }}
{{ form_errors(form.origin) }}
</div><br />
{% endif %}
<div>
{{ form_label(form.name) }}
{% if form.name.vars.errors|length != '' %}
{{ form_widget(form.name, { attr: {'class': 'field_red_border'} }) }}
{% else %}
{{ form_widget(form.name) }}
{% endif %}
</div>
<div>
{{ form_widget(form.button) }}
</div>
{{ form_end(form) }}
{% endblock %}
Customize the field template
You can apply a form template to your form, check this documentation at Form Theming chapter.
If you prefer theming the field directly in your template, check the gray box in this documentation at the Global Form Theming chapter.
Check this documentation for more form customization.
Pass the errors to the template
When you submit the form, your code will call this method $form->handleRequest($request); which will check if the user input properly the data.
Based on the documentation of the Form class you can get a list of errors by calling $form->getErrors(). The variable $form is an instance of the Form class and not the FormView class. You may want to enable error bubbling.
Now that you know how to get the errors you can pass them as a variable to your template.

Raw filter on Sonata Admin Bundle configureShowFields

I'm doing a project with Symfony2 and Sonata Admin Bundle.
How I can apply the filter raw of twig (to display formated text) in action configureShowFields?
I would not override Sonata templates...
The code of my configureShowFields:
protected function configureShowFields(ShowMapper $showMapper)
{
$showMapper
->add('active')
->add('title')
->add('subtitle') // I need this field with twig RAW filter
->add('description') //I need this field with twig RAW filter
->add('url')
->add('date')
->add('tags')
->add('file');
}
You can use the "safe" sonata field option as follow:
protected function configureShowFields(ShowMapper $showMapper)
{
$showMapper
->add('subtitle', null, array('safe' => true))
;
}
It will add the "raw" twig filter to your entity field.
From the base_show_field.html.twig:
{% block field %}
{% if field_description.options.safe %}
{{ value|raw }}
{% else %}
{{ value|nl2br }}
{% endif %}
{% endblock %}
You need to make a custom template.
Under:
sonata_doctrine_orm_admin:
templates:
types:
list:
array: SonataAdminBundle:CRUD:list_array.html.twig
*** other existing declarations ***
raw: MyBundle:CRUD:raw.html.twig
Then make the template that the declaration maps to, and give 'raw' as the second argument to add field. It'll then call your new template to render that field.

Resources