Files
VITEC-website/packages/vitec/Classes/View/ProductListJsonView.php
2026-05-18 14:46:25 +02:00

59 lines
1.8 KiB
PHP

<?php
declare(strict_types=1);
namespace Evomedien\Vitec\View;
use TYPO3\CMS\Extbase\Mvc\View\AbstractView;
/**
* JSON View for Headless API
*/
class ProductListJsonView extends AbstractView
{
public function render(): string
{
$products = $this->variables['products'] ?? [];
$productsData = [];
foreach ($products as $product) {
$categories = [];
if ($product->getCategories()) {
foreach ($product->getCategories() as $category) {
$categories[] = [
'uid' => $category->getUid(),
'title' => $category->getTitle(),
];
}
}
$image = null;
if ($product->getProductimage() && $product->getProductimage()->count() > 0) {
$imageObject = $product->getProductimage()->current();
$image = [
'uid' => $imageObject->getUid(),
'url' => $imageObject->getOriginalResource()->getPublicUrl(),
'title' => $imageObject->getTitle(),
'alternative' => $imageObject->getAlternative(),
'description' => $imageObject->getDescription(),
];
}
$productsData[] = [
'uid' => $product->getUid(),
'title' => $product->getTitle(),
'subtitle' => $product->getSubtitle(),
'teaser' => $product->getTeaser(),
'description' => $product->getDescription(),
'slug' => $product->getSlug(),
'categories' => $categories,
'image' => $image,
];
}
return json_encode([
'products' => $productsData,
], JSON_THROW_ON_ERROR);
}
}