Creating a custom Icon Set Provider
An icon set provider is responsible for providing icons and writing the
necessary stylesheet. The class must implement
\Daniel,
but typically extends
\Daniel
instead.
If the abstract class is used, you must specify the icon provider, which will later be responsible for rendering the icons. You can use existing core classes or your own implementations for this purpose.
declare(strict_types=1);
namespace Vendor\MyExt\Imaging\IconSet;
use DanielHaring\IconHub\Imaging\IconSet\AbstractIconSetProvider;
use TYPO3\CMS\Core\Imaging\IconProvider\SvgIconProvider;
class CustomIconSetProvider extends AbstractIconSetProvider
{
protected function getProviderName(): string
{
return SvgIconProvider::class;
}
}
Providing icons
The actual icons are provided via the generator method
provide. This method is expected to yield instances of
\Daniel.
If the recommended abstract class is used, icons can be created using
the helper method
create.
declare(strict_types=1);
namespace Vendor\MyExt\Imaging\IconSet;
use DanielHaring\IconHub\Imaging\IconSet\AbstractIconSetProvider;
class CustomIconSetProvider extends AbstractIconSetProvider
{
public function provideIcons(): \Generator
{
yield $this->createIcon('tx-myext-dummy', 'Dummy');
}
}
Write the stylesheet
The
provide method is also primarily used to generate the
stylesheet.
The required styles can be written using
$this->styles->write.
This method can be used as often as needed to accumulate styles.
Although not strictly necessary, it is highly recommended to check in
advance whether stylesheet writing is enabled to avoid unnecessary
computing.
$this->styles->is can be used for this
purpose.
declare(strict_types=1);
namespace Vendor\MyExt\Imaging\IconSet;
use DanielHaring\IconHub\Imaging\IconSet\AbstractIconSetProvider;
class CustomIconSetProvider extends AbstractIconSetProvider
{
public function provideIcons(): \Generator
{
if ($this->styles->isEnabled()) {
$this->styles->write('/* Base styles to write. */');
$this->styles->write('/* Icon specific styles to write. */');
}
yield $this->createIcon('tx-myext-dummy', 'Dummy');
}
}
Full example
This is what a simplified, typical icon set provider looks like:
declare(strict_types=1);
namespace Vendor\MyExt\Imaging\IconSet;
use DanielHaring\IconHub\Imaging\IconSet\AbstractIconSetProvider;
use TYPO3\CMS\Core\Imaging\IconProvider\SvgIconProvider;
class CustomIconSetProvider extends AbstractIconSetProvider
{
public function provideIcons(): \Generator
{
if ($this->styles->isEnabled()) {
$this->styles->write('/* Base styles for the icon set. */');
}
foreach (['dummy', 'test'] as $iconName) {
if ($this->styles->isEnabled()) {
$this->styles->write('/* Styles for icon "' . $iconName . '". */');
}
yield $this->createIcon('tx-myext-' . $iconName, $iconName, [
'source' => 'EXT:my_ext/Resources/Public/Icons/' . $iconName . '.svg'
]);
}
}
protected function getProviderName(): string
{
return SvgIconProvider::class;
}
}