From 3607ba4ec30aa8b66bccd3997bacaa816cc787aa Mon Sep 17 00:00:00 2001 From: Cellou Diallo Date: Mon, 10 Jun 2024 15:55:20 +0200 Subject: [PATCH] adding configuration information to avoid hardcoding data --- .../src/Controller/IconsetsController.php | 1 + .../ui_icons_test/ui_icons_test.iconset.yml | 15 ++++++- src/Plugin/Iconset/CodepointsHandler.php | 44 ++++++++----------- 3 files changed, 33 insertions(+), 27 deletions(-) diff --git a/modules/ui_icons_library/src/Controller/IconsetsController.php b/modules/ui_icons_library/src/Controller/IconsetsController.php index 6f7afce..40f69f8 100644 --- a/modules/ui_icons_library/src/Controller/IconsetsController.php +++ b/modules/ui_icons_library/src/Controller/IconsetsController.php @@ -44,6 +44,7 @@ public function overview() { ], ]; } + return $build; } diff --git a/modules/ui_icons_library/tests/modules/ui_icons_test/ui_icons_test.iconset.yml b/modules/ui_icons_library/tests/modules/ui_icons_test/ui_icons_test.iconset.yml index b4775e0..66a6580 100644 --- a/modules/ui_icons_library/tests/modules/ui_icons_test/ui_icons_test.iconset.yml +++ b/modules/ui_icons_library/tests/modules/ui_icons_test/ui_icons_test.iconset.yml @@ -26,11 +26,22 @@ uswsd: - '/libraries/uswds/packages/usa-icon/src/img/usa-icons-bg' - '/libraries/uswds/packages/usa-icon/src/img/uswds-icons' -material_icons: - label: 'Material icons' +material_icons_outlined: + label: 'Material icons Outlined' plugin: 'codepoints' + config: + template : '{{ icon_id }}' + library: 'ui_icons_test/material' assets: - 'https://raw.githubusercontent.com/google/material-design-icons/master/font/MaterialIconsOutlined-Regular.codepoints' +material_icons_round: + label: 'Material Icon Round' + plugin: 'codepoints' + config: + template: '{{ icon_id }}' + library: 'ui_icons_test/material' + assets: + - 'https://raw.githubusercontent.com/google/material-design-icons/master/font/MaterialIconsRound-Regular.codepoints' diff --git a/src/Plugin/Iconset/CodepointsHandler.php b/src/Plugin/Iconset/CodepointsHandler.php index 4607ddd..c80d446 100644 --- a/src/Plugin/Iconset/CodepointsHandler.php +++ b/src/Plugin/Iconset/CodepointsHandler.php @@ -18,44 +18,27 @@ */ class CodepointsHandler extends PluginBase implements IconHandlerInterface { - /** - * URL to the codepoints file. - */ - const CODEPOINTS_URL = 'https://raw.githubusercontent.com/google/material-design-icons/master/font/MaterialIconsOutlined-Regular.codepoints'; - /** * {@inheritdoc} */ public function createAssets($asset_info, IconsetInterface $iconset) { - // @todo get url from $asset_info, - return new CodepointsAsset(static::CODEPOINTS_URL); - } - - /** - * {@inheritdoc} - */ - public function supports(IconsetInterface $iconset) { - // Check if the iconset supports this handler. - return $iconset->getType() === 'codepoints'; + return new CodepointsAsset($asset_info); } /** * */ public function build($icon_id, AssetInterface $iconset, array $options = []) { - // @todo don't hardcode this here + $config = $this->configuration; return [ - "#type" => "html_tag", - "#tag" => "span", - "#value" => $icon_id, - "#attributes" => [ - "class" => [ - "material-icons-outlined", - ], + '#type' => 'inline_template', + "#template" => $config['template'], + "#context" => [ + 'icon_id' => $icon_id, ], "#attached" => [ "library" => [ - "ui_icons_test/material", + $config['library'], ], ], ]; @@ -74,7 +57,18 @@ public function getJsSettings() { */ public function formatJson(AssetInterface $asset) { // @todo Implement formatJson() method. - return []; + // return []; + $icons = $asset->getIcons(); + $formatted_icons = []; + + foreach ($icons as $icon_id => $icon_data) { + $formatted_icons[] = [ + 'id' => $icon_id, + 'label' => $icon_data['label'], + ]; + } + + return json_encode($formatted_icons); } }