oveleon/contao-component-style-manager
Manage CSS classes in Contao as reusable style groups. Define, organize and apply component styles consistently in the backend, simplify editorial workflows, and keep templates clean by selecting predefined class sets for content elements and modules.
Installation:
composer require oveleon/contao-component-style-manager
Or via Contao Manager by searching for "StyleManager."
First Use Case:
my_theme_classes) and define groups (e.g., colors, spacing).bg-red, mt-2) with labels and descriptions.Template Integration:
Use the template variable $styleManagerClasses in your Twig templates to output the selected classes:
<div class="{{ styleManagerClasses.my_theme_classes }}">
Your content here
</div>
Backend Configuration:
header_styles, button_variants).colors, animations).extendModule: ["news_list"]).label_callback (replaces deprecated child_record_callback) for dynamic labels:
my_group:
classes:
dynamic_class:
label_callback: "App\Callback\DynamicLabelCallback"
Template Usage:
{{ styleManagerClasses.my_archive_identifier|join(' ') }}
{% if styleManagerClasses.my_archive_identifier contains 'bg-dark' %}
<div class="dark-mode">
{% endif %}
Dynamic Integration:
// config/autoload/contao-style-manager.php
return [
'style_manager' => [
'archives' => [
'my_custom_archive' => [
'title' => 'Custom Archive',
'groups' => [
'my_group' => [
'title' => 'My Group',
'classes' => [
'custom-class' => 'Custom Class Label',
],
],
],
],
],
],
];
extendFormFields: true in YAML/Backend config.YAML Configuration (Recommended for Reusability):
Create /templates/style-manager-custom.yaml:
my_archive:
title: "Custom Styles"
groupAlias: my_group
children:
my_group:
cssClasses:
"text-bold": "Bold Text"
"text-italic": "Italic Text"
extendContentElement: true
contentElements: ["text", "headline"]
BundleConfigListener to load styles from vendor packages (e.g., themes).{{ styleManagerClasses.my_archive|replace({' ': '_'}) }}
// In a custom service or controller
$this->StyleManager->getClassesForArchive('my_archive');
Deprecated Features:
child_record_callback is removed in v3.12.1. Replace with label_callback for dynamic labels.PHP/Contao Compatibility:
YAML Parsing Issues:
/templates/ or /vendor/ with correct naming (style-manager-*.yaml).pid or id keys when merging configurations.Backend Widget Quirks:
--sm-i: 3 or .w33 class).blankOption: true in YAML.Template Variable Scope:
$this->Template->styleManagerClasses = $this->StyleManager->getClassesForArchive('my_archive');
Check Archive Assignment: Verify archives are linked to the correct DCA in the backend widget settings.
YAML Validation: Use a validator (e.g., YAML Lint) to catch syntax errors.
Backend Logs:
Enable Contao’s debug mode (config/localconfig.php):
$GLOBALS['TL_CONFIG']['debugMode'] = true;
Check for errors in System → Log.
Class Output: Debug template variables with:
{{ dump(styleManagerClasses) }}
Custom Form Fields: Extend form fields by adding to YAML:
my_group:
extendFormFields: true
formFields: ["input", "textarea"]
Third-Party DCA Support:
Register custom DCAs in config/autoload/contao-style-manager.php:
return [
'style_manager' => [
'supported_dcas' => [
'tl_my_custom_table' => [
'archive_identifier' => 'my_custom_archive',
],
],
],
];
Override Widget Templates:
Copy /vendor/oveleon/contao-component-style-manager/src/Resources/contao/templates/ to /templates/ and modify:
style_manager_widget.html5 (main widget).style_manager_group.html5 (group tabs).Event Listeners:
Hook into the style_manager.archives.load event to dynamically modify archives:
// src/EventListener/StyleManagerListener.php
public function onLoadArchives(StyleManagerEvent $event) {
$event->getArchives()->add('dynamic_archive', [
'title' => 'Dynamic Archive',
// ...config
]);
}
Register in services.yaml:
services:
App\EventListener\StyleManagerListener:
tags:
- { name: kernel.event_listener, event: style_manager.archives.load, method: onLoadArchives }
CSS/JS Overrides:
Load custom assets via AddBackendAssetsListener:
// src/EventListener/AddBackendAssetsListener.php
public function onAddBackendAssets(BackendAssetsEvent $event) {
$event->addCssFile('bundles/mytheme/css/style-manager-override.css');
}
Note: Ensure your callback classes implement StyleManagerLabelCallbackInterface for label_callback:
class DynamicLabelCallback implements StyleManagerLabelCallbackInterface {
public function getLabel(string $className, array $config): string {
return "Dynamic Label for {$className}";
}
}
How can I help you explore Laravel packages today?