sonata-project / SonataAdminBundle

The missing Symfony Admin Generator
https://docs.sonata-project.org/projects/SonataAdminBundle
MIT License
2.11k stars 1.26k forks source link

Changing TemplateRegistry service does not change TemplateRegistry in Admin classes #5522

Closed grekpg closed 5 years ago

grekpg commented 5 years ago

Environment

docker ubuntu

Sonata packages

composer show --latest 'sonata-project/*'
sonata-project/admin-bundle              3.48.0 3.48.0 The missing Symfony Admin Generator
sonata-project/block-bundle              3.15.0 3.15.0 Symfony SonataBlockBundle
sonata-project/cache                     1.1.1  2.0.1  Cache library
sonata-project/cache-bundle              2.4.2  3.0.1  This bundle provides caching services
sonata-project/core-bundle               3.16.2 3.16.2 Symfony SonataCoreBundle
sonata-project/datagrid-bundle           2.5.0  3.0.0  Symfony SonataDatagridBundle
sonata-project/doctrine-extensions       1.2.0  1.2.0  Doctrine2 behavioral extensions
sonata-project/doctrine-orm-admin-bundle 3.8.3  3.8.3  Symfony Sonata / Integrate Doctrine ORM into the SonataAdminBundle
sonata-project/easy-extends-bundle       2.5.0  2.5.0  Symfony SonataEasyExtendsBundle
sonata-project/exporter                  2.0.1  2.0.1  Lightweight Exporter library
sonata-project/notification-bundle       3.6.1  3.6.1  Symfony SonataNotificationBundle
sonata-project/page-bundle               3.10.0 3.10.0 This bundle provides a Site and Page management through container and block services
sonata-project/seo-bundle                2.7.0  2.7.0  Symfony SonataSeoBundle
sonata-project/translation-bundle        2.4.1  2.4.1  SonataTranslationBundle
sonata-project/user-bundle               4.3.0  4.3.0  Symfony SonataUserBundle

Symfony packages

symfony/acl-bundle           v1.0.0             v1.0.0             Symfony AclBundle
symfony/asset                v4.2.5             v4.2.5             Symfony Asset Component
symfony/browser-kit          v4.2.5             v4.2.5             Symfony BrowserKit Component
symfony/cache                v4.2.5             v4.2.5             Symfony Cache component with PSR-6, PSR-16, and tags
symfony/config               v4.2.5             v4.2.5             Symfony Config Component
symfony/console              v4.2.5             v4.2.5             Symfony Console Component
symfony/contracts            v1.0.2             v1.0.2             A set of abstractions extracted out of the Symfony components
symfony/css-selector         v4.2.5             v4.2.5             Symfony CssSelector Component
symfony/debug                v4.2.5             v4.2.5             Symfony Debug Component
symfony/debug-bundle         v4.2.5             v4.2.5             Symfony DebugBundle
symfony/debug-pack           v1.0.7             v1.0.7             A debug pack for Symfony projects
symfony/dependency-injection v4.2.5             v4.2.5             Symfony DependencyInjection Component
symfony/doctrine-bridge      v4.2.5             v4.2.5             Symfony Doctrine Bridge
symfony/dom-crawler          v4.2.5             v4.2.5             Symfony DomCrawler Component
symfony/dotenv               v4.2.5             v4.2.5             Registers environment variables from a .env file
symfony/event-dispatcher     v4.2.5             v4.2.5             Symfony EventDispatcher Component
symfony/expression-language  v4.2.5             v4.2.5             Symfony ExpressionLanguage Component
symfony/filesystem           v4.2.5             v4.2.5             Symfony Filesystem Component
symfony/finder               v4.2.5             v4.2.5             Symfony Finder Component
symfony/flex                 v1.2.1             v1.2.1             Composer plugin for Symfony
symfony/form                 v4.2.5             v4.2.5             Symfony Form Component
symfony/framework-bundle     v4.2.5             v4.2.5             Symfony FrameworkBundle
symfony/http-foundation      v4.2.5             v4.2.5             Symfony HttpFoundation Component
symfony/http-kernel          v4.2.5             v4.2.5             Symfony HttpKernel Component
symfony/inflector            v4.2.5             v4.2.5             Symfony Inflector Component
symfony/intl                 v4.2.5             v4.2.5             A PHP replacement layer for the C intl extension that includes additional data from the ICU library.
symfony/lts                  dev-master c1affae dev-master c1affae Enforces Long Term Supported versions of Symfony components
Package symfony/lts is abandoned, you should avoid using it. Use symfony/flex instead.
symfony/maker-bundle         v1.11.5            v1.11.5            Symfony Maker helps you create empty commands, controllers, form classes, tests and more so you can forget about writing boilerplate code.
symfony/monolog-bridge       v4.2.5             v4.2.5             Symfony Monolog Bridge
symfony/monolog-bundle       v3.3.1             v3.3.1             Symfony MonologBundle
symfony/options-resolver     v4.2.5             v4.2.5             Symfony OptionsResolver Component
symfony/orm-pack             v1.0.6             v1.0.6             A pack for the Doctrine ORM
symfony/panther              v0.3.0             v0.3.0             A browser testing and web scraping library for PHP and Symfony.
symfony/phpunit-bridge       v4.2.5             v4.2.5             Symfony PHPUnit Bridge
symfony/polyfill-ctype       v1.11.0            v1.11.0            Symfony polyfill for ctype functions
symfony/polyfill-intl-icu    v1.11.0            v1.11.0            Symfony polyfill for intl's ICU-related data and classes
symfony/polyfill-intl-idn    v1.11.0            v1.11.0            Symfony polyfill for intl's idn_to_ascii and idn_to_utf8 functions
symfony/polyfill-mbstring    v1.11.0            v1.11.0            Symfony polyfill for the Mbstring extension
symfony/polyfill-php72       v1.11.0            v1.11.0            Symfony polyfill backporting some PHP 7.2+ features to lower PHP versions
symfony/process              v4.2.5             v4.2.5             Symfony Process Component
symfony/profiler-pack        v1.0.4             v1.0.4             A pack for the Symfony web profiler
symfony/property-access      v4.2.5             v4.2.5             Symfony PropertyAccess Component
symfony/property-info        v4.2.5             v4.2.5             Symfony Property Info Component
symfony/routing              v4.2.5             v4.2.5             Symfony Routing Component
symfony/security             v4.2.5             v4.2.5             Symfony Security Component
symfony/security-acl         v3.0.1             v3.0.1             Symfony Security Component - ACL (Access Control List)
symfony/security-bundle      v4.2.5             v4.2.5             Symfony SecurityBundle
symfony/serializer           v4.2.5             v4.2.5             Symfony Serializer Component
symfony/serializer-pack      v1.0.2             v1.0.2             A pack for the Symfony serializer
symfony/stopwatch            v4.2.5             v4.2.5             Symfony Stopwatch Component
symfony/swiftmailer-bundle   v3.2.5             v3.2.5             Symfony SwiftmailerBundle
symfony/templating           v4.2.5             v4.2.5             Symfony Templating Component
symfony/test-pack            v1.0.5             v1.0.5             A pack for functional and end-to-end testing within a Symfony app
symfony/translation          v4.2.5             v4.2.5             Symfony Translation Component
symfony/twig-bridge          v4.2.5             v4.2.5             Symfony Twig Bridge
symfony/twig-bundle          v4.2.5             v4.2.5             Symfony TwigBundle
symfony/validator            v4.2.5             v4.2.5             Symfony Validator Component
symfony/var-dumper           v4.2.5             v4.2.5             Symfony mechanism for exploring and dumping PHP variables
symfony/var-exporter         v4.2.5             v4.2.5             A blend of var_export() + serialize() to turn any serializable data structure to plain PHP code
symfony/web-link             v4.2.5             v4.2.5             Symfony WebLink Component
symfony/web-profiler-bundle  v4.2.5             v4.2.5             Symfony WebProfilerBundle
symfony/web-server-bundle    v4.2.5             v4.2.5             Symfony WebServerBundle
symfony/webpack-encore-pack  v1.0.3             v1.0.3             A pack for Symfony Encore
symfony/yaml                 v4.2.5             v4.2.5             Symfony Yaml Component

PHP version

$ php -v
PHP 7.3.2-3+ubuntu16.04.1+deb.sury.org+1 (cli) (built: Feb  8 2019 15:43:26) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.3.2, Copyright (c) 1998-2018 Zend Technologies
    with Zend OPcache v7.3.2-3+ubuntu16.04.1+deb.sury.org+1, Copyright (c) 1999-2018, by Zend Technologies

Subject

I register my own global_template_registry - I need switch template depends of logged user. (I implement bootsrap 4 its ready for simple part, rest admins must use v3 standard layout.

sonata.admin.global_template_registry:
        class: App\Service\SonataTemplateRegistry
        public: true
        arguments: ['%sonata.admin.configuration.templates%', '@mea.contact']

parameters:
    sonata.admin.global_template_registry: App\Service\SonataTemplateRegistry

in any admin class

  protected function configureListFields(ListMapper $listMapper)
    {

        var_dump($this->getTemplateRegistry());

i get Sonata\AdminBundle\Templating\TemplateRegistry not like i setup App\Service\SonataTemplateRegistry

I found in vendor/sonata-project/admin-bundle/src/DependencyInjection/Compiler/AddDependencyCallsCompilerPass.php

public function fixTemplates(

...

 $templateRegistryDefinition = $container
            ->register($templateRegistryId, TemplateRegistry::class)
this put hardcoded TemplateRegistry::class 

Expected results

load correct global template registry

Here is stackoverflow question about this https://stackoverflow.com/questions/55530510/sonata-admin-override-template-depend-of-user

kunicmarko20 commented 5 years ago
parameters:
    sonata.admin.global_template_registry: App\Service\SonataTemplateRegistry

this won't work because we aren't using parameters to define class of the global_template_registry:

https://github.com/sonata-project/SonataAdminBundle/blob/d6d0a50277dbe71a2053fe50334e58be2dcf8b30/src/Resources/config/core.xml#L85

Alternative is to decorate it https://symfony.com/doc/current/service_container/service_decoration.html

grekpg commented 5 years ago

Thanks for respond.

i try this:

    mea.sonata.admin.global_template_registry:
        class: App\Service\SonataTemplateRegistry
        decoration_inner_name: Sonata\AdminBundle\Templating\TemplateRegistry
        decorates: sonata.admin.global_template_registry
        public: true
        arguments: ['%sonata.admin.configuration.templates%', '@mea.contact']

also i try manualy put to admin my template registry

  app.admin.social.text.templates:
    class: App\SocialManager\Admin\TextTemplatesAdmin
    arguments: [~, App\SocialManager\Entity\TextTemplate,~]
    tags:
      - { name: sonata.admin, manager_type: orm, label: "Text Templates" }
    public: true
    calls:
      - [ setTemplateRegistry, ['@mea.sonata.admin.global_template_registry']]

but still in admin

 protected function configureListFields(ListMapper $listMapper)
    {

        dump($this->getTemplateRegistry());

give my Sonata\AdminBundle\Templating\TemplateRegistry

kunicmarko20 commented 5 years ago

Did you try decorating https://github.com/sonata-project/SonataAdminBundle/blob/72f55b2f6cf34b3e7f6e5a6ca0bead0cc4629ad5/src/DependencyInjection/Compiler/AddDependencyCallsCompilerPass.php#L405

grekpg commented 5 years ago

I try decorate

 decoration_inner_name: Sonata\AdminBundle\Templating\TemplateRegistry
        decorates: sonata.admin.global_template_registry

like that

mea.sonata.admin.global_template_registry:
        class: App\Service\SonataTemplateRegistry
        decoration_inner_name: Sonata\AdminBundle\Templating\TemplateRegistry
        decorates: sonata.admin.global_template_registry
        public: true
        arguments: ['%sonata.admin.configuration.templates%', '@mea.contact']

You ask that i try decorate - template registry admin child - no , because i need globaly change it so in this way i need to do this for all admins. But why

calls:
      - [ setTemplateRegistry, ['@mea.sonata.admin.global_template_registry']]

Also dont work ? I correct try decorate main template registry ?

grekpg commented 5 years ago

but yes - this work

    mea.sonata.admin.global_template_registry:
        class: App\Service\SonataTemplateRegistry
        decorates: app.admin.social.text.templates.template_registry
        public: true
        arguments: ['%sonata.admin.configuration.templates%', '@mea.contact']

but this is not global, is possible to globally set template registry without creating new template registry for all admin classes ?

Kalyse commented 3 years ago

FYI: this is still an issue..

is possible to globally set template registry without creating new template registry for all admin classes

This is exactly what I've been attempting to do. I want to pass a different TemplateRegistry per Admin however it's not possible. Every AdminClass despite getting set, is then injected witht he default TemplateRegistry.

wbloszyk commented 3 years ago

In SonataAdminBundle 3.x template registry is auto generated by AddDependencyCallsCompilerPass with global (sonata) configuration. Evan if you call setTemplateRegistry the registry will be override.

I working on it and it should be done in #6566 (Auto generate will be done when setTemplateRegistry is not call).

For now you can create own compiler pass to override template registry or set it after containier configuration.