class Icon

Provides a render element to display an icon.

Properties:

  • #pack_id: (string) Icon Pack provider plugin id.
  • #icon_id: (string) Name of the icon.
  • #settings: (array) Settings sent to the inline Twig template.

Usage Example:

$build['icon'] = [
    '#type' => 'icon',
    '#pack_id' => 'material_symbols',
    '#icon_id' => 'home',
    '#settings' => [
        'width' => 64,
    ],
];

@internal

Hierarchy

Expanded class hierarchy of Icon

1 file declares its use of Icon
IconTest.php in core/tests/Drupal/Tests/Core/Theme/Icon/IconTest.php
10 string references to 'Icon'
BareHtmlPageRenderer::systemPageAttachments in core/lib/Drupal/Core/Render/BareHtmlPageRenderer.php
Helper for system_page_attachments.
DbLogController::overview in core/modules/dblog/src/Controller/DbLogController.php
Displays a listing of database log messages.
file.schema.yml in core/modules/file/config/schema/file.schema.yml
core/modules/file/config/schema/file.schema.yml
IconsTwigExtension::getFunctions in core/lib/Drupal/Core/Template/IconsTwigExtension.php
IconsTwigExtensionTest::testGetFunctions in core/tests/Drupal/Tests/Core/Theme/Icon/IconsTwigExtensionTest.php
Test the IconsTwigExtension::getFunctions method.

... See full list

File

core/lib/Drupal/Core/Render/Element/Icon.php, line 33

Namespace

Drupal\Core\Render\Element
View source
class Icon extends RenderElementBase {
    
    /**
     * {@inheritdoc}
     */
    public function getInfo() : array {
        return [
            '#pre_render' => [
                [
                    self::class,
                    'preRenderIcon',
                ],
            ],
            '#pack_id' => '',
            '#icon_id' => '',
            '#settings' => [],
        ];
    }
    
    /**
     * Icon element pre render callback.
     *
     * @param array $element
     *   An associative array containing the properties of the icon element.
     *
     * @return array
     *   The modified element.
     */
    public static function preRenderIcon(array $element) : array {
        $icon_full_id = IconDefinition::createIconId($element['#pack_id'], $element['#icon_id']);
        $pluginManagerIconPack = \Drupal::service('plugin.manager.icon_pack');
        if (!($icon = $pluginManagerIconPack->getIcon($icon_full_id))) {
            return $element;
        }
        // Build context minimal values as icon_id, optional source and attributes.
        $context = [
            'icon_id' => $icon->getIconId(),
        ];
        // Better to not have source value if not set for the template.
        if ($source = $icon->getSource()) {
            $context['source'] = $source;
        }
        // Silently ensure settings is an array.
        if (!is_array($element['#settings'])) {
            $element['#settings'] = [];
        }
        $extractor_data = $icon->getAllData();
        // Inject attributes variable if not created by the extractor.
        if (!isset($extractor_data['attributes'])) {
            $extractor_data['attributes'] = new Attribute();
        }
        $element['inline-template'] = [
            '#type' => 'inline_template',
            '#template' => $icon->getTemplate(),
            // Context include data from extractor and settings, priority on settings
            // from this element. Context as last value to be sure nothing override
            // icon_id or source if set.
'#context' => array_merge($extractor_data, $element['#settings'], $context),
        ];
        if ($library = $icon->getLibrary()) {
            $element['inline-template']['#attached'] = [
                'library' => [
                    $library,
                ],
            ];
        }
        return $element;
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title Overrides
Icon::getInfo public function Returns the element properties for this element. Overrides ElementInterface::getInfo
Icon::preRenderIcon public static function Icon element pre render callback.
PluginInspectionInterface::getPluginDefinition public function Gets the definition of the plugin implementation. 7
PluginInspectionInterface::getPluginId public function Gets the plugin ID of the plugin instance. 3
RenderElementBase::preRenderAjaxForm public static function Adds Ajax information about an element to communicate with JavaScript. 2
RenderElementBase::preRenderGroup public static function Adds members of this group as actual elements for rendering. 2
RenderElementBase::processAjaxForm public static function Form element processing handler for the #ajax form property. 3
RenderElementBase::processGroup public static function Arranges elements into groups. 2
RenderElementBase::setAttributes public static function Overrides ElementInterface::setAttributes 2

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.