This commit is contained in:
Philipp Dieter
2022-06-13 14:49:21 +02:00
4 changed files with 202 additions and 0 deletions

View File

@@ -0,0 +1,78 @@
<?php
namespace Cjel\TemplatesAide\UserFunc;
/**
* This file is part of the "Site Templates" Extension for TYPO3 CMS.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*
* (c) 2021 Philipp Dieter <philipp@glanzstueck.agency>, Glanzstück GmbH
*/
use DiDom\Document;
use DiDom\Element;
use TYPO3\CMS\Core\Utility\GeneralUtility;
class ParseExternalLinks
{
/**
* @return string
*/
public function render($content, $conf = [])
{
$domDocument = new \DOMDocument();
$testdocument = $domDocument->loadXML($content);
if ($testdocument === false) {
return $content;
}
$document = new Document($content);
$a = $document->find('a')[0];
$href = $a->getAttribute('href');
if (substr($href, 0, 7) != "http://"
&& substr($href, 0, 8) != "https://"
) {
return $content;
}
$parsedHref = parse_url($href);
if ($_SERVER['SERVER_NAME'] == $parsedHref['host']) {
return $content;
}
if ($a->getAttribute('target') == '_blank') {
$a->setAttribute('rel', 'noopener');
}
$class = $a->getAttribute('class');
if ($class) {
$class .= ' link link-external';
} else {
$class = 'link link-external';
}
$a->setAttribute('class', $class);
if ($conf['linkText']) {
$screenreaderHint = new Element('span', $conf['linkText'] . ' ');
$screenreaderHint->setAttribute('class', 'link-external-sr-only');
$a->prependChild($screenreaderHint);
}
if ($conf['iconFile']) {
$iconDataFile = GeneralUtility::getFileAbsFileName(
$conf['iconFile']
);
$iconData = file_get_contents($iconDataFile);
$icon = new Element('span');
$icon->setInnerHtml($iconData);
$icon->setAttribute('class', 'icon-link-external');
if ($iconPosition == 'start') {
$a->prepentChild($icon);
} else {
$a->appendChild($icon);
}
}
$innerHtml = $a->innerHtml();
$a->setInnerHtml(
'<span class="link-external-inner">'
. $innerHtml
. '</span>'
);
return $document->find('body')[0]->innerHtml();
}
}

View File

@@ -0,0 +1,78 @@
<?php
namespace Cjel\TemplatesAide\ViewHelpers;
/***
*
* This file is part of the "Templates Aide" Extension for TYPO3 CMS.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*
* (c) 2021 Philipp Dieter <philippdieter@attic-media.net>
*
***/
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractTagBasedViewHelper;
use TYPO3\CMS\Extbase\Service\ImageService;
use TYPO3\CMS\Core\Imaging\ImageMagickFile;
/**
*
*/
class ImageAppendViewHelper extends AbstractTagBasedViewHelper
{
/**
* ImageService
*
* @var ImageService
*/
protected $imageService;
/**
* @param
*/
public function injectImageService(
ImageService $imageService
) {
$this->imageService = $imageService;
}
/**
* Initialize arguments.
*/
public function initializeArguments()
{
parent::initializeArguments();
$this->registerUniversalTagAttributes();
$this->registerArgument('images', 'array', '');
}
/**
* Resizes a given image (if required) and renders the respective img tag
*
* @see https://docs.typo3.org/typo3cms/TyposcriptReference/ContentObjects/Image/
*
* @throws Exception
* @return string Rendered tag
*/
public function render()
{
foreach ($this->arguments['images'] as $image) {
$imagePath = $image->getForLocalProcessing(false);
//$image = $this->imageService->getImage('', $imageArgument, true);
//$image = $this->imageService->getImageUri($image);
$imageMagickFile = ImageMagickFile::fromFilePath($imagePath, 0);
\TYPO3\CMS\Extbase\Utility\DebuggerUtility::var_dump(
$imageMagickFile, null, 3
);
}
}
}