ex_icons-8.x-1.0/ex_icons.module
ex_icons.module
<?php
/**
* @file
* Adds an API to manage and use icons expressed in a SVG sprite sheet.
*/
/**
* Implements hook_help().
*/
function ex_icons_help($route_name) {
switch ($route_name) {
case 'help.page.ex_icons':
$output = '';
$output .= '<h3>' . t('About') . '</h3>';
$output .= '<p>' . t('The External-use Icons module creates a bridge between other modules or themes in integrating <a href=":css_tricks_article">external-referencing SVG icons</a> defined in sprite sheets into Drupal.', [':css_tricks_article' => 'https://css-tricks.com/svg-use-with-external-reference-take-2/']) . '</p>';
$output .= '<p>' . t('This module provides a visual UI for end users to be able to select icons. provided by extensions. This module provides no icons by itself.') . '</p>';
$output .= '<p>' . t('For more information, see the <a href=":doc_url">online project page for the External-use Icons module</a>.', [':doc_url' => 'https://www.drupal.org/project/ex_icons']) . '</p>';
return $output;
}
}
/**
* Implements hook_theme().
*/
function ex_icons_theme() {
return [
'ex_icon' => [
'variables' => [
'id' => '',
'title' => '',
'attributes' => [],
'title_attributes' => [],
],
],
'form_element__ex_icon_select' => [
'base hook' => 'form_element',
],
'form_element_label__ex_icon_select' => [
'base hook' => 'form_element',
],
'radios__ex_icon_select' => [
'base hook' => 'radios',
],
];
}
/**
* Prepares variables for svg icon templates.
*
* Default template: ex-icon.html.twig.
*
* @param array $variables
* An associative array containing:
* - id: The plugin definition ID of the icon to use.
* - title: The accessible label for the icon, the semantic meaning of it for
* assistive technology.
* - attributes: The HTML attributes to apply to the SVG element.
* - title_attributes: The HTML attributes to apply to the title element.
*/
function template_preprocess_ex_icon(array &$variables) {
/** @var \Drupal\ex_icons\ExIconsManager $icons_manager */
$icons_manager = \Drupal::service('ex_icons.manager');
/** @var \Drupal\ex_icons\ExIconInterface $icon */
$icon = $icons_manager->getInstance(['id' => $variables['id'] ?: '']);
$variables['attributes'] += ['aria-hidden' => 'true'];
$variables['title_attributes']['class'][] = 'visually-hidden';
// If one dimension attribute is set (not both):
if (isset($variables['attributes']['width']) xor isset($variables['attributes']['height'])) {
$numeric_width = isset($variables['attributes']['width']) && is_numeric($variables['attributes']['width']);
$numeric_height = isset($variables['attributes']['height']) && is_numeric($variables['attributes']['height']);
if ($numeric_width) {
$variables['attributes']['height'] = $variables['attributes']['width'] / $icon->getAspectRatio();
}
elseif ($numeric_height) {
$variables['attributes']['width'] = $variables['attributes']['height'] * $icon->getAspectRatio();
}
}
$variables['url'] = $icon->getUrl();
\Drupal::service('renderer')->addCacheableDependency($variables, $icons_manager);
}
/**
* Prepares variables for icon selection form element item templates.
*
* Default template: form-element--ex-icon-select.html.twig.
*
* @param array $variables
* An associative array containing:
* -element: An associative array containing the properties of the element.
* Properties used: #title, #title_display, #description, #id, #required,
* #children, #type, #name, #label_for.
*
* @see template_preprocess_form_element()
*/
function template_preprocess_form_element__ex_icon_select(array &$variables) {
$variables['label']['#theme'] = 'form_element_label__ex_icon_select';
}
/**
* Implements hook_page_attachments().
*/
function ex_icons_page_attachments(array &$attachments) {
$icons_manager = \Drupal::service('ex_icons.manager');
$paths = [];
foreach (array_keys($icons_manager->getDefinitions()) as $id) {
// Skip the fall-back null instance.
if ($id == 'ex_icon_null') {
continue;
}
$instance = $icons_manager->getInstance(['id' => $id]);
$provider = $instance->getProvider();
// Already got a URL for this provider.
if (isset($paths[$provider])) {
continue;
}
// Use first discovered icon's URL for each provider, removing it's hash
// portion to be more generic.
$paths[$provider] = strstr($instance->getUrl(), '#', TRUE);
}
$attachments['#attached']['drupalSettings']['exIcons']['paths'] = $paths;
}
/**
* Implements hook_ex_icons_alter().
*/
function ex_icons_ex_icons_alter(array &$definitions) {
$definitions['ex_icon_null'] = [
'id' => 'ex_icon_null',
'width' => 1,
'height' => 1,
'url' => '',
'provider' => 'ex_icons',
'class' => 'Drupal\\ex_icons\\ExIcon',
];
}
/**
* Implements hook_themes_installed().
*/
function ex_icons_themes_installed($theme_list) {
\Drupal::service('ex_icons.manager')->clearCachedDefinitions();
}
/**
* Implements hook_themes_uninstalled().
*/
function ex_icons_themes_uninstalled($theme_list) {
\Drupal::service('ex_icons.manager')->clearCachedDefinitions();
}
/**
* Implements hook_modules_installed().
*/
function ex_icons_modules_installed($theme_list) {
\Drupal::service('ex_icons.manager')->clearCachedDefinitions();
}
/**
* Implements hook_modules_uninstalled().
*/
function ex_icons_modules_uninstalled($theme_list) {
\Drupal::service('ex_icons.manager')->clearCachedDefinitions();
}
