Sonata admin non-deprecated method for setting templates? - symfony

The documentation for Sonata Admin Bundle offers this method for setting per-admin templates:
https://sonata-project.org/bundles/admin/2-0/doc/reference/templates.html
However that method has now been deprecated. Is there a new method, not mentioned yet in the documentation, or have the devs simply jumped the gun with this deprecation?

You can define your custom template for each admin by using the following code in your admin class.
public function configure()
{
$this->setTemplate('show', 'sonata_admin/show.html.twig');
// similarly for all actions
}
You must place your twig file in templates/sonata_admin/ folder, if you are using symfony 4.
The twig file must be like,
{% extends '#SonataAdmin/CRUD/show.html.twig' %}
{% block block_name %}
//your content
{% endblock %}

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

Use custom column in Sonata Admin list

I created a project with Symfony 4.1, and install Sonata Admin Bundle.
In a listing of my categories, I try to add a column which is not related to a field of Category
So I did
/* Admin/CategoryAdmin.php */
protected function configureListFields(ListMapper $listMapper)
{
$listMapper
->addIdentifier('name')
->add('test_column', 'string', [
'template' => 'template_test.html.twig',
]);
}
And my template.
{# templates/template_test.html.twig #}
{% extends 'SonataAdminBundle:CRUD:base_list_field.html.twig' %}
{% block field %}
TEST
{% endblock %}
The column is created, but it's empty. What did I do wrong?
Here's my test project: https://github.com/AntoineLemaire/sonata-admin-issue/commits/master
I had a other big project with Symfony 3.4 where it's working with no problem, so I created a fresh projet in 3.4, but I got the same issue.
No error message, juste blank for my column
---------- EDIT -----------
I had a better look, and it seams that the compiled template does not match my template
On my old big Symfony3.4 projet, compiled template is the same as template.
But I still don't know why
Ad yceruto said in the comments, the notation of my twig extends was not good:
{% extends 'SonataAdminBundle:CRUD:base_list_field.html.twig' %}
instead of
{% extends '#SonataAdmin/CRUD/base_list_field.html.twig' %}
This ist most likely a path problem. The tricky thing is, that the configureList function won't give you any error (other like in configureForm). It displays the column, tries to match a property in your object but left it empty if there is no property. Double-check your path. I think you are pointing to the wrong file path.
You write
{# templates/template_test.html.twig #}
but you point to
'template' => 'template_test.html.twig',
So sonata is looking for app/Resources/view/template_test.html.twig
but your comment say its anywhere in app/Resources/view/templates/template_test.html.twig or somewhere else.

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.

symfony2 - Change layout at runtime

Is possible in Symfony2 to change the layout (using twig) at runtime?
The layout should change based on a record fetched from a database, so I am thinking to implement a sort of LayoutManager that decides what layout to load for every request, but still I can't find a way to do this at runtime.
Twig template
{% extends myLayoutName %}
{% block my_block_with_content %}
Some content here
{% endblock %}
Controller
public function myAction() {
$layoutName = '...'; // Calculate layout name
return $this->render(
'AcmeAcmeBundle::template_name.html.twig',
['myLayoutName' => $layoutName]
);
}

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