class Icon

Provides a render element to display an icon.

Properties:

@property $pack_id (string) Icon Pack provider plugin id. @property $icon_id (string) Name of the icon. @property $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

Attributes

#[RenderElement('icon')]

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
12 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

7 #type uses of Icon
IconDefinition::getRenderable in core/lib/Drupal/Core/Theme/Icon/IconDefinition.php
Get the icon renderable element array.
IconDefinitionTest::testGetRenderable in core/tests/Drupal/Tests/Core/Theme/Icon/IconDefinitionTest.php
Test the IconDefinition::getRenderable method.
IconsTwigExtension::getIconRenderable in core/lib/Drupal/Core/Template/IconsTwigExtension.php
Get an icon renderable array.
IconsTwigExtensionTest::testGetIconRenderable in core/tests/Drupal/Tests/Core/Theme/Icon/IconsTwigExtensionTest.php
Test the IconsTwigExtension::getIconRenderable method.
IconTest::testPreRenderIcon in core/tests/Drupal/Tests/Core/Theme/Icon/IconTest.php
Test the Icon::preRenderIcon method.

... See full list

File

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

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 Deprecated Modifiers Object type Summary Overriden Title Overrides
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
Icon::getInfo public function Returns the element properties for this element. Overrides ElementInterface::getInfo
Icon::preRenderIcon public static function Icon element pre render callback.
MessengerTrait::$messenger protected property The messenger. 25
MessengerTrait::messenger public function Gets the messenger. 25
MessengerTrait::setMessenger public function Sets the messenger.
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin ID.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 2
PluginBase::getPluginId public function Gets the plugin ID of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable Deprecated public function Determines if the plugin is configurable.
RenderElementBase::$renderParent protected property The parent element.
RenderElementBase::$renderParentName protected property The parent key.
RenderElementBase::$storage protected property The storage.
RenderElementBase::addChild public function Adds a child render element. Overrides ElementInterface::addChild
RenderElementBase::changeType public function Change the type of the element. Overrides ElementInterface::changeType
RenderElementBase::create public static function Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface::create 2
RenderElementBase::createChild public function Creates a render object and attaches it to the current render object. Overrides ElementInterface::createChild
RenderElementBase::elementInfoManager protected function Returns the element info manager.
RenderElementBase::getChild public function Gets a child. Overrides ElementInterface::getChild
RenderElementBase::getChildren public function Returns child elements. Overrides ElementInterface::getChildren
RenderElementBase::initializeInternalStorage public function Initialize storage. Overrides ElementInterface::initializeInternalStorage
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::removeChild public function Removes a child. Overrides ElementInterface::removeChild
RenderElementBase::setAttributes public static function Sets a form element's class attribute. Overrides ElementInterface::setAttributes 2
RenderElementBase::setType protected function Set type on initialize. 1
RenderElementBase::toRenderable public function Returns a render array. Overrides ElementInterface::toRenderable
RenderElementBase::__construct public function Constructs a new render element object. Overrides PluginBase::__construct 6
RenderElementBase::__get public function Magic method: gets a property value.
RenderElementBase::__isset public function Magic method: checks if a property value is set.
RenderElementBase::__set public function Magic method: Sets a property value.
RenderElementBase::__sleep public function Overrides DependencySerializationTrait::__sleep
RenderElementBase::__unset public function Magic method: unsets a property value.
RenderElementBase::__wakeup public function Overrides DependencySerializationTrait::__wakeup
StringTranslationTrait::$stringTranslation protected property The string translation service. 3
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language. 1

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