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'
Related
I have snippet like this in the init.sls:
{% for server, args in pillar.get('servers', {}).items() %}
software-server#{{ server }}
service.running:
- enable: true
- require:
- pkg: software_pkgs
- watch:
- file: software_config
/etc/software/{{server}}.json:
file.managed:
- source: salt://software/files/config.json.j2
- template: jinja
{% endfor %}
config.json.j2:
{
listen: {{server}}:{{listen_addr}}
}
and in the pillar:
software.servers:
server1:
listen_addr:10.0.0.1
server2:
listen_addr:127.0.01
in each of the {{server}}.json the listen_addr is different. I don't know if saltstack has something like a scope for current loop, or is there a workaround for this.
You probably need to use context or defaults options in file.managed:
file.managed
In your example it would like like :
/etc/software/{{server}}.json:
file.managed:
- source: salt://software/files/config.json.j2
- template: jinja
- context:
server: {{ server }}
listen_addr: {{ server['listen_addr'] }}
According to this http://symfony.com/doc/current/bundles/FOSUserBundle/overriding_templates.html
I can override FOSUserBundle templates in the app/Resources directory or create a child bundle.
Is there a way to place these templates into my own bundle directory instead?
I've tried adding:
twig:
debug: "%kernel.debug%"
strict_variables: "%kernel.debug%"
paths:
'%kernel.root_dir%/../src/AppBundle/Resources/FOSUserBundle/views/': FOSUSerBundle
to my config.yml
No, the template names are hardcoded inside the FOSUserBundle's controllers, thus you are not able to place them in your own bundle. (Unless you overwrite all controllers completely)
No, you can't. Template names are hardcoded in fosub controllers. But,
as a workaround you can include your own template in rewrited one. For sample, do it like this:
// app/Resources/FOSUserBundle/views/layout.html.twig
{% include '#YourBundleName/layout.html.twig' %}
I did template overriding for FosUserBundle in one of my project developed in symfony2.3 version.
Please try below code that may help you
config.yml
twig:
debug: "%kernel.debug%"
strict_variables: "%kernel.debug%"
FrontBundle/Controller/LoginController.php
<?php
namespace Dhi\UserBundle\Controller;
use FOS\UserBundle\Controller\SecurityController as Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Core\SecurityContextInterface;
use Symfony\Component\Security\Core\Exception\AuthenticationException;
use Symfony\Component\HttpFoundation\RedirectResponse;
class LoginController extends Controller
{
protected function loginController(array $data)
{
$request = $this->get('request');
return $this->render('FrontBundle:login:login.html.twig');
}
}
FrontBundle/Resources/views/login/login.html.twig
{% extends "DhiUserBundle::layout.html.twig" %}
{% trans_default_domain 'FOSUserBundle' %}
{% block body %}
{% block fos_user_content %}
{% trans_default_domain 'FOSUserBundle' %}
// Your login form code here
{% endblock fos_user_content %}
{% endblock body %}
You may look at your versions of FOSUser and Symfony and read this thread : https://github.com/FriendsOfSymfony/FOSUserBundle/issues/2439
I've experience this bug which didn't allow me to override FOS templates because they used (in latest version 2.0.0-beta) the namespaced syntax (the one with # symbol) to link templates. And because of it, before Symfony's 2.7.23 version, you can't override templates by putting the right named file in the same folders as described here : http://symfony.com/doc/master/bundles/FOSUserBundle/overriding_templates.html
I had the same issue today. My config.yml file is below:
# Twig Configuration
twig:
debug: '%kernel.debug%'
strict_variables: '%kernel.debug%'
paths:
"%kernel.root_dir%/../src/WebBundle/Resources/views/user/": FOSUser
I added all FOSUserBundle templates under WebBundle/Resources/views/user/ and made some extend/include changes in all templates and now it works.
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' %}
I have a route:
professor_identity_edit:
path: /professor/{id}/identity
defaults: { _controller: FrontBundle:ProfessorIdentity:edit }
How can I get the parameter {id} in twig.html file ?
I already tried app.request.query.get('id') but with no success.
Not a good thing but you can still do. Normally you would pass the info to Twig from the controller like #Maerlyn pointed out.
Get route parameters in Twig.
{{ app.request.attributes.get('_route_params') }}
AND
Gets whole bundle name in Twig.
{{ app.request.attributes.get("_controller") }}
Get route name in Twig.
{{ app.request.attributes.get('_route') }}
I found the solution:
{{ app.request.attributes.get('_route_params')['id'] }}
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