SonataAdminBundle custom rendering of text fields in list - symfony

I'm using symfony2 and SonataAdminBundle.
I have a simple Entity called Post in which I have content field that is basically html text (from a ckeditor for the record). I need to display in the Post list the content field as raw html, without escaping it.
Hacking base_list_field template like this
{% block field %}{{ value|raw }}{% endblock %}
works, but it's clearly not the proper way.

The solution:
I've defined a custom html type in the config.yml for sonata_doctrine_orm_admin:
sonata_doctrine_orm_admin:
templates:
types:
list:
html: MyBundle:Default:list_html.html.twig
And created the custom list_html.html.twig template in which i do not escape HTML:
{% extends 'SonataAdminBundle:CRUD:base_list_field.html.twig' %}
{% block field%}
{{value|raw}}
{% endblock %}
Now in the PostAdmin I can define the behaviour of the field in the configureListFields method:
$listMapper
->add('content', 'html')

I know it's an old post that has an accepted answer, but now you can also use the safe option to tell Symfony not to sanitize the output.
$mapper->add('content', null, [
'safe' => true,
]);

Related

Write Basic HTML in sonata edit Tab

So in my sonata admin CMS, I'm trying to add a page called info
protected function configureFormFields(FormMapper $formMapper)
{
$formMapper
->tab('Info')
->end();
}
In my admin.yml I have this under calls:
calls:
- [ setTemplates, [{edit: admin\virtual-event-info.html.twig }]]
And then in a twig file I have this
{% extends 'SonataAdminBundle:CRUD:base_edit.html.twig' %}
<h1>I just want to write some basic static html out here</h1>
I just want to write out a basic static html page for FAQ's. I don't want to take any inputs or ask for any data, I just want a place for new people to know some basic info about that section of the CMS. Can anyone direct me on how to get that HTML to actually show under that tab?
It looks like you should put your html content inside the {% block form %}. That will replace the original content defined in the extended template (vendor/sonata-project/admin-bundle/src/Resources/views/CRUD/base_edit_form.html.twig).
{% extends 'SonataAdminBundle:CRUD:base_edit.html.twig' %}
{% block form %}
<h1>This replaces the base edit form block content</h1>
{% endblock %}

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'])
;
}

Require a Twig Block Be Defined in a Child

I have the <title> tag defined as a block in my base twig file and I want to make sure all my views override this block. Is there a way to mark the block as required so I get an error if I forget?
This isn't built into Twig (maybe you should make a feature request!)
There is one way I can think of, but it's not completely using blocks.
If you have a base.html.twig of, let's say for a quick example:
<title>{% block title %}{{ title }}{% endblock %}</title>
and you extend this block:
{% extends '::base.html.twig' %}
but don't declare a {% block title %} - then Twig will throw a notice in the development environment (and in the prod.log in the Production environment) about an unset variable. (You really shouldn't want Symfony to throw an error in Production for something trivial like this.)
Then there's two ways of "satisfying the requirement":
Pass a title variable into your extended Twig file
Override the title block with your own contents
Example 1, in your controller:
return $this->render('AcmeBundle:Extended:view.html.twig', array(
'title' => 'My fancy title'
));
Example 2, in your Twig file:
{% extends 'AcmeBundle::base.html.twig' %}
{% block title %}My fancy title{% endblock %}

How can I change symfony2 form fields default options globally?

Is there a way to change default options for form fields globally in symfony2?
More specifically, I want to change the render of ALL datetime fields to use single_text instead of the default choice widget.
Can it be done? Or do I need to implement a custom type and set the default in there, like for example the birthdate type?
I prefer an option that leads to minimal changes in the codebase.
The post is old, but you can use an alternative method, overriding the DateType symfony class ...
service.yml
services:
form.type.date:
class: "YourApp\YourBundle\Form\DateType"
tags:
- { name: "form.type", alias: "date" }
DateType.php
<?php
namespace YourApp\YourBundle\Form;
use Symfony\Component\Form\Extension\Core\Type\DateType as SymfonyDateType;
use Symfony\Component\OptionsResolver\OptionsResolver;
class DateType extends SymfonyDateType
{
/**
* {#inheritdoc}
*/
public function configureOptions(OptionsResolver $resolver)
{
parent::configureOptions( $resolver );
$resolver->setDefault( 'widget', 'single_text' );
}
}
You can check if the service is taken by container
$ ./app/console debug:container | grep form.type.date
form.type.date YourApp\YourBundle\Form\DateType
form.type.datetime Symfony\Component\Form\Extension\Core\Type\DateTimeType
You have to define a form theme.
It's very easy and requires only a little bit coding time. First of all, you have to know which block to customize; in that case, you can do something like
{% block my_data_widget %}
{% spaceless %}
{% if type is defined and type == 'date' %}
// do all your customization
{% else %}
// don't know ...
{% endif %}
{% endspaceless %}
{% endblock form_widget_simple %}
Now that you have defined this snippet of code, you can use it into your main template (or whatever you use into your form views) in that way
{% form_theme form 'YourBundle:Form:myDataWidget' %}
Last but not least important, you have to place your form theme into Resources/views folder. In my example, your path will be Resources/views/Form/myDataWidget
Update
Did you tried with
{% set type = type|default('single_text') %}
or something like that?

symfony2 - twig - how to render a twig template from inside a twig template

I have a xxx.html.twig file which shows a page, but when I want to refresh the page with different data and just update it with new data, I have a select and a submit button for it.
The thing is that I don't know how do I call an action in the controller which I pass parameters to from my twig and call for new data and then I render the same twig template again with new parameters.
How do I do so?
Here are a few different ways:
{{ render(app.request.baseUrl ~ '/helper/test', {"hostid2": hostid } ) }}
or
{% include 'MyCoreBundle:Helper:test.html.twig' with {"hostid2": hostid } only %}
or
{% render controller("MyCoreBundle:Helper:test", {'hostid2': hostid}) %}
Symfony 2.1:
{% render 'YourBundle:YourController:yourAction' with {'var': value} %}
Symfony 2.6+:
{{ render(controller('YourBundle:YourController:yourAction', {'var': value})) }}
And, of course, read the documentation.
I think some parts are depricated here.
To make the include work in latest Symfony 3.1.10, I solved it like this:
{% extends 'base.html.twig' %}
{% block body %}
{{ include('AppBundle:Default:inner_content.html.twig') }}
{% endblock %}
Note: include() with parentheses.
Then all the variables are included from the parent template. If you like to restrict some variables in the child template, you use with ... only (look over)

Resources