Skip to content

Commit 9bb0719

Browse files
[FEATURE] Introduce event to modify icon providers (#1582)
Co-authored-by: Benjamin Kott <benjamin.kott@outlook.com>
1 parent 3b05a54 commit 9bb0719

File tree

6 files changed

+110
-7
lines changed

6 files changed

+110
-7
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of the package bk2k/bootstrap-package.
7+
*
8+
* For the full copyright and license information, please read the
9+
* LICENSE file that was distributed with this source code.
10+
*/
11+
12+
namespace BK2K\BootstrapPackage\Events;
13+
14+
use BK2K\BootstrapPackage\Icons\IconProviderInterface;
15+
16+
final class ModifyIconProvidersEvent
17+
{
18+
/**
19+
* @param IconProviderInterface[] $iconProviders
20+
*/
21+
public function __construct(
22+
private array $iconProviders,
23+
) {
24+
}
25+
26+
/**
27+
* @return IconProviderInterface[]
28+
*/
29+
public function getIconProviders(): array
30+
{
31+
return $this->iconProviders;
32+
}
33+
34+
/**
35+
* @param IconProviderInterface[] $iconProviders
36+
*/
37+
public function setIconProviders(array $iconProviders): void
38+
{
39+
$this->iconProviders = $iconProviders;
40+
}
41+
}

Classes/Icons/IconProviderInterface.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,12 @@
99

1010
namespace BK2K\BootstrapPackage\Icons;
1111

12+
use Symfony\Component\DependencyInjection\Attribute\AutoconfigureTag;
13+
1214
/**
1315
* IconProviderInterface
1416
*/
17+
#[AutoconfigureTag('bootstrapPackage.iconProvider')]
1518
interface IconProviderInterface
1619
{
1720
/**
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
declare(strict_types = 1);
3+
4+
/*
5+
* This file is part of the package bk2k/bootstrap-package.
6+
*
7+
* For the full copyright and license information, please read the
8+
* LICENSE file that was distributed with this source code.
9+
*/
10+
11+
namespace BK2K\BootstrapPackage\Service;
12+
13+
use BK2K\BootstrapPackage\Events\ModifyIconProvidersEvent;
14+
use BK2K\BootstrapPackage\Icons\IconProviderInterface;
15+
use Symfony\Component\DependencyInjection\Attribute\AutowireIterator;
16+
17+
final readonly class IconProviderService
18+
{
19+
public function __construct(
20+
#[AutowireIterator('bootstrapPackage.iconProvider')]
21+
private iterable $iconProviders
22+
) {
23+
}
24+
25+
public function __invoke(ModifyIconProvidersEvent $event): void
26+
{
27+
$classList = [];
28+
$iconProviders = $event->getIconProviders();
29+
30+
// add already registered class names to prevent duplicate registration
31+
array_walk($iconProviders, static function (IconProviderInterface $iconProvider) use (&$classList) {
32+
$classList[] = get_class($iconProvider);
33+
});
34+
35+
/** @var IconProviderInterface $iconProvider */
36+
foreach ($this->iconProviders as $iconProvider) {
37+
$className = get_class($iconProvider);
38+
if (in_array($className, $classList, true)) {
39+
continue;
40+
}
41+
$iconProviders[] = $iconProvider;
42+
$classList[] = $className;
43+
}
44+
45+
$event->setIconProviders($iconProviders);
46+
}
47+
}

Classes/Service/IconService.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,23 @@
1010

1111
namespace BK2K\BootstrapPackage\Service;
1212

13+
use BK2K\BootstrapPackage\Events\ModifyIconProvidersEvent;
1314
use BK2K\BootstrapPackage\Icons\IconProviderInterface;
15+
use Psr\EventDispatcher\EventDispatcherInterface;
1416
use TYPO3\CMS\Core\Utility\GeneralUtility;
1517

1618
/**
1719
* IconService
1820
*/
1921
class IconService
2022
{
23+
protected EventDispatcherInterface $eventDispatcher;
24+
25+
public function __construct()
26+
{
27+
$this->eventDispatcher = GeneralUtility::makeInstance(EventDispatcherInterface::class);
28+
}
29+
2130
public function getIconSetItems(array &$configuration): void
2231
{
2332
$iconSets = [];
@@ -84,6 +93,9 @@ public function getIconProviders(): array
8493
}
8594
}
8695

87-
return $iconProviders;
96+
/** @var ModifyIconProvidersEvent $event */
97+
$event = $this->eventDispatcher->dispatch(new ModifyIconProvidersEvent($iconProviders));
98+
99+
return $event->getIconProviders();
88100
}
89101
}

Configuration/Services.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ services:
1313
identifier: 'bk2k/bootstrap-package/set-backend-styling'
1414
event: TYPO3\CMS\Core\Package\Event\AfterPackageActivationEvent
1515

16+
BK2K\BootstrapPackage\Service\IconProviderService:
17+
tags:
18+
- name: event.listener
19+
identifier: 'bk2k/bootstrap-package/modify-icon-providers'
20+
event: BK2K\BootstrapPackage\Events\ModifyIconProvidersEvent
21+
1622
BK2K\BootstrapPackage\Backend\ToolbarItem\VersionToolbarItem:
1723
tags:
1824
- name: event.listener

ext_localconf.php

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,6 @@
5454
= \BK2K\BootstrapPackage\Hooks\PageRenderer\PreProcessHook::class . '->execute';
5555
}
5656

57-
// Register icon provider
58-
$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ext/bootstrap-package/icons']['provider'][\BK2K\BootstrapPackage\Icons\IoniconsProvider::class]
59-
= \BK2K\BootstrapPackage\Icons\IoniconsProvider::class;
60-
$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ext/bootstrap-package/icons']['provider'][\BK2K\BootstrapPackage\Icons\GlyphiconsProvider::class]
61-
= \BK2K\BootstrapPackage\Icons\GlyphiconsProvider::class;
62-
6357
// Add default RTE configuration for bootstrap package
6458
$GLOBALS['TYPO3_CONF_VARS']['RTE']['Presets']['bootstrap'] = 'EXT:bootstrap_package/Configuration/RTE/Default.yaml';
6559

0 commit comments

Comments
 (0)