class SvgSpriteExtractor
Plugin implementation of the icon_extractor.
This extractor needs the file content only to extract ids, no SVG is parse or printed.
@internal This API is experimental.
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements \Drupal\Component\Plugin\PluginInspectionInterface, \Drupal\Component\Plugin\DerivativeInspectionInterface
- class \Drupal\Core\Theme\Icon\IconExtractorBase extends \Drupal\Component\Plugin\PluginBase implements \Drupal\Core\Theme\Icon\IconExtractorInterface, \Drupal\Core\Plugin\PluginWithFormsInterface uses \Drupal\Core\Plugin\PluginWithFormsTrait
- class \Drupal\Core\Theme\Icon\IconExtractorWithFinder extends \Drupal\Core\Theme\Icon\IconExtractorBase implements \Drupal\Core\Theme\Icon\IconExtractorWithFinderInterface, \Drupal\Core\Plugin\PluginWithFormsInterface, \Drupal\Core\Plugin\ContainerFactoryPluginInterface uses \Drupal\Core\Plugin\PluginWithFormsTrait
- class \Drupal\Core\Theme\Plugin\IconExtractor\SvgSpriteExtractor extends \Drupal\Core\Theme\Icon\IconExtractorWithFinder
- class \Drupal\Core\Theme\Icon\IconExtractorWithFinder extends \Drupal\Core\Theme\Icon\IconExtractorBase implements \Drupal\Core\Theme\Icon\IconExtractorWithFinderInterface, \Drupal\Core\Plugin\PluginWithFormsInterface, \Drupal\Core\Plugin\ContainerFactoryPluginInterface uses \Drupal\Core\Plugin\PluginWithFormsTrait
- class \Drupal\Core\Theme\Icon\IconExtractorBase extends \Drupal\Component\Plugin\PluginBase implements \Drupal\Core\Theme\Icon\IconExtractorInterface, \Drupal\Core\Plugin\PluginWithFormsInterface uses \Drupal\Core\Plugin\PluginWithFormsTrait
Expanded class hierarchy of SvgSpriteExtractor
1 file declares its use of SvgSpriteExtractor
- SvgSpriteExtractorTest.php in core/
tests/ Drupal/ Tests/ Core/ Theme/ Icon/ Plugin/ SvgSpriteExtractorTest.php
File
-
core/
lib/ Drupal/ Core/ Theme/ Plugin/ IconExtractor/ SvgSpriteExtractor.php, line 22
Namespace
Drupal\Core\Theme\Plugin\IconExtractorView source
class SvgSpriteExtractor extends IconExtractorWithFinder {
/**
* {@inheritdoc}
*/
public function discoverIcons() : array {
$files = $this->getFilesFromSources();
if (empty($files)) {
return [];
}
$icons = [];
foreach ($files as $file) {
$icon_ids = $this->extractIdsFromXml($file['absolute_path'] ?? '');
foreach ($icon_ids as $icon_id) {
$id = IconDefinition::createIconId($this->configuration['id'], (string) $icon_id);
$icons[$id] = [
'absolute_path' => $file['absolute_path'],
'source' => $file['source'],
'group' => $file['group'] ?? NULL,
];
}
}
return $icons;
}
/**
* Extract icon ID from XML.
*
* @param string $source
* Local path or url to the svg file.
*
* @return array
* A list of icons with keys:
* - icon_ids: array of icon Id found
* - attributes: Attribute object from the svg
*/
private function extractIdsFromXml(string $source) : array {
if (!($content = $this->iconFinder
->getFileContents($source))) {
return [];
}
libxml_use_internal_errors(TRUE);
if (!($svg = simplexml_load_string((string) $content))) {
// @todo do we need to log a warning with the xml error?
return [];
}
return $this->extractIdsFromSymbols($svg->symbol) ?: $this->extractIdsFromSymbols($svg->defs->symbol ?? NULL);
}
/**
* Extract icon ID from SVG symbols.
*
* @param \SimpleXMLElement|null $wrapper
* A SVG element.
*
* @return array
* A list of icons ID.
*/
private function extractIdsFromSymbols(?\SimpleXMLElement $wrapper) : array {
if ($wrapper === NULL) {
return [];
}
$ids = [];
foreach ($wrapper as $symbol) {
if (isset($symbol['id']) && 0 === preg_match('/[^\\w-]/', (string) $symbol['id'])) {
$ids[] = (string) $symbol['id'];
}
}
return $ids;
}
}
Members
Title Sort descending | Deprecated | Modifiers | Object type | Summary | Overriden Title | Overrides |
---|---|---|---|---|---|---|
IconExtractorBase::buildConfigurationForm | public | function | Form constructor. | Overrides PluginFormInterface::buildConfigurationForm | ||
IconExtractorBase::createIcon | public | function | Create the icon definition from an extractor plugin. | Overrides IconExtractorInterface::createIcon | ||
IconExtractorBase::DEFINITION_REMOVE | private | constant | ||||
IconExtractorBase::description | public | function | Returns the translated plugin description. | Overrides IconExtractorInterface::description | ||
IconExtractorBase::label | public | function | Returns the translated plugin label. | Overrides IconExtractorInterface::label | ||
IconExtractorBase::loadIcon | public | function | Load an icon object. | Overrides IconExtractorInterface::loadIcon | 1 | |
IconExtractorBase::submitConfigurationForm | public | function | Form submission handler. | Overrides PluginFormInterface::submitConfigurationForm | ||
IconExtractorBase::validateConfigurationForm | public | function | Form validation handler. | Overrides PluginFormInterface::validateConfigurationForm | ||
IconExtractorWithFinder::checkRequiredConfigSources | protected | function | Check the required `config > sources` value from definition. | |||
IconExtractorWithFinder::create | public static | function | Creates an instance of the plugin. | Overrides ContainerFactoryPluginInterface::create | ||
IconExtractorWithFinder::getFilesFromSources | public | function | Create files data from sources config. | Overrides IconExtractorWithFinderInterface::getFilesFromSources | ||
IconExtractorWithFinder::__construct | public | function | Constructs a IconExtractorWithFinder object. | Overrides PluginBase::__construct | ||
PluginBase::$configuration | protected | property | Configuration information passed into the plugin. | |||
PluginBase::$pluginDefinition | protected | property | The plugin implementation definition. | |||
PluginBase::$pluginId | protected | property | The plugin ID. | |||
PluginBase::DERIVATIVE_SEPARATOR | constant | A string which is used to separate base plugin IDs from the derivative ID. | ||||
PluginBase::getBaseId | public | function | Gets the base_plugin_id of the plugin instance. | Overrides DerivativeInspectionInterface::getBaseId | ||
PluginBase::getDerivativeId | public | function | Gets the derivative_id of the plugin instance. | Overrides DerivativeInspectionInterface::getDerivativeId | ||
PluginBase::getPluginDefinition | public | function | Gets the definition of the plugin implementation. | Overrides PluginInspectionInterface::getPluginDefinition | ||
PluginBase::getPluginId | public | function | Gets the plugin ID of the plugin instance. | Overrides PluginInspectionInterface::getPluginId | ||
PluginBase::isConfigurable | Deprecated | public | function | Determines if the plugin is configurable. | ||
PluginWithFormsTrait::getFormClass | public | function | Implements \Drupal\Core\Plugin\PluginWithFormsInterface::getFormClass(). | |||
PluginWithFormsTrait::hasFormClass | public | function | Implements \Drupal\Core\Plugin\PluginWithFormsInterface::hasFormClass(). | |||
SvgSpriteExtractor::discoverIcons | public | function | Get a list of all the icons discovered by this extractor. | Overrides IconExtractorInterface::discoverIcons | ||
SvgSpriteExtractor::extractIdsFromSymbols | private | function | Extract icon ID from SVG symbols. | |||
SvgSpriteExtractor::extractIdsFromXml | private | function | Extract icon ID from XML. |
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.