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