function SvgExtractor::extractSvg

Extract svg values, simply exclude parent <svg>.

Parameters

string $source: Local path or url to the svg file.

Return value

array|null The SVG `content` as string and `viewbox` value if any.

1 call to SvgExtractor::extractSvg()
SvgExtractor::loadIcon in core/lib/Drupal/Core/Theme/Plugin/IconExtractor/SvgExtractor.php
Load an icon object.

File

core/lib/Drupal/Core/Theme/Plugin/IconExtractor/SvgExtractor.php, line 104

Class

SvgExtractor
Plugin implementation of the icon_extractor.

Namespace

Drupal\Core\Theme\Plugin\IconExtractor

Code

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;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.