From 11b4d76179122c163d4323f9ab05f027ac9ad92e Mon Sep 17 00:00:00 2001 From: Vincent Langlet Date: Fri, 16 Apr 2021 11:28:38 +0200 Subject: [PATCH 1/4] Add default_admin_services node --- .../recipe_overwrite_admin_configuration.rst | 49 +++++++++---------- docs/reference/configuration.rst | 2 +- .../AddDependencyCallsCompilerPass.php | 7 ++- src/DependencyInjection/Configuration.php | 38 ++++++++++++++ .../SonataAdminExtension.php | 2 + .../DependencyInjection/ConfigurationTest.php | 41 ++++++++++++++++ 6 files changed, 111 insertions(+), 28 deletions(-) diff --git a/docs/cookbook/recipe_overwrite_admin_configuration.rst b/docs/cookbook/recipe_overwrite_admin_configuration.rst index 9205d37575..a974a92fd6 100644 --- a/docs/cookbook/recipe_overwrite_admin_configuration.rst +++ b/docs/cookbook/recipe_overwrite_admin_configuration.rst @@ -12,30 +12,29 @@ with the following templates: sonata_admin: admin_services: - id.of.admin.service: - # service configuration - model_manager: sonata.admin.manager.orm - data_source: sonata.admin.data_source.orm - form_contractor: sonata.admin.builder.orm_form - show_builder: sonata.admin.builder.orm_show - list_builder: sonata.admin.builder.orm_list - datagrid_builder: sonata.admin.builder.orm_datagrid - translator: translator - configuration_pool: sonata.admin.pool - route_generator: sonata.admin.route.default_generator - validator: validator - security_handler: sonata.admin.security.handler - menu_factory: knp_menu.factory - route_builder: sonata.admin.route.path_info - label_translator_strategy: sonata.admin.label.strategy.native + # service configuration + model_manager: sonata.admin.manager.orm + data_source: sonata.admin.data_source.orm + form_contractor: sonata.admin.builder.orm_form + show_builder: sonata.admin.builder.orm_show + list_builder: sonata.admin.builder.orm_list + datagrid_builder: sonata.admin.builder.orm_datagrid + translator: translator + configuration_pool: sonata.admin.pool + route_generator: sonata.admin.route.default_generator + validator: validator + security_handler: sonata.admin.security.handler + menu_factory: knp_menu.factory + route_builder: sonata.admin.route.path_info + label_translator_strategy: sonata.admin.label.strategy.native - # templates configuration - templates: - # view templates - view: - user_block: mytemplate.twig.html - # form related theme templates => this feature need to be implemented by the Persistency layer of each Admin Bundle - form: ['MyTheme.twig.html', 'MySecondTheme.twig.html'] - filter: ['MyTheme.twig.html', 'MySecondTheme.twig.html'] + # templates configuration + templates: + # view templates + view: + user_block: mytemplate.twig.html + # form related theme templates => this feature need to be implemented by the Persistency layer of each Admin Bundle + form: ['MyTheme.twig.html', 'MySecondTheme.twig.html'] + filter: ['MyTheme.twig.html', 'MySecondTheme.twig.html'] -With these settings you will be able to change default services and templates used by the ``id.of.admin.service`` admin instance. +With these settings you will be able to change default services and templates used by the admin instance. diff --git a/docs/reference/configuration.rst b/docs/reference/configuration.rst index 6fdc59e0a6..7e8155178f 100644 --- a/docs/reference/configuration.rst +++ b/docs/reference/configuration.rst @@ -119,7 +119,7 @@ Full Configuration Options id: ~ position: right class: col-md-4 - admin_services: + default_admin_services: model_manager: null data_source: null form_contractor: null diff --git a/src/DependencyInjection/Compiler/AddDependencyCallsCompilerPass.php b/src/DependencyInjection/Compiler/AddDependencyCallsCompilerPass.php index c4b4508e91..15ebb8faab 100644 --- a/src/DependencyInjection/Compiler/AddDependencyCallsCompilerPass.php +++ b/src/DependencyInjection/Compiler/AddDependencyCallsCompilerPass.php @@ -265,7 +265,7 @@ public function applyConfigurationFromAttribute(Definition $definition, array $a 'datagrid_builder', 'translator', 'configuration_pool', - 'router', + 'route_generator', 'validator', // NEXT_MAJOR: Remove this line 'security_handler', 'menu_factory', @@ -296,13 +296,16 @@ public function applyConfigurationFromAttribute(Definition $definition, array $a public function applyDefaults(ContainerBuilder $container, $serviceId, array $attributes = []) { $definition = $container->getDefinition($serviceId); + // NEXT_MAJOR: Remove this line. $settings = $container->getParameter('sonata.admin.configuration.admin_services'); $definition->setShared(false); $managerType = $attributes['manager_type']; - $overwriteAdminConfiguration = $settings[$serviceId] ?? []; + // NEXT_MAJOR: Remove this line and uncomment the following + $overwriteAdminConfiguration = $settings[$serviceId] ?? $container->getParameter('sonata.admin.configuration.default_admin_services'); +// $overwriteAdminConfiguration = $container->getParameter('sonata.admin.configuration.default_admin_services'); $defaultAddServices = [ 'model_manager' => sprintf('sonata.admin.manager.%s', $managerType), diff --git a/src/DependencyInjection/Configuration.php b/src/DependencyInjection/Configuration.php index 6d2a10c30a..708d26579e 100644 --- a/src/DependencyInjection/Configuration.php +++ b/src/DependencyInjection/Configuration.php @@ -344,11 +344,49 @@ public function getConfigTreeBuilder() ->end() ->end() ->end() + ->arrayNode('default_admin_services') + ->addDefaultsIfNotSet() + ->children() + ->scalarNode('model_manager')->defaultNull()->end() + ->scalarNode('data_source')->defaultNull()->end() + ->scalarNode('field_description_factory')->defaultNull()->end() + ->scalarNode('form_contractor')->defaultNull()->end() + ->scalarNode('show_builder')->defaultNull()->end() + ->scalarNode('list_builder')->defaultNull()->end() + ->scalarNode('datagrid_builder')->defaultNull()->end() + ->scalarNode('translator')->defaultNull()->end() + ->scalarNode('configuration_pool')->defaultNull()->end() + ->scalarNode('route_generator')->defaultNull()->end() + ->scalarNode('security_handler')->defaultNull()->end() + ->scalarNode('menu_factory')->defaultNull()->end() + ->scalarNode('route_builder')->defaultNull()->end() + ->scalarNode('label_translator_strategy')->defaultNull()->end() + ->scalarNode('pager_type')->defaultNull()->end() + ->arrayNode('templates') + ->addDefaultsIfNotSet() + ->children() + ->arrayNode('form') + ->prototype('scalar')->end() + ->end() + ->arrayNode('filter') + ->prototype('scalar')->end() + ->end() + ->arrayNode('view') + ->useAttributeAsKey('id') + ->prototype('scalar')->end() + ->end() + ->end() + ->end() + ->end() + ->end() + // NEXT_MAJOR: Remove this node. ->arrayNode('admin_services') + ->setDeprecated('The "%node%" option is deprecated since sonata-project/admin-bundle 3.x.') ->prototype('array') ->children() ->scalarNode('model_manager')->defaultNull()->end() ->scalarNode('data_source')->defaultNull()->end() + ->scalarNode('field_description_factory')->defaultNull()->end() ->scalarNode('form_contractor')->defaultNull()->end() ->scalarNode('show_builder')->defaultNull()->end() ->scalarNode('list_builder')->defaultNull()->end() diff --git a/src/DependencyInjection/SonataAdminExtension.php b/src/DependencyInjection/SonataAdminExtension.php index 9abbe445dd..864a017107 100644 --- a/src/DependencyInjection/SonataAdminExtension.php +++ b/src/DependencyInjection/SonataAdminExtension.php @@ -122,6 +122,8 @@ public function load(array $configs, ContainerBuilder $container) $container->setParameter('sonata.admin.configuration.global_search.empty_boxes', $config['global_search']['empty_boxes']); $container->setParameter('sonata.admin.configuration.global_search.case_sensitive', $config['global_search']['case_sensitive']); $container->setParameter('sonata.admin.configuration.templates', $config['templates']); + $container->setParameter('sonata.admin.configuration.default_admin_services', $config['default_admin_services']); + // NEXT_MAJOR: Remove this line. $container->setParameter('sonata.admin.configuration.admin_services', $config['admin_services']); $container->setParameter('sonata.admin.configuration.default_controller', $config['default_controller']); $container->setParameter('sonata.admin.configuration.dashboard_groups', $config['dashboard']['groups']); diff --git a/tests/DependencyInjection/ConfigurationTest.php b/tests/DependencyInjection/ConfigurationTest.php index d2c92c2abc..a73222fa25 100644 --- a/tests/DependencyInjection/ConfigurationTest.php +++ b/tests/DependencyInjection/ConfigurationTest.php @@ -58,6 +58,11 @@ public function testOptionsWithInvalidFormat(): void ]]); } + /** + * NEXT_MAJOR: Remove this test. + * + * @group legacy + */ public function testCustomTemplatesPerAdmin(): void { $config = $this->process([[ @@ -75,6 +80,11 @@ public function testCustomTemplatesPerAdmin(): void $this->assertSame('@SonataAdmin/mycustomtemplate.html.twig', $config['admin_services']['my_admin_id']['templates']['view']['user_block']); } + /** + * NEXT_MAJOR: Remove this test. + * + * @group legacy + */ public function testAdminServicesDefault(): void { $config = $this->process([[ @@ -84,6 +94,7 @@ public function testAdminServicesDefault(): void $this->assertSame([ 'model_manager' => null, 'data_source' => null, + 'field_description_factory' => null, 'form_contractor' => null, 'show_builder' => null, 'list_builder' => null, @@ -106,6 +117,36 @@ public function testAdminServicesDefault(): void ], $config['admin_services']['my_admin_id']); } + public function testDefaultAdminServicesDefault(): void + { + $config = $this->process([[ + 'default_admin_services' => [], + ]]); + + $this->assertSame([ + 'model_manager' => null, + 'data_source' => null, + 'field_description_factory' => null, + 'form_contractor' => null, + 'show_builder' => null, + 'list_builder' => null, + 'datagrid_builder' => null, + 'translator' => null, + 'configuration_pool' => null, + 'route_generator' => null, + 'security_handler' => null, + 'menu_factory' => null, + 'route_builder' => null, + 'label_translator_strategy' => null, + 'pager_type' => null, + 'templates' => [ + 'form' => [], + 'filter' => [], + 'view' => [], + ], + ], $config['default_admin_services']); + } + public function testDashboardWithoutRoles(): void { $config = $this->process([]); From 5631e88d3a2073a414a724bd097b4f337404d06c Mon Sep 17 00:00:00 2001 From: Vincent Langlet Date: Sun, 2 May 2021 13:38:03 +0200 Subject: [PATCH 2/4] Move form/filter config --- .../AddDependencyCallsCompilerPass.php | 13 ++++++++++-- src/DependencyInjection/Configuration.php | 21 ++++++------------- .../DependencyInjection/ConfigurationTest.php | 5 ----- .../SonataAdminExtensionTest.php | 2 ++ 4 files changed, 19 insertions(+), 22 deletions(-) diff --git a/src/DependencyInjection/Compiler/AddDependencyCallsCompilerPass.php b/src/DependencyInjection/Compiler/AddDependencyCallsCompilerPass.php index 15ebb8faab..057a6c94a2 100644 --- a/src/DependencyInjection/Compiler/AddDependencyCallsCompilerPass.php +++ b/src/DependencyInjection/Compiler/AddDependencyCallsCompilerPass.php @@ -389,11 +389,20 @@ public function applyDefaults(ContainerBuilder $container, $serviceId, array $at $definition->addMethodCall('setSecurityInformation', ['%sonata.admin.configuration.security.information%']); } + $defaultTemplates = $container->getParameter('sonata.admin.configuration.templates'); + \assert(\is_array($defaultTemplates)); + if (!$definition->hasMethodCall('setFormTheme')) { - $definition->addMethodCall('setFormTheme', [$overwriteAdminConfiguration['templates']['form'] ?? []]); + // NEXT_MAJOR: Remove this line and uncomment the following one. + $formTheme = $overwriteAdminConfiguration['templates']['form'] ?? $defaultTemplates['form_theme'] ?? []; +// $formTheme = $defaultTemplates['form_theme'] ?? []; + $definition->addMethodCall('setFormTheme', [$formTheme]); } if (!$definition->hasMethodCall('setFilterTheme')) { - $definition->addMethodCall('setFilterTheme', [$overwriteAdminConfiguration['templates']['filter'] ?? []]); + // NEXT_MAJOR: Remove this line and uncomment the following one. + $filterTheme = $overwriteAdminConfiguration['templates']['filter'] ?? $defaultTemplates['filter_theme'] ?? []; +// $filterTheme = $defaultTemplates['filter_theme'] ?? []; + $definition->addMethodCall('setFilterTheme', [$filterTheme]); } return $definition; diff --git a/src/DependencyInjection/Configuration.php b/src/DependencyInjection/Configuration.php index 708d26579e..944d3c6dad 100644 --- a/src/DependencyInjection/Configuration.php +++ b/src/DependencyInjection/Configuration.php @@ -362,21 +362,6 @@ public function getConfigTreeBuilder() ->scalarNode('route_builder')->defaultNull()->end() ->scalarNode('label_translator_strategy')->defaultNull()->end() ->scalarNode('pager_type')->defaultNull()->end() - ->arrayNode('templates') - ->addDefaultsIfNotSet() - ->children() - ->arrayNode('form') - ->prototype('scalar')->end() - ->end() - ->arrayNode('filter') - ->prototype('scalar')->end() - ->end() - ->arrayNode('view') - ->useAttributeAsKey('id') - ->prototype('scalar')->end() - ->end() - ->end() - ->end() ->end() ->end() // NEXT_MAJOR: Remove this node. @@ -463,6 +448,12 @@ public function getConfigTreeBuilder() ->scalarNode('button_history')->defaultValue('@SonataAdmin/Button/history_button.html.twig')->cannotBeEmpty()->end() ->scalarNode('button_list')->defaultValue('@SonataAdmin/Button/list_button.html.twig')->cannotBeEmpty()->end() ->scalarNode('button_show')->defaultValue('@SonataAdmin/Button/show_button.html.twig')->cannotBeEmpty()->end() + ->arrayNode('form_theme') + ->prototype('scalar')->end() + ->end() + ->arrayNode('filter_theme') + ->prototype('scalar')->end() + ->end() ->end() ->end() diff --git a/tests/DependencyInjection/ConfigurationTest.php b/tests/DependencyInjection/ConfigurationTest.php index a73222fa25..21782792ad 100644 --- a/tests/DependencyInjection/ConfigurationTest.php +++ b/tests/DependencyInjection/ConfigurationTest.php @@ -139,11 +139,6 @@ public function testDefaultAdminServicesDefault(): void 'route_builder' => null, 'label_translator_strategy' => null, 'pager_type' => null, - 'templates' => [ - 'form' => [], - 'filter' => [], - 'view' => [], - ], ], $config['default_admin_services']); } diff --git a/tests/DependencyInjection/SonataAdminExtensionTest.php b/tests/DependencyInjection/SonataAdminExtensionTest.php index 8e97c5d6e6..5a4dfb0dca 100644 --- a/tests/DependencyInjection/SonataAdminExtensionTest.php +++ b/tests/DependencyInjection/SonataAdminExtensionTest.php @@ -368,6 +368,8 @@ public function testDefaultTemplates(): void 'button_history' => '@SonataAdmin/Button/history_button.html.twig', 'button_list' => '@SonataAdmin/Button/list_button.html.twig', 'button_show' => '@SonataAdmin/Button/show_button.html.twig', + 'form_theme' => [], + 'filter_theme' => [], ], $this->container->getParameter('sonata.admin.configuration.templates')); } From 4f70788cdaf993b7626d061103896ab09e7b6677 Mon Sep 17 00:00:00 2001 From: Vincent Langlet Date: Fri, 7 May 2021 01:34:23 +0200 Subject: [PATCH 3/4] Update doc --- .../recipe_overwrite_admin_configuration.rst | 35 ++++++++++++------- 1 file changed, 23 insertions(+), 12 deletions(-) diff --git a/docs/cookbook/recipe_overwrite_admin_configuration.rst b/docs/cookbook/recipe_overwrite_admin_configuration.rst index a974a92fd6..4329e0d0e7 100644 --- a/docs/cookbook/recipe_overwrite_admin_configuration.rst +++ b/docs/cookbook/recipe_overwrite_admin_configuration.rst @@ -5,16 +5,17 @@ Sometimes you might want to overwrite some Admin settings from vendors. This recipe will explain how to achieve this operation. However, keep in mind this operation is quite dangerous and might break code. -From the configuration file, you can add a new section named ``admin_services`` +From the configuration file, you can add a new section named ``default_admin_services`` with the following templates: .. code-block:: yaml sonata_admin: - admin_services: + default_admin_services: # service configuration model_manager: sonata.admin.manager.orm data_source: sonata.admin.data_source.orm + field_description_factory: sonata.admin.field_description_factory.orm form_contractor: sonata.admin.builder.orm_form show_builder: sonata.admin.builder.orm_show list_builder: sonata.admin.builder.orm_list @@ -22,19 +23,29 @@ with the following templates: translator: translator configuration_pool: sonata.admin.pool route_generator: sonata.admin.route.default_generator - validator: validator security_handler: sonata.admin.security.handler menu_factory: knp_menu.factory route_builder: sonata.admin.route.path_info label_translator_strategy: sonata.admin.label.strategy.native + pager_type: default - # templates configuration - templates: - # view templates - view: - user_block: mytemplate.twig.html - # form related theme templates => this feature need to be implemented by the Persistency layer of each Admin Bundle - form: ['MyTheme.twig.html', 'MySecondTheme.twig.html'] - filter: ['MyTheme.twig.html', 'MySecondTheme.twig.html'] +With these settings you will be able to change default services and templates used by the admin instances. -With these settings you will be able to change default services and templates used by the admin instance. +If you need to override the service of a specific admin, you can do it during the service declaration: + +.. code-block:: yaml + + # config/services.yaml + + services: + admin.blog_post: + class: App\Admin\BlogPostAdmin + arguments: [~, App\Entity\BlogPost, ~] + tags: + - name: sonata.admin + manager_type: orm + label: 'Blog post' + label_translator_strategy: sonata.admin.label.strategy.native + route_builder: sonata.admin.route.path_info + pager_type: simple + # and so on From c98ebfc4803b6327a374d35a1a8c4a4efa0dca5b Mon Sep 17 00:00:00 2001 From: Vincent Langlet Date: Sat, 8 May 2021 19:32:05 +0200 Subject: [PATCH 4/4] Update doc --- docs/reference/configuration.rst | 9 +++------ docs/reference/templates.rst | 2 ++ 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/docs/reference/configuration.rst b/docs/reference/configuration.rst index 7e8155178f..baf5943149 100644 --- a/docs/reference/configuration.rst +++ b/docs/reference/configuration.rst @@ -136,13 +136,7 @@ Full Configuration Options route_builder: null label_translator_strategy: null pager_type: null - templates: - form: [] - filter: [] - view: - # Prototype - id: ~ templates: user_block: '@SonataAdmin/Core/user_block.html.twig' add_block: '@SonataAdmin/Core/add_block.html.twig' @@ -176,6 +170,9 @@ Full Configuration Options pager_results: '@SonataAdmin/Pager/results.html.twig' tab_menu_template: '@SonataAdmin/Core/tab_menu_template.html.twig' knp_menu_template: '@SonataAdmin/Menu/sonata_menu.html.twig' + form_theme: [] + filter_theme: [] + assets: stylesheets: diff --git a/docs/reference/templates.rst b/docs/reference/templates.rst index e55cadce19..52251d06a4 100644 --- a/docs/reference/templates.rst +++ b/docs/reference/templates.rst @@ -147,6 +147,8 @@ You can specify your templates in the config file: button_history: '@SonataAdmin/Button/history_button.html.twig' button_list: '@SonataAdmin/Button/list_button.html.twig' button_show: '@SonataAdmin/Button/show_button.html.twig' + form_theme: [] + filter_theme: [] .. warning::