Sonata how to add to the menu - symfony

I want to build the menu in the left of Sonata like the picture below.
I have a controller in front end Symfony, how can I add the link to this menu

You can use configureSideMenu() function in your adminController
Refer this

Your layout should extend: "SonataAdminBundle::standard_layout.html.twig" and overwrite the "side_bar_nav" block from sonata. That's it.
{% extends 'SonataAdminBundle::standard_layout.html.twig' %}
{% block side_bar_nav %}
{% include 'MyBundleBundle:Default:menu.html.twig' %}
{% endblock %}
Change sonata template in app/admin/config.yml:
sonata_admin:
title: "Admin"
templates:
layout: MyBundleBundle:CRUD:standard_layout.html.twig
I hope this helps you.

I add my menu items via YourBundle\Bundle\Resources\config\services.yml
services:
acme_recept.admin.recept:
class: Acme\Bundle\YourBundle\Admin\ReceptAdmin
arguments: [~, Acme\Bundle\YourBundle\Entity\Recept, SkreprReceptBundle:ReceptAdmin]
tags:
- {name: sonata.admin, manager_type: orm, **group: Administrator**, **label: Recepten**}
Via tags you can give the label for the menu-item

Related

Overriding PriceHelper in Sylius

I am trying to override PriceHelper class by adding getOriginalPrice function but since the service not registered, on service.yml, I put:
services:
AppBundle\Helper\PriceHelper\:
class: AppBundle\Helper\PriceHelper
arguments:
- "#sylius.calculator.product_variant_price"
tags:
- { name: templating.helper, event: sylius.templating.helper.price, method: getOriginalPrice, alias: sylius_calculate_original_price }
On twig, I added:
{%- macro calculateOriginalPrice(variant) -%}
{% from _self import convertAndFormat %}
{{- convertAndFormat(variant|sylius_calculate_original_price({'channel': sylius.channel})) }}
{%- endmacro -%}
and replaced:
{{ money.calculatePrice(product|sylius_resolve_variant) }}
to
{{ money.calculateOriginalPrice(product|sylius_resolve_variant) }}
Error:
Unknown "sylius_calculate_original_price" filter. Did you mean
"sylius_calculate_price"?
Any idea?
Problem solved.
The services.yml is actually fine. Just need to add below in config.yml:
twig:
globals:
sylius_calculate_original_price: "#app.templating.helper.price"
and in twig:
{{ sylius_calculate_original_price.getOriginalPrice(variant,{'channel': sylius.channel}) }}
services.yml can be shorten to:
app.templating.helper.price:
decorates: sylius.templating.helper.price
class: AppBundle\Helper\PriceHelper
arguments:
- "#sylius.calculator.product_variant_price"
The PriceHelper service is configured in this file: https://github.com/Sylius/Sylius/blob/f7d42d2ce64288407372775e0ed421debcd50cd3/src/Sylius/Bundle/CoreBundle/Resources/config/services/templating.xml
But instead of replacing the service like you did, you should decorate it. Extend the PriceHelper class with a new class and add the functionality that you need, then add configuration for your new service like described in the following link, to decorate the PriceHelper service: http://symfony.com/doc/current/service_container/service_decoration.html
In your specific case, you have to use this configuration:
AppBundle\Helper\PriceHelper:
decorates: '#sylius.templating.helper.price'
arguments:
$productVariantPriceCalculator: '#sylius.calculator.product_variant_price'

How to owerride the 'show' template in the new version of Sonata Admin

How can I override the 'show' template in the new version of Sonata Admin? Before I extended in my template the base template:
{% extends 'SonataAdminBundle:CRUD:base_show.html.twig' %}
and overriden:
{% block show_field %} my content {% endblock %}.
But now this does not work. What template do I need to extend now?
P.S. I want to override a template only for one entity, so global override is not a solution for me.
Symfony version: 4.1.*
SonataAdmin version: ^3.39
This how I do it:
In config/services.yaml
admin.invoice:
class: App\Admin\InvoiceAdmin
arguments: [~, App\Entity\Invoice, ~]
tags:
- { name: sonata.admin, manager_type: orm, label: invoice, label_catalogue: App, label_translator_strategy: sonata.admin.label.strategy.noop }
calls:
- [ setTemplate, [show, "InvoiceAdmin/show.html.twig"]]
public: true
Then I copy vendor/sonata-project/admin-bundle/src/Resources/views/CRUD/base_show.html.twig to templates/InvoiceAdmin/show.html.twig.
Try if the base_show_macro template is what you are looking for:
vendor/sonata-project/admin-bundle/Resources/views/CRUD/base_show_macro.html.twig
or in twig:
{% extends 'SonataAdminBundle:CRUD:base_show_macro.html.twig' %}

Add new Button in the List view of sonata admin Bundle

Maybe the question is too easy to answer, but I searched in the documentation of sonata admin bundle and i didn't find what I need.
When you navigate to the List view of a model in sonata admin Bundle you find the Action Button in the upper right and under it you find the add new action.
In my case I need that the Add new action to be displayed directly In the View Like in this screenshot :
Any one can help me please ?
I know this is an old question, but I'll leave this answer for future reference.
I had to do something similar, this is how I did it.
In your admin class override the configureActionButtons() method
class YourAdmin extends AbstractAdmin
{
//...
/**
* Overriden from (AbstractAdmin)
*/
public function configureActionButtons($action, $object = null)
{
$list = parent::configureActionButtons($action,$object);
$list['custom_action'] = array(
'template' => 'AcmeBundle:YourAdmin:custom_button.html.twig',
);
return $list;
}
//...
}
And then creating your button in AcmeBundle/Resources/views/YourAdmin/custom_button.html.twig
{# custom_button.html.twig #}
<a class="sonata-action-element" href="{{ admin.generateUrl('your_route') }}">
<i class="fa fa-plus-circle"></i>Custom Action
</a>
Of course you can add permissions check afterwards (they were omitted for clarity)
Hope it helps somebody
In SonataAdmin ~3 you can configure the templates that display the template for your action directly in the YourEntityAdmin::configureListFields() method.
To do so you first
create new template anywhere, extend sonata layout and use sonata_admin_content block.
"Anywhere" really means "anywhere": this confused me a bit at first.
Basically, you create a template for example in Resources\CRUD\list__action_your_action.html.twig and then call it from the configuration in the YourEntityAdmin::configureListFields():
class YourEntityAdmin
{
protected function configureListFields(ListMapper $listMapper)
{
$listMapper
// other fields...
->add('_action', null, array(
'actions' => array(
// ...
'clone' => array(
'template' => 'AppBundle:CRUD:list__your_action.html.twig'
)
)
))
;
}
}
REMEMBER TO WRITE SOMETHING IN THIS TEMPLATE, ALSO A SIMPLE STRING LIKE
{# Resources\CRUD\list__action_your_action.html.twig #}
your action button template
"your action button template" will be rendered in the "Actions" column and is basically the label of your button: using the template you can manipulate it as you like.
This were really hard to understand as in my column there were no actions as I leaved the template empty so not rendering anything!
Anyway, you can find the full procedure to add a new button (and an associated route/action) in the documentation about CREATING A CUSTOM ADMIN ACTION.
Which Sonata version are you on ? And what is the version of your example?
Mine is 2.3, does not look like you can play on that from parameters, but you can override the layout to let only appear the create button.
If you need it for all your admins, you better override the layout from the config. If you need it only for list, or show, or remove only overrides those templates then.
in config.yml:
sonata_admin:
templates:
layout: AppBundle:Layouts:standard_layout_override.html.twig
show: AppBundle:Layouts:show.html.twig
list: AppBundle:Layouts:list.html.twig
delete: AppBundle:Layouts:delete.html.twig
edit: AppBundle:Layouts:edit.html.twig
in that file override that block:
{% block sonata_page_content_header %}
{% block sonata_page_content_nav %}
{% if _tab_menu is not empty or _actions is not empty %}
<nav class="navbar navbar-default" role="navigation">
{% block tab_menu_navbar_header %}
{% if _navbar_title is not empty %}
<div class="navbar-header">
<span class="navbar-brand">{{ _navbar_title|raw }}</span>
</div>
{% endif %}
{% endblock %}
<div class="container-fluid">
<div class="navbar-left">
{% if _tab_menu is not empty %}
{{ _tab_menu|raw }}
{% endif %}
</div>
{% if _actions|replace({ '<li>': '', '</li>': '' })|trim is not empty %}
<ul class="nav navbar-nav navbar-right">
<li class="dropdown sonata-actions">
{{ 'link_actions'|trans({}, 'SonataAdminBundle') }} <b class="caret"></b>
<ul class="dropdown-menu" role="menu">
{{ _actions|raw }}
</ul>
</li>
</ul>
{% endif %}
</div>
</nav>
{% endif %}
{% endblock sonata_page_content_nav %}
{% endblock sonata_page_content_header %}
You should also be able to do it for some admins only overriding defining a specific template for those admins on service definitions like that:
app.admin.product:
class: AppBundle\Admin\ProductAdmin
arguments: [~, AppBundle\Entity\Product, AppBundle:Admin\Product]
tags:
- {name: sonata.admin, manager_type: orm, group: Products, label: Products}
calls:
- [ setTemplate, [edit, AppBundle:Product:edit.html.twig]]
But I can't get that "AppBundle:Product:edit.html.twig" template to delete the list action overriding the same block.
Hope this helps you.

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.

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