Skip to content

Commit

Permalink
Add default_admin_services node
Browse files Browse the repository at this point in the history
  • Loading branch information
VincentLanglet committed Apr 30, 2021
1 parent c6f416e commit 2e1bc4d
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 28 deletions.
49 changes: 24 additions & 25 deletions docs/cookbook/recipe_overwrite_admin_configuration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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.
2 changes: 1 addition & 1 deletion 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 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
38 changes: 38 additions & 0 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
2 changes: 2 additions & 0 deletions src/DependencyInjection/SonataAdminExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,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
41 changes: 41 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,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([]);
Expand Down

0 comments on commit 2e1bc4d

Please sign in to comment.