\s*)?\\[media\\](?:\s*<\/p>)?/';
/**
* @var string
*/
protected $imgClass = '';
/**
* @var string
*/
protected $videoClass = '';
/**
* @var string
*/
protected $audioClass = '';
/**
* Initialize all arguments. You need to override this method and call
* $this->registerArgument(...) inside this method, to register all your arguments.
*
* @api
*/
public function initializeArguments()
{
$this->registerArgument('news', 'object', 'the news post', true);
$this->registerArgument('imgClass', 'string', 'add css class to images');
$this->registerArgument('videoClass', 'string', 'wrap videos in a div with this class');
$this->registerArgument('audioClass', 'string', 'wrap audio files in a div with this class');
$this->registerArgument('fileIndex', 'int', 'index of image to start with', false, 0);
}
/**
* @param \TYPO3\CMS\Core\Resource\FileInterface $image
* @return string
*/
private function renderImage(\TYPO3\CMS\Core\Resource\FileInterface $image): string
{
if ($this->objectManager === null) {
$this->objectManager = GeneralUtility::makeInstance(ObjectManager::class);
}
$imageService = $this->objectManager->get(ImageService::class);
$cropString = '';
if ($image->hasProperty('crop')) {
$cropString = $image->getProperty('crop');
}
$cropVariantCollection = CropVariantCollection::create((string)$cropString);
$cropVariant = $this->arguments['cropVariant'] ?: 'default';
$cropArea = $cropVariantCollection->getCropArea($cropVariant);
$processingInstructions = [
'width' => null,
'height' => null,
'crop' => $cropArea->isEmpty() ? null : $cropArea->makeAbsoluteBasedOnFile($image)
];
$processedImage = $imageService->applyProcessingInstructions($image, $processingInstructions);
$imageUri = $imageService->getImageUri($processedImage);
$alt = trim($image->getProperty('alternative'));
$title = trim($image->getProperty('title'));
$description = trim($image->getProperty('description'));
$imageAttributes = [
'src' => $imageUri,
'alt' => $alt ?: ($title ?: ''),
'title' => $title
];
if (!empty($this->imgClass)) {
$imageAttributes['class'] = $this->imgClass;
}
$imageAttributes = array_reduce(
array_keys($imageAttributes),
function ($carry, $key) use ($imageAttributes) {
return $carry . ' ' . $key . '="' . htmlspecialchars($imageAttributes[$key]) . '"';
},
''
);
if (!empty($description)) {
$description = '' . $description . '