Initial Commit
This commit is contained in:
58
packages/vitec/Classes/View/ProductListJsonView.php
Normal file
58
packages/vitec/Classes/View/ProductListJsonView.php
Normal file
@@ -0,0 +1,58 @@
|
||||
<?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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user