Skip to content

Commit 51a0889

Browse files
committed
Add events to exclude headline styles and second headline
1 parent 2a32a46 commit 51a0889

File tree

4 files changed

+95
-10
lines changed

4 files changed

+95
-10
lines changed

config/services.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@ services:
66

77
ContaoThemeManager\Core\:
88
resource: '../src/'
9-
exclude: '../src/{Model,DependencyInjection,Resources}'
9+
exclude: '../src/{Model,DependencyInjection,Event}'
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace ContaoThemeManager\Core\Event;
4+
5+
use Symfony\Contracts\EventDispatcher\Event;
6+
7+
class ExcludeHeadlineStyleEvent extends Event
8+
{
9+
public function __construct(public array $types)
10+
{}
11+
12+
public function setTypes(array $types): void
13+
{
14+
$this->types = $types;
15+
}
16+
17+
public function getTypes(): array
18+
{
19+
return $this->types;
20+
}
21+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace ContaoThemeManager\Core\Event;
4+
5+
use Symfony\Contracts\EventDispatcher\Event;
6+
7+
class ExcludeSecondHeadlineEvent extends Event
8+
{
9+
public function __construct(public array $types)
10+
{}
11+
12+
public function setTypes(array $types): void
13+
{
14+
$this->types = $types;
15+
}
16+
17+
public function getTypes(): array
18+
{
19+
return $this->types;
20+
}
21+
}

src/ThemeManager.php

Lines changed: 52 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
use Contao\DataContainer;
1414
use Contao\File;
1515
use Contao\System;
16+
use ContaoThemeManager\Core\Event\ExcludeHeadlineStyleEvent;
17+
use ContaoThemeManager\Core\Event\ExcludeSecondHeadlineEvent;
1618
use ContaoThemeManager\Core\StyleManager\StyleManagerXML;
1719
use Oveleon\ContaoThemeCompilerBundle\Compiler\FileCompiler;
1820
use Symfony\Component\Filesystem\Filesystem;
@@ -43,29 +45,70 @@ public function extendHeadlineField($dc): void
4345
'form',
4446
];
4547

46-
$objPalette = PaletteManipulator::create()
47-
->addField(['headlineStyle', 'headline2', 'headline2Style'], 'headline');
48+
$eventDispatcher = System::getContainer()->get('event_dispatcher');
4849

49-
$test = $GLOBALS['TL_DCA'][$dc->table]['palettes'];
50+
$excludeHeadlineStyleEvent = new ExcludeHeadlineStyleEvent($skipTypes);
51+
$excludeSecondHeadlineEvent = new ExcludeSecondHeadlineEvent($skipTypes);
52+
53+
$eventDispatcher->dispatch($excludeHeadlineStyleEvent);
54+
$eventDispatcher->dispatch($excludeSecondHeadlineEvent);
55+
56+
$excludeHeadlineStyleTypes = $excludeHeadlineStyleEvent->getTypes();
57+
$excludeSecondHeadlineTypes = $excludeSecondHeadlineEvent->getTypes();
5058

5159
foreach ($GLOBALS['TL_DCA'][$dc->table]['palettes'] as $name => $palette)
5260
{
53-
if (in_array($name, $skipTypes))
61+
if (is_array($palette))
5462
{
5563
continue;
5664
}
5765

58-
if (!is_array($palette) && strpos($palette, 'headlineStyle') === false && strpos($palette, 'headline') !== false)
66+
if (!str_contains($palette, 'headline') || str_contains($palette, 'headlineStyle'))
5967
{
60-
$objPalette->applyToPalette($name, $dc->table);
68+
continue;
6169
}
70+
71+
$includeHeadlineStyle = !in_array($name, $excludeHeadlineStyleTypes, true);
72+
$includeSecondHeadline = !in_array($name, $excludeSecondHeadlineTypes, true);
73+
74+
if (!$includeHeadlineStyle && !$includeSecondHeadline)
75+
{
76+
continue;
77+
}
78+
79+
$fields = [];
80+
81+
if ($includeHeadlineStyle)
82+
{
83+
$fields[] = 'headlineStyle';
84+
}
85+
86+
if ($includeSecondHeadline)
87+
{
88+
$fields[] = 'headline2';
89+
90+
if ($includeHeadlineStyle)
91+
{
92+
$fields[] = 'headline2Style';
93+
}
94+
}
95+
96+
if ([] === $fields)
97+
{
98+
continue;
99+
}
100+
101+
PaletteManipulator::create()
102+
->addField($fields, 'headline')
103+
->applyToPalette($name, $dc->table)
104+
;
62105
}
63106
}
64107

65108
/**
66109
* Adjust the file palettes
67110
*/
68-
public function adjustCustomFilePalettes(DataContainer $dc)
111+
public function adjustCustomFilePalettes(DataContainer $dc): void
69112
{
70113
if (!$dc->id)
71114
{
@@ -109,7 +152,7 @@ public function onParseThemeManagerConfiguration($compiler, $configVars): void
109152
}
110153
}
111154

112-
// Delete existing file if no custom config could be parsed
155+
// Delete the existing file if no custom config could be parsed
113156
if (0 === $counter && file_exists($path = $this->rootDir . '/' . $xmlPath))
114157
{
115158
unlink($path);
@@ -124,7 +167,7 @@ public function onParseThemeManagerConfiguration($compiler, $configVars): void
124167
}
125168

126169
/**
127-
* Creates a css file within assets
170+
* Creates a CSS file within assets
128171
*
129172
* @throws \Exception
130173
*/

0 commit comments

Comments
 (0)