From ea847996cf136518ddb55b06872519757bf5d86a Mon Sep 17 00:00:00 2001 From: Philipp Dieter Date: Sat, 7 May 2022 02:33:22 +0200 Subject: [PATCH] [TASK] Add stack to mark external links --- Classes/UserFunc/ParseExternalLinks.php | 78 +++++++++++++++++++ Classes/ViewHelpers/ImageAppendViewHelper.php | 78 +++++++++++++++++++ .../TypoScript/Setup/parseExternalLinks.ts | 12 +++ src/styles/link-external.sass | 34 ++++++++ 4 files changed, 202 insertions(+) create mode 100644 Classes/UserFunc/ParseExternalLinks.php create mode 100644 Classes/ViewHelpers/ImageAppendViewHelper.php create mode 100644 Resources/Private/TypoScript/Setup/parseExternalLinks.ts create mode 100644 src/styles/link-external.sass diff --git a/Classes/UserFunc/ParseExternalLinks.php b/Classes/UserFunc/ParseExternalLinks.php new file mode 100644 index 0000000..2051aca --- /dev/null +++ b/Classes/UserFunc/ParseExternalLinks.php @@ -0,0 +1,78 @@ +, 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( + '' + . $innerHtml + . '' + ); + return $document->find('body')[0]->innerHtml(); + } +} diff --git a/Classes/ViewHelpers/ImageAppendViewHelper.php b/Classes/ViewHelpers/ImageAppendViewHelper.php new file mode 100644 index 0000000..4a8b2e0 --- /dev/null +++ b/Classes/ViewHelpers/ImageAppendViewHelper.php @@ -0,0 +1,78 @@ + + * + ***/ + +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 + ); + + + + } + + } + +} diff --git a/Resources/Private/TypoScript/Setup/parseExternalLinks.ts b/Resources/Private/TypoScript/Setup/parseExternalLinks.ts new file mode 100644 index 0000000..7a66668 --- /dev/null +++ b/Resources/Private/TypoScript/Setup/parseExternalLinks.ts @@ -0,0 +1,12 @@ +page.10.stdWrap.parseFunc { + htmlSanitize = 0 + externalBlocks = a + externalBlocks { + a.stdWrap.postUserFunc = Cjel\TemplatesAide\UserFunc\ParseExternalLinks->render + a.stdWrap.postUserFunc { + iconFile = EXT:site_templates/Resources/Private/Partials/Atoms/IconExternalLinkFill.html + iconPosistion = end + linkText = Externer Link zu + } + } +} diff --git a/src/styles/link-external.sass b/src/styles/link-external.sass new file mode 100644 index 0000000..97de655 --- /dev/null +++ b/src/styles/link-external.sass @@ -0,0 +1,34 @@ +.link-external + .link-external-inner + position: relative + .icon-circle-right + display: none + .icon-link-external + display: inline-block + position: relative + top: 2px + margin-left: 4px + //right: 2px + width: 16px + line-height: 1 + div + display: inline-block + .svg-icon-element, + .svg-icon + display: block + width: 16px + height: 16px + .link-external-sr-only + position: absolute !important + margin: 0 + border: 0 + padding: 0 + width: 1px + height: 1px + overflow: hidden + white-space: nowrap + clip: rect(1px 1px 1px 1px) + clip: rect(1px, 1px, 1px, 1px) + clip-path: inset(50%) + &.imagelink + padding-left: 0