Skip to content

Commit

Permalink
Added option to load different Admin LTE skins
Browse files Browse the repository at this point in the history
  • Loading branch information
fastnloud committed Sep 14, 2020
1 parent c4d0026 commit 914993c
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 4 deletions.
2 changes: 1 addition & 1 deletion docs/reference/configuration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ Full Configuration Options
sort_admins: false
confirm_exit: true
js_debug: false
skin: 'skin-black'
use_select2: true
use_icheck: true
use_bootlint: false
Expand Down Expand Up @@ -181,7 +182,6 @@ Full Configuration Options
- bundles/sonatacore/vendor/components-font-awesome/css/font-awesome.min.css
- bundles/sonatacore/vendor/ionicons/css/ionicons.min.css
- bundles/sonataadmin/vendor/admin-lte/dist/css/AdminLTE.min.css
- bundles/sonataadmin/vendor/admin-lte/dist/css/skins/skin-black.min.css
- bundles/sonataadmin/vendor/iCheck/skins/square/blue.css
- bundles/sonatacore/vendor/eonasdan-bootstrap-datetimepicker/build/css/bootstrap-datetimepicker.min.css
- bundles/sonataadmin/vendor/jqueryui/themes/base/jquery-ui.css
Expand Down
18 changes: 17 additions & 1 deletion src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,23 @@ public function getConfigTreeBuilder()
->booleanNode('sort_admins')->defaultFalse()->info('Auto order groups and admins by label or id')->end()
->booleanNode('confirm_exit')->defaultTrue()->end()
->booleanNode('js_debug')->defaultFalse()->end()
->enumNode('skin')
->defaultValue('skin-black')
->values([
'skin-black',
'skin-black-light',
'skin-blue',
'skin-blue-light',
'skin-green',
'skin-green-light',
'skin-purple',
'skin-purple-light',
'skin-red',
'skin-red-light',
'skin-yellow',
'skin-yellow-light',
])
->end()
->booleanNode('use_select2')->defaultTrue()->end()
->booleanNode('use_icheck')->defaultTrue()->end()
->booleanNode('use_bootlint')->defaultFalse()->end()
Expand Down Expand Up @@ -406,7 +423,6 @@ public function getConfigTreeBuilder()
'bundles/sonatacore/vendor/components-font-awesome/css/font-awesome.min.css',
'bundles/sonatacore/vendor/ionicons/css/ionicons.min.css',
'bundles/sonataadmin/vendor/admin-lte/dist/css/AdminLTE.min.css',
'bundles/sonataadmin/vendor/admin-lte/dist/css/skins/skin-black.min.css',
'bundles/sonataadmin/vendor/iCheck/skins/square/blue.css',

'bundles/sonatacore/vendor/eonasdan-bootstrap-datetimepicker/build/css/bootstrap-datetimepicker.min.css',
Expand Down
5 changes: 5 additions & 0 deletions src/DependencyInjection/SonataAdminExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,11 @@ public function getNamespace()

private function buildStylesheets(array $config): array
{
$config['assets']['stylesheets'][] = sprintf(
'bundles/sonataadmin/vendor/admin-lte/dist/css/skins/%s.min.css',
$config['options']['skin']
);

return $this->mergeArray(
$config['assets']['stylesheets'],
$config['assets']['extra_stylesheets'],
Expand Down
4 changes: 3 additions & 1 deletion src/Resources/views/standard_layout.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ file that was distributed with this source code.
{% set _actions = block('actions') is defined ? block('actions')|trim : null %}
{% set _navbar_title = block('navbar_title') is defined ? block('navbar_title')|trim : null %}
{% set _list_filters_actions = block('list_filters_actions') is defined ? block('list_filters_actions')|trim : null -%}
{% set _skin = sonata_admin.adminPool.getOption('skin') %}
{% set _use_select2 = sonata_admin.adminPool.getOption('use_select2') %}
{% set _use_icheck = sonata_admin.adminPool.getOption('use_icheck') %}

Expand All @@ -35,6 +36,7 @@ file that was distributed with this source code.

<meta data-sonata-admin='{{ {
config: {
SKIN: _skin,
CONFIRM_EXIT: sonata_admin.adminPool.getOption('confirm_exit'),
USE_SELECT2: _use_select2,
USE_ICHECK: _use_icheck,
Expand Down Expand Up @@ -113,7 +115,7 @@ file that was distributed with this source code.
</head>
<body
{% block body_attributes -%}
class="sonata-bc skin-black fixed
class="sonata-bc {% block admin_lte_skin_class %}{{ _skin }}{% endblock %} fixed
{% if _use_select2 %}sonata-select2{% endif %}
{% if _use_icheck %}sonata-icheck{% endif %}
{% if app.request.cookies.get('sonata_sidebar_hide') -%}
Expand Down
76 changes: 75 additions & 1 deletion tests/DependencyInjection/SonataAdminExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
use Sonata\AdminBundle\Translator\NoopLabelTranslatorStrategy;
use Sonata\AdminBundle\Translator\UnderscoreLabelTranslatorStrategy;
use Sonata\AdminBundle\Twig\GlobalVariables;
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
use Symfony\Component\Config\Definition\Processor;

class SonataAdminExtensionTest extends AbstractExtensionTestCase
Expand Down Expand Up @@ -158,7 +159,7 @@ public function testExtraStylesheetsGetAdded(): void
$stylesheets = $this->container->getDefinition('sonata.admin.pool')->getArgument(3)['stylesheets'];

$this->assertSame(
array_merge($this->defaultConfiguration['assets']['stylesheets'], $extraStylesheets),
array_merge($this->getDefaultStylesheets(), $extraStylesheets),
$stylesheets
);
}
Expand Down Expand Up @@ -346,8 +347,81 @@ public function testLegacyTextExtensionConfiguration(): void
$this->assertNull($this->container->getDefinition('sonata.string.twig.extension')->getArgument(0));
}

public function testDefaultSkin(): void
{
$this->container->setParameter('kernel.bundles', []);
$this->load();

$stylesheets = $this->container->getDefinition('sonata.admin.pool')->getArgument(3)['stylesheets'];
$skin = $this->container->getDefinition('sonata.admin.pool')->getArgument(3)['skin'];

$this->assertSame($this->getDefaultStylesheets(), $stylesheets);
$this->assertSame('skin-black', $skin);
}

public function testSetSkin(): void
{
$this->container->setParameter('kernel.bundles', []);
$this->load([
'options' => [
'skin' => 'skin-blue',
],
]);

$stylesheets = $this->container->getDefinition('sonata.admin.pool')->getArgument(3)['stylesheets'];
$skin = $this->container->getDefinition('sonata.admin.pool')->getArgument(3)['skin'];

$this->assertSame($this->getDefaultStylesheets('skin-blue'), $stylesheets);
$this->assertSame('skin-blue', $skin);
}

public function testSetDefaultSkin(): void
{
$this->container->setParameter('kernel.bundles', []);
$this->load([
'options' => [
'skin' => 'skin-black',
],
]);

$stylesheets = $this->container->getDefinition('sonata.admin.pool')->getArgument(3)['stylesheets'];
$skin = $this->container->getDefinition('sonata.admin.pool')->getArgument(3)['skin'];

$this->assertSame($this->getDefaultStylesheets(), $stylesheets);
$this->assertSame('skin-black', $skin);
}

public function testSetInvalidSkin(): void
{
$this->expectException(InvalidConfigurationException::class);
$this->expectExceptionMessage('The value "skin-invalid" is not allowed for path "sonata_admin.options.skin". Permissible values: "skin-black", "skin-black-light", "skin-blue", "skin-blue-light", "skin-green", "skin-green-light", "skin-purple", "skin-purple-light", "skin-red", "skin-red-light", "skin-yellow", "skin-yellow-light"');
$this->container->setParameter('kernel.bundles', []);
$this->load([
'options' => [
'skin' => 'skin-invalid',
],
]);
}

protected function getContainerExtensions(): array
{
return [new SonataAdminExtension()];
}

private function getDefaultStylesheets(?string $skin = 'skin-black'): array
{
$this->load([
'options' => [
'skin' => $skin,
],
]);

$defaultStylesheets = $this->defaultConfiguration['assets']['stylesheets'];
$defaultStylesheets[] = sprintf(
'bundles/sonataadmin/vendor/admin-lte/dist/css/skins/%s.min.css',
$this->container->getDefinition('sonata.admin.pool')->getArgument(3)['skin']
);

return $defaultStylesheets;
}
}

0 comments on commit 914993c

Please sign in to comment.