-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create custom handler for codepoints
- Loading branch information
Showing
5 changed files
with
166 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
modules/ui_icons_library/tests/modules/ui_icons_test/ui_icons_test.libraries.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
material: | ||
css: | ||
base: | ||
https://cdn.jsdelivr.net/npm/[email protected]/iconfont/material-icons.min.css: {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
<?php | ||
|
||
namespace Drupal\ui_icons\Asset; | ||
|
||
use Drupal\iconset\Asset\AssetInterface; | ||
|
||
/** | ||
* ... | ||
*/ | ||
class CodepointsAsset implements AssetInterface { | ||
|
||
|
||
/** | ||
* The list of icon data keyed by the icon ID. | ||
* | ||
* @var array | ||
*/ | ||
protected $icons; | ||
|
||
/** | ||
* ... | ||
* | ||
* @param string $url | ||
* ... | ||
*/ | ||
public function __construct(string $url) { | ||
$this->icons = $this->fetchCodepoints($url); | ||
} | ||
|
||
/** | ||
* | ||
*/ | ||
protected function fetchCodepoints(string $url): array { | ||
$icons = []; | ||
|
||
$data = file_get_contents($url); | ||
if ($data === FALSE) { | ||
return []; | ||
} | ||
|
||
$lines = explode("\n", $data); | ||
foreach ($lines as $line) { | ||
if (empty($line)) { | ||
continue; | ||
} | ||
[$icon_id, $codepoint] = explode(' ', $line); | ||
$icons[$icon_id] = [ | ||
'label' => $icon_id, | ||
]; | ||
} | ||
return $icons; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getIcons() { | ||
return $this->icons; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getIcon($icon_id) { | ||
return $this->icons[$icon_id] ?? FALSE; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
<?php | ||
|
||
namespace Drupal\ui_icons\Plugin\Iconset; | ||
|
||
use Drupal\Core\Plugin\PluginBase; | ||
use Drupal\iconset\Asset\AssetInterface; | ||
use Drupal\iconset\IconsetInterface; | ||
use Drupal\iconset\Plugin\IconHandlerInterface; | ||
use Drupal\ui_icons\Asset\CodepointsAsset; | ||
|
||
/** | ||
* ... | ||
* | ||
* @IconsetIconHandler( | ||
* id = "codepoints", | ||
* label = @Translation("Codepoints"), | ||
* ) | ||
*/ | ||
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'; | ||
} | ||
|
||
/** | ||
* | ||
*/ | ||
public function build($icon_id, AssetInterface $iconset, array $options = []) { | ||
// @todo don't hardcode this here | ||
return [ | ||
"#type" => "html_tag", | ||
"#tag" => "span", | ||
"#value" => $icon_id, | ||
"#attributes" => [ | ||
"class" => [ | ||
"material-icons-outlined", | ||
], | ||
], | ||
"#attached" => [ | ||
"library" => [ | ||
"ui_icons_test/material", | ||
], | ||
], | ||
]; | ||
} | ||
|
||
/** | ||
* | ||
*/ | ||
public function getJsSettings() { | ||
// @todo Implement getJsSettings() method. | ||
return []; | ||
} | ||
|
||
/** | ||
* | ||
*/ | ||
public function formatJson(AssetInterface $asset) { | ||
// @todo Implement formatJson() method. | ||
return []; | ||
} | ||
|
||
} |