Skip to content

Instantly share code, notes, and snippets.

@bwente
Last active August 19, 2022 17:11
Show Gist options
  • Save bwente/391ee96ccc402aa631667dfe06fe2634 to your computer and use it in GitHub Desktop.
Save bwente/391ee96ccc402aa631667dfe06fe2634 to your computer and use it in GitHub Desktop.
svgLoader -- MODX snippet to load an SVG file and modify the style.
<?php
$svg_file = $modx->getOption('file',$scriptProperties, '');
$svg_id = $modx->getOption('id',$scriptProperties,'mySVGId');
$svg_class = $modx->getOption('class',$scriptProperties,'mySVGClass');
$svg_name = $modx->getOption('name',$scriptProperties,'SVG');
$svg_height = $modx->getOption('height',$scriptProperties,'440px');
$svg_width = $modx->getOption('width',$scriptProperties,'440px');
$svg_background_color = $modx->getOption('background_color',$scriptProperties,'transparent');
$svg_svgstyle = $modx->getOption('svgstyle',$scriptProperties,'top:2px;position:inherit;');
$svg_fill = $modx->getOption('fill',$scriptProperties,'#369');
$svg_rotation = $modx->getOption('rotation',$scriptProperties,'0');
$svg_style = $modx->getOption('styles',$scriptProperties,'');
$svg_title = $modx->getOption('title',$scriptProperties,'graphic image');
$svg_svgstyle = 'background-color:' . $svg_background_color . ';' . $svg_svgstyle;
$svg=simplexml_load_file($svg_file);
$svg->addAttribute('id', $svg_id);
$svg->addAttribute('class', $svg_class);
$svg->addAttribute('data-name', $svg_name);
$svg->addAttribute('height', $svg_height);
$svg->addAttribute('width', $svg_width);
$svg->addAttribute('transform', 'rotate('. $svg_rotation . ')');
$svg->addAttribute('fill', $svg_fill);
$svg->addAttribute('aria-hidden', 'true');
//$svg->addAttribute('title', $svg_title);
$svg->addAttribute('role', 'img');
$svg->addAttribute('aria-label', str_replace(' ', '_', $svg_title));
$svg->addChild('title', $svg_title);
$svg->title->addAttribute('id', str_replace(' ', '_', $svg_title));
$svg->addChild('style', $svg_style);
$svg->attributes()->style = $svg_svgstyle;
$newSVG = $svg->asXML();
return $newSVG;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment