Write Basic HTML in sonata edit Tab - symfony

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 %}

Related

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

Permanently extend symfony twig template

In my symfony2 application I created a dashboard which currently consists of many navigation elements.
Now I am trying to split those elements into several bundles.
This is the code I have:
{# app/Resources/views/base.html.twig #}
{# ... #}
{% block body %} {% endblock %}
{# ... #}
Then in the ProfileBundle:
{# src/MyApp/ProfileBundle/Resources/views/Dashboard/index.html.twig #}
{% block body %}
<p>Heading</p>
<ul>
{% block dashboardNavi %} {% endblock %}
</ul>
{% block %}
edit: The controller:
class DashboardController extends Controller
{
public function indexAction()
{
return $this->render('MyAppProfileBundle:Dashboard:index.html.twig', array());
}
}
The routing:
pricecalc_profile_dashboad_security:
pattern: /dashboard
defaults: {_controller: MyAppProfileBundle:Dashboard:index }
That template is rendered correctly, when my route "/dashboard" is loaded.
What I now'd like to do, is extend that dashboardNavi-Block in multiple Bundles without changing the route from my ProfileBundle.
Each of those Bundles brings it`s own routes and controllers for custom actions, but all bundles should extend that one block to add links for their custom actions to the dashboard screen.
What I have so far is:
{# src/MyApp/ProfileNewsletterBundle/Resources/views/Dashboard/indexNewsletter.html.twig #}
{% extends 'MyAppProfileBundle:Dashboard:index.html.twig' %}
{% block dashboardNavi %}
{{ parent() }}
<li>Test</li>
{% endblock %}
but that template is never rendered.
edit 2:
Maybe my understanding of how symfony is working in terms of template inheritance is kind of wrong. I'll specify what I am trying to do.
I got one Bundle (DashboardBundle) which consists of an own route, controller, view etc. The view contains two blocks - like navigation and dashboard.
Now, I would like to have those two blocks extended by some other Bundles - just adding new navigation items and shortcuts on that dashboard and navigation block.
I would like to do those enhancements without modifying my Dashboard-Bundle - if that is possible at all.
When finished, I will have 16 Bundles, each providing own functionality in own Controllers - and they should just be linked on that dashboard.
Is it possible to have the dashboard-view extended that way without modifying the view itself?
I finally managed to fix that after having understood how symfony works in extending controllers and views.
I added a new Controller:
{# src/MyApp/ProfileNewsletterBundle/Controllers/DashboardController.php #}
class DashboardController extends Controller {
public function indexAction()
{
return $this->render('ProfileNewsletterBundle:Dashboard:index.html.twig', array());
}
}
modified the bundle ProfileNewsletterBundle to let the method getParent return ProfileBundle,
and modified the view:
{% extends 'ProfileBundle:Dashboard:index.html.twig' %}
{% block dashboardNavi %}
<li>Test</li>
{% endblock %}
That seems to work fine so far.
Thank you all for spending your time on that.

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 %}

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'))

SonataAdminBundle custom rendering of text fields in list

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,
]);

Resources