Created
October 9, 2021 16:23
-
-
Save cepheiVV/17c4fc84be8c50a9419070eb1e89ab1a to your computer and use it in GitHub Desktop.
TYPO3 Fluid SvgViewHelper
This file contains hidden or 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
<?php | |
namespace Vendor\Extension\ViewHelpers; | |
/*************************************************************** | |
* | |
* Copyright notice | |
* | |
* (c) 2016 Andrii Pozdieiev <[email protected]>, Mosa Al-Husseini <[email protected]>, Pixelant AB | |
* | |
* All rights reserved | |
* | |
* This script is part of the TYPO3 project. The TYPO3 project is | |
* free software; you can redistribute it and/or modify | |
* it under the terms of the GNU General Public License as published by | |
* the Free Software Foundation; either version 3 of the License, or | |
* (at your option) any later version. | |
* | |
* The GNU General Public License can be found at | |
* http://www.gnu.org/copyleft/gpl.html. | |
* | |
* This script is distributed in the hope that it will be useful, | |
* but WITHOUT ANY WARRANTY; without even the implied warranty of | |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
* GNU General Public License for more details. | |
* | |
* This copyright notice MUST APPEAR in all copies of the script! | |
***************************************************************/ | |
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractTagBasedViewHelper; | |
use TYPO3\CMS\Core\Core\Environment; | |
/** | |
* | |
*/ | |
class SvgViewHelper extends AbstractTagBasedViewHelper | |
{ | |
public function initializeArguments() | |
{ | |
parent::initializeArguments(); | |
$this->registerArgument('class', 'string', 'Specifies an alternate class for the svg', false); | |
$this->registerArgument('width', 'float', 'Specifies a width for the svg', false); | |
$this->registerArgument('height', 'float', 'Specifies a height for the svg', false); | |
$this->registerArgument('source', 'string', 'The path to the SVG file', true); | |
} | |
/** | |
* Generate a list from the content | |
*/ | |
public function render() | |
{ | |
$path = Environment::getPublicPath(); | |
$source = $path.$this->arguments['source']; | |
if (file_exists($source)) { | |
return $this->getInlineSvg($source); | |
} | |
} | |
/** | |
* @param string $source | |
* | |
* @return string | |
*/ | |
protected function getInlineSvg($source) | |
{ | |
if (!file_exists($source)) { | |
return ''; | |
} | |
$svgContent = file_get_contents($source); | |
$svgContent = preg_replace('/<script[\s\S]*?>[\s\S]*?<\/script>/i', '', $svgContent); | |
// Disables the functionality to allow external entities to be loaded when parsing the XML, must be kept | |
$previousValueOfEntityLoader = libxml_disable_entity_loader(true); | |
$svgElement = simplexml_load_string($svgContent); | |
libxml_disable_entity_loader($previousValueOfEntityLoader); | |
// remove xml version tag | |
$domXml = dom_import_simplexml($svgElement); | |
if (isset($this->arguments['class'])) { | |
$domXml->setAttribute('class', $this->arguments['class']); | |
} | |
if (isset($this->arguments['width'])) { | |
$domXml->setAttribute('width', $this->arguments['width']); | |
} | |
if (isset($this->arguments['height'])) { | |
$domXml->setAttribute('height', $this->arguments['height']); | |
} | |
return $domXml->ownerDocument->saveXML($domXml->ownerDocument->documentElement); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment