Skip to content
Snippets Groups Projects
Commit ccc40985 authored by David Galeano's avatar David Galeano
Browse files

Issue #3480854 by gxleano: Add settings form to allow collections and create...

Issue #3480854 by gxleano: Add settings form to allow collections and create hook_ui_icons_alter() to generate icon packs based on user selection
parent 07d825b4
No related branches found
No related tags found
2 merge requests!23Issue #3480854 by gxleano: Add settings form to allow collections and create...,!22Issue #3480854 by gxleano: Add settings form to allow collections and create...
iconify_icons_provider.settings:
title: 'Iconify Icons Provider'
parent: system.admin_config_content
description: 'Iconify Icons Provider Settings.'
route_name: iconify_icons_provider.settings
<?php
/**
* @file
* Drupal's integration with Iconify.
*/
/**
* Implements hook_ui_icons_alter().
*
* Alter the UI icon pack definitions.
*
* @throws \GuzzleHttp\Exception\GuzzleException
* @throws \JsonException
*/
function iconify_icons_provider_ui_icons_alter(array &$icon_pack_definitions): void {
$config = Drupal::configFactory()->get('iconify_icons_provider.settings');
$collections = $config->get('collections');
// Iterate through each collection and create a corresponding icon pack.
foreach ($collections as $collection) {
// Generate a machine name for the icon pack based on the collection name.
$icon_pack_name = _iconify_icons_provider_generate_icon_pack_name($collection);
// Check if the icon pack definition already exists, and skip if it does.
if (!isset($icon_pack_definitions[$icon_pack_name])) {
// Generate the icon pack definition based on the collection.
$icon_pack_definitions[$icon_pack_name] = _iconify_icons_provider_create_icon_pack_definition($collection);
}
}
}
/**
* Helper function to generate a machine name for an icon pack.
*
* @param string $collection_name
* The original collection name in the format 'prefix-suffix'.
*
* @return string
* The machine name in the format 'iconify_prefix_suffix'.
*/
function _iconify_icons_provider_generate_icon_pack_name(string $collection_name): string {
// Define the pattern to match any 'prefix-suffix' style keys.
$pattern = '/^(\w+)-(\w+)$/';
// Use preg_replace_callback to transform the collection name into a machine
// name.
return preg_replace_callback($pattern, function ($matches) {
return 'iconify_' . $matches[1] . '_' . $matches[2];
}, $collection_name);
}
/**
* Helper function to create an icon pack definition from a collection.
*
* @param string $collection
* The icon collection name.
*
* @return array
* The icon pack definition array.
*/
function _iconify_icons_provider_create_icon_pack_definition(string $collection): array {
// Build the icon pack definition based on the template structure.
return [
'enabled' => TRUE,
'label' => 'Iconify ' . $collection,
'extractor' => 'iconify',
'config' => [
'collections' => [$collection],
],
'settings' => [
'size' => [
'title' => 'Size',
'type' => 'integer',
'default' => 32,
],
'flip' => [
'title' => 'Flip',
'type' => 'text',
'enum' => [
'original',
'horizontal',
'vertical',
'horizontal,vertical',
],
],
'rotate' => [
'title' => 'Rotate',
'type' => 'text',
'enum' => [
'0deg',
'90deg',
'180deg',
'270deg',
],
],
],
'template' => _iconify_icons_provider_build_icon_template(),
'provider' => 'iconify_icons_provider',
'id' => _iconify_icons_provider_generate_icon_pack_name($collection),
];
}
/**
* Helper function to build the icon template.
*
* @return string
* The icon template string.
*/
function _iconify_icons_provider_build_icon_template(): string {
// Return the icon template with placeholders for the parameters.
return <<<EOT
{% set params = {
width: size,
height: size,
rotate: rotate,
flip: flip
}|filter(v => v is not null) %}
<img src="{{ source }}?{{ params|url_encode }}" />
EOT;
}
iconify_icons_provider.settings:
path: '/admin/config/iconify_icons_provider/settings'
requirements:
_permission: 'configure custom configuration form'
defaults:
_form: \Drupal\iconify_icons_provider\Form\Settings
_title: 'Iconify Icons Provider settings'
This diff is collapsed.
<?php
namespace Drupal\iconify_icons_provider\Form;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Config\TypedConfigManagerInterface;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Link;
use Drupal\Core\Url;
use Drupal\iconify_icons\IconifyServiceInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Admin settings for Iconify Icons Provider.
*
* @package Drupal\iconify_icons\Form
*/
class Settings extends ConfigFormBase {
/**
* The iconify icons service.
*
* @var \Drupal\iconify_icons\IconifyServiceInterface
*/
protected IconifyServiceInterface $iconify;
/**
* Constructs a Settings object.
*
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* The factory for configuration objects.
* @param \Drupal\Core\Config\TypedConfigManagerInterface $typedConfigManager
* The typed config manager.
* @param \Drupal\iconify_icons\IconifyServiceInterface $iconify
* The iconify icons service.
*/
public function __construct(ConfigFactoryInterface $config_factory, TypedConfigManagerInterface $typedConfigManager, IconifyServiceInterface $iconify) {
parent::__construct($config_factory, $typedConfigManager);
$this->iconify = $iconify;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('config.factory'),
$container->get('config.typed'),
$container->get('iconify_icons.iconify_service')
);
}
/**
* {@inheritdoc}
*/
protected function getEditableConfigNames() {
return ['iconify_icons_provider.settings'];
}
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'iconify_icons_provider_settings';
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$config = $this->config('iconify_icons_provider.settings');
$form = parent::buildForm($form, $form_state);
$form['search'] = [
'#type' => 'textfield',
'#title' => $this->t('Icon Collections'),
'#size' => 60,
'#maxlength' => 128,
'#placeholder' => $this->t('Filter collections'),
'#attributes' => [
'class' => ['iconify-icons-widget-checkboxes-filter'],
],
'#attached' => [
'library' => ['iconify_icons/default'],
],
];
$form['collections'] = [
'#type' => 'checkboxes',
'#options' => $this->getOptions(),
'#default_value' => $config->get('collections') ?? [],
'#description' => $this->t('Select the collections which are going to provide the pack of icons. See @collectionIconsLink list.', [
'@collectionIconsLink' => Link::fromTextAndUrl($this->t('the Iconify icon collections'), Url::fromUri('https://icon-sets.iconify.design/', [
'attributes' => [
'target' => '_blank',
],
]))->toString(),
]),
'#attributes' => [
'class' => ['iconify-icons-widget-collections'],
],
'#attached' => [
'library' => ['iconify_icons/default'],
],
];
return $form;
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$collections = array_filter($form_state->getValue('collections'));
$this->configFactory->getEditable('iconify_icons_provider.settings')
->set('collections', array_combine($collections, $collections))
->save();
parent::submitForm($form, $form_state);
}
/**
* TBD.
*/
protected function getCustomCollectionName(array $collection, string $collection_id): string {
return sprintf(
'<strong>%s</strong> - %s (%d) <a href="https://icon-sets.iconify.design/%s" target="_blank">See icons</a>',
$collection['name'] ?? 'Unknown Name',
$collection['category'] ?? 'Uncategorized',
$collection['total'] ?? 0,
$collection_id
);
}
/**
* TBD.
*/
protected function getOptions(): array {
// Fetch and sort collections by 'total' in descending order.
$collections = $this->iconify->getCollections();
uasort($collections, fn($a, $b) => $b['total'] <=> $a['total']);
$options = [];
foreach ($collections as $collection_id => $collection) {
$options[$collection_id] = $this->getCustomCollectionName($collection, $collection_id);
}
return $options;
}
}
......@@ -87,13 +87,14 @@ class IconifyExtractor extends IconExtractorBase implements ContainerFactoryPlug
if (!is_string($icon_id)) {
continue;
}
$icon_full_id = $this->configuration['icon_pack_id'] . ':' . $icon_id;
$source = sprintf($this->iconify::DESIGN_DOWNLOAD_API_ENDPOINT, $collection, $icon_id);
$icons[$icon_full_id] = self::createIcon($icon_id, $this->configuration, $source);
$icons[] = $this->createIcon($icon_id, $source);
}
}
return $icons;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment