/** * 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; }
/** * @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(); }