1 icon.inc theme_icon(array $variables)

Returns HTML for an inline-icon.

This effectively returns the contents of an SVG file. But it could potentially be override to replace inlined SVGs with other mechanisms, like an icon font.

@since 1.28.0 Function added.

Parameters

array $variables: An associative array containing:

  • name: The machine name for the icon being displayed.
  • path: The full path to the icon file, relative to installation root.
  • alt: Alternative text for this icon. Default icons are SVGs and this value is added as a <title> tag within the SVG. If not specified, the icon will automatically be assigned the aria-hidden="true" attribute.
  • attributes: Attributes to be added to the icon itself.

Return value

string: The HTML output.

File

core/includes/icon.inc, line 289
Provides the Backdrop API for placing icons.

Code

function theme_icon(array $variables) {
  // Ensure the filename is .svg.
  if (image_is_svg($variables['path'])) {
    // Ensure the file contents are an SVG.
    $svg_contents = file_get_contents($variables['path']);
    if (strpos($svg_contents, '<svg') === 0) {
      // Clean out any embedded XSS within the SVG. This very-restrictive set
      // of options should be adequate for icons.
      $svg_contents = filter_xss($svg_contents, array('svg', 'use', 'title',
        'desc', 'defs', 'linearGradient', 'stop', 'rect', 'circle', 'path'));

      // Move the "alt" text to an attribute.
      if ($variables['alt']) {
        $variables['attributes']['alt'] = $variables['alt'];
      }
      else {
        $variables['attributes']['aria-hidden'] = 'true';
      }

      // Add default icon classes.
      $variables['attributes']['class'] = array_merge(
      array('icon', 'icon--' . $variables['name']), 
      isset($variables['attributes']['class']) ? $variables['attributes']['class'] : array()
      );

      return image_add_svg_attributes($svg_contents, $variables['attributes']);
    }
  }
}