Skip to content

Commit 9e4f66a

Browse files
committed
Add a feature to auto-load theme libraries.
1 parent d5464b0 commit 9e4f66a

File tree

2 files changed

+24
-11
lines changed

2 files changed

+24
-11
lines changed

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,15 @@ Extremely simple module to load a styleguide page.
33

44
All opinions about the styleguide are part of the twig template in
55
the theme, which distinguishes it from other styleguide drupal modules.
6+
7+
## Adding custom CSS/JS to the styleguide
8+
If you create a library in your theme (THEMENAME.library.yml) called
9+
`ts_styleguide`, it will be automatically loaded on your styleguide page.
10+
11+
Example:
12+
```yaml
13+
ts_styleguide:
14+
css:
15+
theme:
16+
assets/css/styleguide.css: {}
17+
```

src/Controller/StyleGuideController.php

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,37 +4,35 @@
44

55
use Drupal\Core\Access\AccessResult;
66
use Drupal\Core\Controller\ControllerBase;
7-
use Drupal\Component\Utility\Xss;
87
use Symfony\Component\DependencyInjection\ContainerInterface;
9-
use Twig\Error\LoaderError;
108

119
/**
1210
* Creates style guide at /styleguide.
1311
*/
1412
class StyleGuideController extends ControllerBase {
1513

1614
/**
17-
* The theme manager.
15+
* The Library discovery service.
1816
*
19-
* @var \Drupal\Core\Theme\ThemeManagerInterface
17+
* @var \Drupal\Core\Asset\LibraryDiscoveryCollector
2018
*/
21-
protected $themeManager;
19+
protected $libraries;
2220

2321
/**
24-
* The twig service.
22+
* The theme manager.
2523
*
26-
* @var \Drupal\Core\Template\TwigEnvironment
24+
* @var \Drupal\Core\Theme\ThemeManagerInterface
2725
*/
28-
protected $twig;
26+
protected $themeManager;
2927

3028
/**
3129
* {@inheritDoc}
3230
*/
3331
public static function create(ContainerInterface $container) {
3432
$instance = parent::create($container);
3533
$instance->configFactory = $container->get('config.factory');
34+
$instance->libraries = $container->get('library.discovery');
3635
$instance->themeManager = $container->get('theme.manager');
37-
$instance->twig = $container->get('twig');
3836
return $instance;
3937
}
4038

@@ -46,11 +44,14 @@ public static function create(ContainerInterface $container) {
4644
*/
4745
public function tsStyleGuide() {
4846
$themename = $this->configFactory->get('system.theme')->get('default');
49-
50-
return [
47+
$content = [
5148
'#theme' => 'styleguide',
5249
'#theme_name' => $themename,
5350
];
51+
if ($this->libraries->getLibraryByName($themename, 'ts_styleguide')) {
52+
$content['#attached']['library'][] = "$themename/ts_styleguide";
53+
}
54+
return $content;
5455
}
5556

5657
/**

0 commit comments

Comments
 (0)