class SvgExtractor
Plugin implementation of the icon_extractor.
This extractor needs the file content, only local SVG are allowed to avoid any security risk. For remote sources, `path` extractor must be used or `svg_sprite` for remote sprite.
@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\SvgExtractor 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 SvgExtractor
1 file declares its use of SvgExtractor
- SvgExtractorTest.php in core/
tests/ Drupal/ Tests/ Core/ Theme/ Icon/ Plugin/ SvgExtractorTest.php
File
-
core/
lib/ Drupal/ Core/ Theme/ Plugin/ IconExtractor/ SvgExtractor.php, line 26
Namespace
Drupal\Core\Theme\Plugin\IconExtractorView source
class SvgExtractor extends IconExtractorWithFinder {
/**
* {@inheritdoc}
*/
public function discoverIcons() : array {
// Check is included in getFilesFromSources(), but we need to disallow
// remote sources before.
$this->checkRequiredConfigSources();
$this->configuration['config']['sources'] = array_filter($this->configuration['config']['sources'], function ($source) {
return empty(parse_url($source, PHP_URL_SCHEME));
});
if (empty($this->configuration['config']['sources'])) {
return [];
}
$files = $this->getFilesFromSources();
if (empty($files)) {
return [];
}
$icons = [];
foreach ($files as $file) {
if (!isset($file['absolute_path']) || empty($file['absolute_path'])) {
continue;
}
$id = IconDefinition::createIconId($this->configuration['id'], $file['icon_id']);
$icons[$id] = [
'absolute_path' => $file['absolute_path'],
'source' => $file['source'],
'group' => $file['group'] ?? NULL,
];
}
return $icons;
}
/**
* {@inheritdoc}
*/
public function loadIcon(array $icon_data) : ?IconDefinitionInterface {
if (!isset($icon_data['icon_id']) || !isset($icon_data['source']) || !isset($icon_data['absolute_path'])) {
return NULL;
}
if (!($svg_data = $this->extractSvg($icon_data['absolute_path']))) {
return NULL;
}
return $this->createIcon($icon_data['icon_id'], $icon_data['source'], $icon_data['group'] ?? NULL, $svg_data);
}
/**
* Extract svg values, simply exclude parent <svg>.
*
* @param string $source
* Local path or url to the svg file.
*
* @return array|null
* The SVG `content` as string and `viewbox` value if any.
*/
private function extractSvg(string $source) : ?array {
if (!($content = $this->iconFinder
->getFileContents($source))) {
return NULL;
}
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 NULL;
}
$return = [
'content' => '',
'attributes' => new Attribute(),
];
foreach ($svg as $child) {
$return['content'] .= $child->asXML();
}
if (empty($return['content'])) {
return NULL;
}
// Content contain xml data and will be printed, we need to not escape it
// for rendering.
$return['content'] = new FormattableMarkup($return['content'], []);
// Add svg attributes to be available in the template.
foreach ($svg->attributes() as $name => $value) {
$return['attributes']->setAttribute($name, (string) $value);
}
return $return;
}
}
Members
Title Sort descending | Deprecated | Modifiers | Object type | Summary | Overriden Title |
---|---|---|---|---|---|
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::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(). | ||
SvgExtractor::discoverIcons | public | function | Get a list of all the icons discovered by this extractor. | Overrides IconExtractorInterface::discoverIcons | |
SvgExtractor::extractSvg | private | function | Extract svg values, simply exclude parent <svg>. | ||
SvgExtractor::loadIcon | public | function | Load an icon object. | Overrides IconExtractorBase::loadIcon |
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.