Icon API

TYPO3 CMS provides an icon API for all icons in the TYPO3 backend.

Registration

All icons must be registered in the IconRegistry. To register icons for your own extension, create a file called Configuration/Icons.php in your extension - for example: typo3conf/ext/my_extension/Configuration/Icons.php.

Note

In versions below TYPO3 v11.4 the configuration was done in the ext_localconf.php, please use the version selector to look-up the syntax in the corresponding documentation version.

The file needs to return a flat PHP configuration array with the following keys:

EXT:my_extension/Configuration/Icons.php
<?php
   return [
       // Icon identifier
       'mysvgicon' => [
           // Icon provider class
           'provider' => \TYPO3\CMS\Core\Imaging\IconProvider\SvgIconProvider::class,
           // The source SVG for the SvgIconProvider
           'source' => 'EXT:my_extension/Resources/Public/Icons/mysvg.svg',
       ],
       'mybitmapicon' => [
           'provider' => \TYPO3\CMS\Core\Imaging\IconProvider\BitmapIconProvider::class,
           // The source bitmap file
           'source' => 'EXT:my_extension/Resources/Public/Icons/mybitmap.png',
           // All icon providers provide the possibility to register an icon that spins
           'spinning' => true,
       ],
       'anothersvgicon' => [
           'provider' => \TYPO3\CMS\Core\Imaging\IconProvider\SvgIconProvider::class,
           'source' => 'EXT:my_extension/Resources/Public/Icons/anothersvg.svg',
           // Since TYPO3 v12.0 an extension that provides icons for broader
           // use can mark such icons as deprecated with logging to the TYPO3
           // deprecation log. All keys (since, until, replacement) are optional.
           'deprecated' => [
               'since' => 'my extension v2',
               'until' => 'my extension v3',
               'replacement' => 'alternative-icon',
           ],
       ],
   ];

IconProvider

The TYPO3 Core ships two icon providers which can be used straight away:

  • BitmapIconProvider – For all kinds of bitmap icons (GIF, PNG, JPEG, etc.)

  • SvgIconProvider – For SVG icons

Changed in version 12.0: The FontawesomeIconProvider has been available since version 11.5 and was removed from the Core in 12.0. You can use the polyfill extension from friendsoftypo3/fontawesome-provider which is also compatible with TYPO3 v11.

If require need a custom icon provider, you can add your own by writing a class which implements the IconProviderInterface.

Using icons in your code

You can use the Icon API to receive icons in your PHP code or directly in Fluid.

The PHP way

You can use the IconFactory to request an icon:

$iconFactory = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(
   \TYPO3\CMS\Core\Imaging\IconFactory::class
);
$icon = $iconFactory->getIcon(
   'tx-myext-action-preview',
   \TYPO3\CMS\Core\Imaging\Icon::SIZE_SMALL,
   'overlay-identifier'
);
$this->view->assign('icon', $icon);

Changed in version 12.0: The TYPO3 Icon Api previously defaulted to Icon::SIZE_DEFAULT and was adapted to now use Icon::SIZE_MEDIUM instead. Icon::SIZE_MEDIUM is displayed at a fixed size of 32x32 px while Icon::SIZE_DEFAULT now scales with the text.

In cases where the size Icon::SIZE_DEFAULT was explicitly set this might result in changed behavior. Switch to Icon::SIZE_MEDIUM then.

The Fluid Viewhelper

You can also use the Fluid core:icon Viewhelper to render an icon in your view:

{namespace core = TYPO3\CMS\Core\ViewHelpers}
<core:icon identifier="my-icon-identifier" size="small" />

This will render the desired icon using an img-tag. If you prefer having the SVG inlined into your HTML (e.g. for being able to change colors with CSS), you can set the optional alternativeMarkupIdentifier attribute to inline. By default, the icon will pick up the font-color of its surrounding element if you use this option.

{namespace core = TYPO3\CMS\Core\ViewHelpers}
<core:icon identifier="my-icon-identifier" size="small" alternativeMarkupIdentifier="inline" />

The JavaScript way

In JavaScript, icons can be only fetched from the Icon Registry. To achieve this, add the following dependency to your RequireJS module: TYPO3/CMS/Backend/Icons. In this section, the module is known as Icons.

The module has a single public method getIcon() which accepts up to five arguments:

identifier

| Condition: required | Type: string |

Identifier of the icon as registered in the Icon Registry.

size

| Condition: required | Type: string | Default: medium |

Desired size of the icon. All values of the Icons.sizes enum are allowed, these are:

  • default: 1em, to scale with font size

  • small: fixed to 16px

  • medium: fixed to 32px (default)

  • large: fixed to 64px

overlayIdentifier

| Condition: optional | Type: string |

Identifier of an overlay icon as registered in the Icon Registry.

state

| Condition: optional | Type: string |

Sets the state of the icon. All values of the Icons.states enum are allowed, these are: default and disabled.

markupIdentifier

| Condition: optional | Type: string |

Defines how the markup is returned. All values of the Icons.markupIdentifiers enum are allowed, these are: default and inline. Please note that inline is only meaningful for SVG icons.

The method getIcon() returns a jQuery Promise object, as internally an AJAX request is done.

Note

Since TYPO3 v9, the icons are cached in the localStorage of the client to reduce the workload off the server.

Here's an example code how a usage of the JavaScript Icon API may look like:

define(['jquery', 'TYPO3/CMS/Backend/Icons'], function($, Icons) {
    // Get a single icon
    Icons.getIcon('spinner-circle-light', Icons.sizes.small).done(function(spinner) {
        console.log(spinner);
    });
});

Available icons

The TYPO3 Core comes with a number of icons that may be used in your extensions.

To search for available icons, you can use one of these possibilities:

Install the styleguide extension

Install the extension styleguide as described in the Readme in the installation section.

Once installed, you can view available icons by selecting help (?) on the top in the TYPO3 backend, then Styleguide and then Icons, All Icons.

There, browse through existing icons. Use the name under the icon (for example actions-add) as first parameter for IconFactory::getIcon() in PHP or as value for the argument identifier in Fluid (see code examples above).

../../_images/IconProviders.png

Use TYPO3.Icons

An alternative way to look for existing icons is to browse through TYPO3.Icons.