You are here

function template_preprocess_icon in Icon API 8

Same name and namespace in other branches
  1. 7 includes/theme.inc \template_preprocess_icon()

Implements hook_preprocess_icon().

File

includes/theme.inc, line 64
theme.inc Provides hooks and functions for theme related tasks.

Code

function template_preprocess_icon(&$variables) {
  $bundle =& $variables['bundle'];
  $icon =& $variables['icon'];
  if (empty($bundle) || empty($icon)) {
    return;
  }
  if (is_string($bundle)) {
    $bundle = icon_bundle_load($bundle);
  }
  $render_hooks = array_keys(icon_render_hooks());

  // Check to see if the bundle is valid, the icon is a part of the bundle and
  // the bundle has a valid render hook assigned.
  if (!$bundle || !$bundle['status'] || !(isset($bundle['icons'][$icon]) || in_array($icon, $bundle['icons']) || !in_array($bundle['render'], $render_hooks))) {
    $bundle = FALSE;
    $icon = FALSE;
    return;
  }

  // Add classes to the icon wrapper_attributes variable.
  $wrapper_attributes =& $variables['wrapper_attributes'];
  if (!empty($variables['wrapper_class'])) {
    $classes = array();
    foreach (explode(' ', $variables['wrapper_class']) as $class) {
      if ($class = trim(\Drupal::token()
        ->replace($class, $variables['token_data'], array(
        'clear' => TRUE,
      )))) {
        $classes[] = \Drupal\Component\Utility\Html::getClass($class);
      }
    }
    if ($classes) {
      if (!isset($wrapper_attributes['class'])) {
        $wrapper_attributes['class'] = array();
      }
      $wrapper_attributes['class'] = array_unique(array_merge($wrapper_attributes['class'], $classes));
    }
  }

  // Add default icon class.
  $attributes =& $variables['attributes'];
  if (!isset($attributes['class'])) {
    $attributes['class'] = array();
  }
  $attributes['class'][] = 'icon';

  // Prevent screen readers from reading icons.
  $attributes['aria-hidden'] = 'true';

  // Load additional libraries, CSS, JavaScript an other custom
  // attached data associated with this bundle.
  icon_process_attached($bundle);
}