diff --git a/docs/cookbook/recipe_overwrite_admin_configuration.rst b/docs/cookbook/recipe_overwrite_admin_configuration.rst index 9205d37575..4329e0d0e7 100644 --- a/docs/cookbook/recipe_overwrite_admin_configuration.rst +++ b/docs/cookbook/recipe_overwrite_admin_configuration.rst @@ -5,37 +5,47 @@ 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: - 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 - - # 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. + 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 + datagrid_builder: sonata.admin.builder.orm_datagrid + translator: translator + configuration_pool: sonata.admin.pool + route_generator: sonata.admin.route.default_generator + 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 + +With these settings you will be able to change default services and templates used by the admin instances. + +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 diff --git a/docs/reference/configuration.rst b/docs/reference/configuration.rst index 6fdc59e0a6..baf5943149 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 @@ -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:: diff --git a/src/DependencyInjection/Compiler/AddDependencyCallsCompilerPass.php b/src/DependencyInjection/Compiler/AddDependencyCallsCompilerPass.php index c4b4508e91..057a6c94a2 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), @@ -386,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 6d2a10c30a..944d3c6dad 100644 --- a/src/DependencyInjection/Configuration.php +++ b/src/DependencyInjection/Configuration.php @@ -344,11 +344,34 @@ 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() + ->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() @@ -425,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/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..21782792ad 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,31 @@ 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, + ], $config['default_admin_services']); + } + public function testDashboardWithoutRoles(): void { $config = $this->process([]); 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')); }