b13 / assetcollector

Add CSS / SVG / JS to Content templates, and load them only once
GNU General Public License v2.0
8 stars 0 forks source link

Add unique Id to SVG indentifier #23

Open m-kappenberg opened 3 months ago

m-kappenberg commented 3 months ago

Is it possible to append a unique ID to the SVG identifier? This should avoid trouble with same named SVG files from diffrent folders on same page, eg:

Maybe change the SvgViewHelper.php from ~66:

        $this->assetCollector->addXmlFile($file);
        $iconIdentifier = $this->assetCollector->getIconIdentifierFromFileName($file);

to:

        $fileAbsPath = GeneralUtility::getFileAbsFileName($file);
// early return if it seems that the file does not exist (is_file or file_exists, not sure here)
        if (!is_file($fileAbsPath)) {
            return '<!-- Please check path: ' . $file . ' -->';
        }
        $this->assetCollector->addXmlFile($fileAbsPath);
// feed in absolute path, to have same value as in AssetCollector Class
        $iconIdentifier = $this->assetCollector->getIconIdentifierFromFileName($fileAbsPath);

and in the AssetCollector.php ~118 then:

    public function getIconIdentifierFromFileName(string $xmlFile): string
    {
        // maybe other approach here for unique ID without messing up generated HTML to much
        $uniqueId = substr(sha1($xmlFile), 0, 8);
        return str_replace('.svg', '', basename($xmlFile)) . '-'. $uniqueId;
    }

and in the AssetCollector.php ~102, too:

    public function addXmlFile(string $xmlFile): void
    {
        // maybe security check, if file is in public path and a SVG file (is_file or file_exists, not sure here)
        if (file_exists($xmlFile)) {
            $this->xmlFiles[] = $xmlFile;
        } else {
            $xmlFile = preg_replace('/^\//', '', $xmlFile); // or: ltrim($xmlFile, '/');
            $this->xmlFiles[] = GeneralUtility::getFileAbsFileName($xmlFile);
        }
    }

Best regards and many thanks for the extension :-)