You are here

micon.module in Micon 8

Same filename and directory in other branches
  1. 2.x micon.module

Contains micon.module.

File

micon.module
View source
<?php

/**
 * @file
 * Contains micon.module.
 */
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\micon\Entity\Micon;
use Drupal\micon\MiconIconize;
use Drupal\micon\MiconIconInterface;

/**
 * Implements hook_help().
 */
function micon_help($route_name, RouteMatchInterface $route_match) {
  switch ($route_name) {
    case 'help.page.micon':
      $output = '';
      $output .= '<h3>' . t('About') . '</h3>';
      $output .= '<p>' . t('Micon is an IcoMoon icon manager.') . '</p>';
      return $output;
    default:
  }
}

/**
 * Implements hook_theme().
 */
function micon_theme($existing, $type, $theme, $path) {
  $themes = [];
  $themes['micon'] = [
    'variables' => [
      'title' => NULL,
      'attributes' => [],
      'position' => 'before',
      'icon' => NULL,
      'icon_only' => FALSE,
      'icon_attributes' => [],
    ],
    'file' => 'micon.theme.inc',
  ];
  $themes['micon_icon'] = [
    'variables' => [
      'icon' => NULL,
      'attributes' => [],
    ],
    'file' => 'micon.theme.inc',
  ];
  $themes['micon_package'] = [
    'render element' => 'element',
    'file' => 'micon.theme.inc',
  ];
  $themes['micon_icon_list'] = [
    'render element' => 'element',
    'file' => 'micon.theme.inc',
  ];
  $themes['micon_icon_font'] = [
    'render element' => 'element',
    'file' => 'micon.theme.inc',
  ];
  $themes['micon_icon_image'] = [
    'render element' => 'element',
    'file' => 'micon.theme.inc',
  ];
  return $themes;
}

/**
 * Implements hook_theme_suggestions_HOOK().
 */
function micon_theme_suggestions_micon_icon(array $variables) {
  $icon = $variables['icon'];
  if (is_string($icon)) {
    $icon = \Drupal::service('micon.icon.manager')
      ->getIconMatch($icon);
  }
  if ($icon instanceof MiconIconInterface) {
    return [
      'micon_icon__' . $icon
        ->getType(),
    ];
  }
  return [];
}

/**
 * Implements hook_element_info_alter().
 */
function micon_element_info_alter(array &$types) {
  if (isset($types['html'])) {
    $types['html']['#attached']['library'][] = 'micon/micon';
  }
}

/**
 * Implements hook_library_info_alter().
 *
 * Adds Micon stylesheets to the micon library.
 */
function micon_library_info_alter(&$libraries, $extension) {
  if ($extension === 'micon') {
    foreach (Micon::loadActive() as $micon) {
      $libraries['micon']['dependencies'][] = 'micon/micon.' . $micon
        ->id();
    }
  }
}

/**
 * Implements hook_library_info_build().
 */
function micon_library_info_build() {
  $libraries = [];
  foreach (Micon::loadMultiple() as $micon) {
    if ($stylesheet = $micon
      ->getStylesheet()) {
      $libraries['micon.' . $micon
        ->id()]['css']['theme'][$stylesheet] = [];

      // Add SVG library if necessary.
      if ($micon
        ->type() == 'image') {
        $libraries['micon.' . $micon
          ->id()]['dependencies'][] = 'micon/micon.svg';
      }
    }
  }
  return $libraries;
}

/**
 * A drop-in replacement for the t() function from core.
 *
 * @ingroup sanitization
 */
function micon($string = '', array $args = [], array $options = []) {
  return new MiconIconize($string, $args, $options);
}

Functions