/** * used to attach srcset variants of a given image to the specified tag * * @param \TYPO3\CMS\Fluid\Core\ViewHelper\TagBuilder $tag the tag to add the srcset as argument * @param string $src image path to render srcsets for * @return array */ public function addSourceSet($tag, $src) { $srcsets = $this->getSourceSetWidths(); if ('BE' === TYPO3_MODE) { FrontendSimulationUtility::simulateFrontendEnvironment(); } $format = $this->arguments['format']; $quality = $this->arguments['quality']; $treatIdAsReference = (bool) $this->arguments['treatIdAsReference']; if (true === $treatIdAsReference) { $src = $this->arguments['src']; } $imageSources = []; $srcsetVariants = []; foreach ($srcsets as $key => $width) { $srcsetVariant = $this->getImgResource($src, $width, $format, $quality, $treatIdAsReference); $srcsetVariantSrc = rawurldecode($srcsetVariant[3]); $srcsetVariantSrc = $this->preprocessSourceUri(GeneralUtility::rawUrlEncodeFP($srcsetVariantSrc)); $imageSources[$srcsetVariant[0]] = ['src' => $srcsetVariantSrc, 'width' => $srcsetVariant[0], 'height' => $srcsetVariant[1]]; $srcsetVariants[$srcsetVariant[0]] = $srcsetVariantSrc . ' ' . $srcsetVariant[0] . 'w'; } $tag->addAttribute('srcset', implode(',', $srcsetVariants)); if ('BE' === TYPO3_MODE) { FrontendSimulationUtility::resetFrontendEnvironment(); } return $imageSources; }
/** * @param $tagName * @param array $attributes * @param string $content * @param bool $forceClosingTag * @return string */ protected function createTag($tagName, array $attributes = NULL, $content = '', $forceClosingTag = FALSE) { $tag = new TagBuilder($tagName, $content); if ($attributes) { $tag->addAttributes($attributes); } $tag->forceClosingTag($forceClosingTag); return $tag->render(); }
/** * Sets the tag name to $this->tagName. * Additionally, sets all tag attributes which were registered in * $this->tagAttributes and additionalArguments. * * Will be invoked just before the render method. * * @return void * @api */ public function initialize() { parent::initialize(); $this->tag->reset(); $this->tag->setTagName($this->tagName); if ($this->hasArgument('additionalAttributes') && is_array($this->arguments['additionalAttributes'])) { $this->tag->addAttributes($this->arguments['additionalAttributes']); } if (isset(self::$tagAttributes[get_class($this)])) { foreach (self::$tagAttributes[get_class($this)] as $attributeName) { if ($this->hasArgument($attributeName) && $this->arguments[$attributeName] !== '') { $this->tag->addAttribute($attributeName, $this->arguments[$attributeName]); } } } }
/** * @return string */ public function render() { if ('BE' === TYPO3_MODE) { return null; } $languages = $this->arguments['languages']; if (true === $languages instanceof \Traversable) { $languages = iterator_to_array($languages); } elseif (true === is_string($languages)) { $languages = GeneralUtility::trimExplode(',', $languages, true); } else { $languages = (array) $languages; } $pageUid = (int) $this->arguments['pageUid']; if (0 === $pageUid) { $pageUid = $GLOBALS['TSFE']->id; } $normalWhenNoLanguage = $this->arguments['normalWhenNoLanguage']; $addQueryString = $this->arguments['addQueryString']; /** @var UriBuilder $uriBuilder */ $uriBuilder = $this->controllerContext->getUriBuilder(); $uriBuilder = $uriBuilder->reset()->setTargetPageUid($pageUid)->setCreateAbsoluteUri(true)->setAddQueryString($addQueryString); $this->tagBuilder->reset(); $this->tagBuilder->setTagName('link'); $this->tagBuilder->addAttribute('rel', 'alternate'); /** @var PageRenderer $pageRenderer */ $pageRenderer = GeneralUtility::makeInstance(PageRenderer::class); $usePageRenderer = 1 !== (int) $GLOBALS['TSFE']->config['config']['disableAllHeaderCode']; $output = ''; foreach ($languages as $languageUid => $languageName) { if (false === $this->pageService->hidePageForLanguageUid($pageUid, $languageUid, $normalWhenNoLanguage)) { $uri = $uriBuilder->setArguments(array('L' => $languageUid))->build(); $this->tagBuilder->addAttribute('href', $uri); $this->tagBuilder->addAttribute('hreflang', $languageName); $renderedTag = $this->tagBuilder->render(); if (true === $usePageRenderer) { $pageRenderer->addMetaTag($renderedTag); } else { $output .= $renderedTag . LF; } } } if (false === $usePageRenderer) { return trim($output); } return null; }
/** * @test */ public function renderProvidesCorrectVideoTagOutput() { $this->tagBuilder->expects($this->once())->method('render')->will($this->returnValue('<video width="100" height="100">')); $settings = []; $video = $this->getMock(Video::class, ['getHeight', 'getWidth']); $video->expects($this->any())->method('getHeight')->will($this->returnValue(100)); $video->expects($this->any())->method('getWidth')->will($this->returnValue(100)); $this->viewHelper->initialize(); self::assertSame('<video width="100" height="100">', $this->viewHelper->render($settings, $video)); }
/** * Render method * @return string * @throws Exception */ public function render() { $src = $this->arguments['src']; $this->viewHelperVariableContainer->addOrUpdate(self::SCOPE, self::SCOPE_VARIABLE_SRC, $src); $content = $this->renderChildren(); $this->viewHelperVariableContainer->remove(self::SCOPE, self::SCOPE_VARIABLE_SRC); if (FALSE === $this->viewHelperVariableContainer->exists(self::SCOPE, self::SCOPE_VARIABLE_DEFAULT_SOURCE)) { throw new Exception('Please add a source without a media query as a default.', 1438116616); } $defaultSource = $this->viewHelperVariableContainer->get(self::SCOPE, self::SCOPE_VARIABLE_DEFAULT_SOURCE); $defaultImage = new TagBuilder('img'); $defaultImage->addAttribute('src', $defaultSource); $defaultImage->addAttribute('alt', $this->arguments['alt']); if (FALSE === empty($this->arguments['title'])) { $defaultImage->addAttribute('title', $this->arguments['alt']); } $content .= $defaultImage->render(); $this->tag->setContent($content); return $this->tag->render(); }
/** * @return string */ protected function getTagWithContent() { return $this->tagBuilder->render(); }
/** * @param FileInterface $file * @param int|string $width TYPO3 known format; examples: 220, 200m or 200c * @param int|string $height TYPO3 known format; examples: 220, 200m or 200c * @param array $options * @param bool $usedPathsRelativeToCurrentScript See $file->getPublicUrl() * @return string */ public function render(FileInterface $file, $width, $height, array $options = array(), $usedPathsRelativeToCurrentScript = false) { $data = $srcset = $sizes = []; if ($file instanceof FileReference) { $originalFile = $file->getOriginalFile(); } else { $originalFile = $file; } try { $defaultProcessConfiguration = []; $defaultProcessConfiguration['width'] = (int) $width; $defaultProcessConfiguration['crop'] = $file->getProperty('crop'); } catch (\InvalidArgumentException $e) { $defaultProcessConfiguration['crop'] = ''; } foreach ($this->settings['sourceCollection'] as $configuration) { try { if (!is_array($configuration)) { throw new \RuntimeException(); } if (isset($configuration['sizes'])) { $sizes[] = trim($configuration['sizes'], ' ,'); } if ((int) $configuration['width'] > (int) $width) { throw new \RuntimeException(); } $localProcessingConfiguration = $defaultProcessConfiguration; $localProcessingConfiguration['width'] = $configuration['width']; $processedFile = $originalFile->process(ProcessedFile::CONTEXT_IMAGECROPSCALEMASK, $localProcessingConfiguration); $url = $this->absRefPrefix . $processedFile->getPublicUrl(); $data['data-' . $configuration['dataKey']] = $url; $srcset[] = $url . rtrim(' ' . $configuration['srcset'] ?: ''); } catch (\Exception $ignoredException) { continue; } } $src = $originalFile->process(ProcessedFile::CONTEXT_IMAGECROPSCALEMASK, $defaultProcessConfiguration)->getPublicUrl(); $this->tagBuilder->reset(); $this->tagBuilder->setTagName('img'); $this->tagBuilder->addAttribute('src', $src); $this->tagBuilder->addAttribute('alt', $file->getProperty('alternative')); $this->tagBuilder->addAttribute('title', $file->getProperty('title')); switch ($this->settings['layoutKey']) { case 'srcset': if (!empty($srcset)) { $this->tagBuilder->addAttribute('srcset', implode(', ', $srcset)); } $this->tagBuilder->addAttribute('sizes', implode(', ', $sizes)); break; case 'data': if (!empty($data)) { foreach ($data as $key => $value) { $this->tagBuilder->addAttribute($key, $value); } } break; default: $this->tagBuilder->addAttributes(['width' => (int) $width, 'height' => (int) $height]); break; } return $this->tagBuilder->render(); }