Skip to content

Commit

Permalink
Add default_admin_services node (#7077)
Browse files Browse the repository at this point in the history
* Add default_admin_services node

* Move form/filter config

* Update doc

* Update doc
  • Loading branch information
VincentLanglet authored May 8, 2021
1 parent 88d8b1b commit 6ec9a7c
Show file tree
Hide file tree
Showing 8 changed files with 130 additions and 40 deletions.
68 changes: 39 additions & 29 deletions docs/cookbook/recipe_overwrite_admin_configuration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
11 changes: 4 additions & 7 deletions docs/reference/configuration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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'
Expand Down Expand Up @@ -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:
Expand Down
2 changes: 2 additions & 0 deletions docs/reference/templates.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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::

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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;
Expand Down
29 changes: 29 additions & 0 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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()

Expand Down
2 changes: 2 additions & 0 deletions src/DependencyInjection/SonataAdminExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
Expand Down
36 changes: 36 additions & 0 deletions tests/DependencyInjection/ConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ public function testOptionsWithInvalidFormat(): void
]]);
}

/**
* NEXT_MAJOR: Remove this test.
*
* @group legacy
*/
public function testCustomTemplatesPerAdmin(): void
{
$config = $this->process([[
Expand All @@ -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([[
Expand All @@ -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,
Expand All @@ -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([]);
Expand Down
2 changes: 2 additions & 0 deletions tests/DependencyInjection/SonataAdminExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
}

Expand Down

0 comments on commit 6ec9a7c

Please sign in to comment.