function Icon::preRenderIcon

Icon element pre render callback.

Parameters

array $element: An associative array containing the properties of the icon element.

Return value

array The modified element.

3 calls to Icon::preRenderIcon()
IconTest::testPreRenderIcon in core/tests/Drupal/Tests/Core/Theme/Icon/IconTest.php
Test the Icon::preRenderIcon method.
IconTest::testPreRenderIconEmptyValues in core/tests/Drupal/Tests/Core/Theme/Icon/IconTest.php
Test the Icon::preRenderIcon method.
IconTest::testPreRenderIconNoIcon in core/tests/Drupal/Tests/Core/Theme/Icon/IconTest.php
Test the Icon::preRenderIcon method.

File

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

Class

Icon
Provides a render element to display an icon.

Namespace

Drupal\Core\Render\Element

Code

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;
}

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