Zwischenstand vom 29.05.2026

This commit is contained in:
khaccount
2026-05-29 11:14:02 +02:00
parent b6d2142214
commit dba154e572
36 changed files with 5113 additions and 649 deletions

View File

@@ -16,23 +16,19 @@ use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Service\ImageService;
/**
* UserFunc to render product list as JSON for headless
* UserFunc to render product list as JSON for headless.
*
* `render()` performs page discovery (used for top-level list plugins via
* TypoScript). `renderForRecord()` processes one specific tt_content row and
* is used both internally and by ContainerChildrenProcessor to resolve a
* product-list plugin nested inside a b13 container.
*/
class ProductListJsonRenderer
{
public function render(string $content, array $conf): string
{
// IMPORTANT: Headless creates new cObj contexts when rendering JSON fields,
// losing the current content element context. Also, Extbase repositories don't work
// in UserFunc context because the full Extbase framework isn't bootstrapped.
//
// Solution: Query tt_content directly to find the plugin configuration,
// then use direct database queries for products instead of Extbase repositories.
// Get current page UID
$pageId = (int)($GLOBALS['TSFE']->id ?? 0);
// Query tt_content for vitec_productlist on this page
$queryBuilder = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Database\ConnectionPool::class)
->getQueryBuilderForTable('tt_content');
@@ -49,95 +45,93 @@ class ProductListJsonRenderer
->fetchAllAssociative();
if (empty($contentElements)) {
return json_encode(['debug' => 'No vitec_productlist on page ' . $pageId]);
// Not a product-list page: emit nothing so headless removes the key.
return '';
}
// Take the first one (there should typically be only one)
$contentElement = $contentElements[0];
return $this->renderForRecord($contentElements[0]);
}
// Parse FlexForm
$flexFormService = GeneralUtility::makeInstance(FlexFormService::class);
$flexFormData = $flexFormService->convertFlexFormContentToArray($contentElement['pi_flexform'] ?? '');
$settings = $flexFormData['settings'] ?? [];
/**
* Render exactly the given tt_content row (the product-list plugin element).
* Exception-safe: returns '' on any failure.
*
* @param array<string,mixed> $contentElement tt_content row of the plugin
*/
public function renderForRecord(array $contentElement): string
{
try {
// Parse FlexForm of THIS element
$flexFormService = GeneralUtility::makeInstance(FlexFormService::class);
$flexFormData = $flexFormService->convertFlexFormContentToArray($contentElement['pi_flexform'] ?? '');
$settings = $flexFormData['settings'] ?? [];
// Extract category UIDs and debug setting
$categoryUids = array_filter(
array_map('intval', explode(',', (string)($settings['categories'] ?? '')))
);
$debugMode = (bool)($settings['debug'] ?? false);
$allProducts = (bool)($settings['allproducts'] ?? false);
// Extbase repositories don't work in UserFunc context
// Use direct database query instead
$productQueryBuilder = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Database\ConnectionPool::class)
->getQueryBuilderForTable('tx_vitec_domain_model_product');
$productQuery = $productQueryBuilder
->select('p.*')
->from('tx_vitec_domain_model_product', 'p')
->where(
$productQueryBuilder->expr()->eq('p.deleted', 0),
$productQueryBuilder->expr()->eq('p.hidden', 0),
$productQueryBuilder->expr()->eq('p.legacy', 0),
$productQueryBuilder->expr()->eq('p.supportproduct', 0),
$productQueryBuilder->expr()->eq('p.hideonwebsite', 0),
$productQueryBuilder->expr()->eq('p.hideonproducts', 0),
$productQueryBuilder->expr()->eq('p.subproduct', 0)
$categoryUids = array_filter(
array_map('intval', explode(',', (string)($settings['categories'] ?? '')))
);
$debugMode = (bool)($settings['debug'] ?? false);
$allProducts = (bool)($settings['allproducts'] ?? false);
// Add category filter if specified and allProducts is not enabled
if (!empty($categoryUids) && !$allProducts) {
// Join with sys_category_record_mm to filter by categories
$productQuery
->join(
'p',
'sys_category_record_mm',
'mm',
'mm.uid_foreign = p.uid AND mm.tablenames = ' . $productQueryBuilder->createNamedParameter('tx_vitec_domain_model_product', ParameterType::STRING) . ' AND mm.fieldname = ' . $productQueryBuilder->createNamedParameter('categories', ParameterType::STRING)
)
->andWhere(
$productQueryBuilder->expr()->in('mm.uid_local', $productQueryBuilder->createNamedParameter($categoryUids, Connection::PARAM_INT_ARRAY))
)
->groupBy('p.uid');
// Extbase repositories don't work in UserFunc context — direct query.
$productQueryBuilder = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Database\ConnectionPool::class)
->getQueryBuilderForTable('tx_vitec_domain_model_product');
$productQuery = $productQueryBuilder
->select('p.*')
->from('tx_vitec_domain_model_product', 'p')
->where(
$productQueryBuilder->expr()->eq('p.deleted', 0),
$productQueryBuilder->expr()->eq('p.hidden', 0),
$productQueryBuilder->expr()->eq('p.legacy', 0),
$productQueryBuilder->expr()->eq('p.supportproduct', 0),
$productQueryBuilder->expr()->eq('p.hideonwebsite', 0),
$productQueryBuilder->expr()->eq('p.hideonproducts', 0),
$productQueryBuilder->expr()->eq('p.subproduct', 0)
);
if (!empty($categoryUids) && !$allProducts) {
$productQuery
->join(
'p',
'sys_category_record_mm',
'mm',
'mm.uid_foreign = p.uid AND mm.tablenames = ' . $productQueryBuilder->createNamedParameter('tx_vitec_domain_model_product', ParameterType::STRING) . ' AND mm.fieldname = ' . $productQueryBuilder->createNamedParameter('categories', ParameterType::STRING)
)
->andWhere(
$productQueryBuilder->expr()->in('mm.uid_local', $productQueryBuilder->createNamedParameter($categoryUids, Connection::PARAM_INT_ARRAY))
)
->groupBy('p.uid');
}
$products = $productQuery->executeQuery()->fetchAllAssociative();
$productsData = [];
foreach ($products as $product) {
$productsData[] = $this->serializeProduct($product);
}
if ($debugMode) {
return json_encode([
'products' => $productsData,
'debug' => [
'pageId' => (int)($GLOBALS['TSFE']->id ?? 0),
'categoryUids' => $categoryUids,
'productCount' => count($productsData),
'settings' => $settings,
],
]);
}
return json_encode($productsData);
} catch (\Throwable $e) {
return '';
}
$products = $productQuery->executeQuery()->fetchAllAssociative();
// Serialize products (they're already associative arrays from the query)
$productsData = [];
foreach ($products as $product) {
$productsData[] = $this->serializeProduct($product);
}
// If debug mode is enabled, return object with products and debug info
if ($debugMode) {
return json_encode([
'products' => $productsData,
'debug' => [
'pageId' => $pageId,
'categoryUids' => $categoryUids,
'productCount' => count($productsData),
'settings' => $settings
]
]);
}
// Otherwise return products array directly (backward compatible)
return json_encode($productsData);
}
/**
* Serialize a single product DB row to the full headless JSON structure.
*
* Includes every field declared on the Product domain model
* (Evomedien\Vitec\Domain\Model\Product) plus all fully resolved
* relations (categories, productimage, downloads, ogimage, relatedprodukt).
*
* DB-only columns that are NOT part of the domain model
* (cta, links, sorting1-5, key1-3, apptext1-3, productlayout, image,
* relatedimage, system fields) are intentionally omitted.
*
* @param array<string,mixed> $product Associative DB row of tx_vitec_domain_model_product
* @param array<string,mixed> $product
* @return array<string,mixed>
*/
protected function serializeProduct(array $product): array
@@ -145,10 +139,8 @@ class ProductListJsonRenderer
$uid = (int)$product['uid'];
return [
// --- identifier ---
'uid' => $uid,
// --- scalar string fields (Product domain model) ---
'title' => (string)($product['title'] ?? ''),
'slug' => (string)($product['slug'] ?? ''),
'urltitle' => (string)($product['urltitle'] ?? ''),
@@ -166,7 +158,6 @@ class ProductListJsonRenderer
'contentelement' => (string)($product['contentelement'] ?? ''),
'contentelementcta' => (string)($product['contentelementcta'] ?? ''),
// --- boolean flags (Product domain model) ---
'hideonapp' => (bool)($product['hideonapp'] ?? false),
'hideonwebsite' => (bool)($product['hideonwebsite'] ?? false),
'hideondatasheets' => (bool)($product['hideondatasheets'] ?? false),
@@ -176,10 +167,8 @@ class ProductListJsonRenderer
'supportproduct' => (bool)($product['supportproduct'] ?? false),
'subproduct' => (bool)($product['subproduct'] ?? false),
// --- convenience link (kept for backward compatibility) ---
'link' => '/product/' . (string)($product['slug'] ?? ''),
// --- fully resolved relations ---
'categories' => $this->getProductCategories($uid),
'images' => $this->getProductImages($uid),
'downloads' => $this->getProductDownloads($uid),
@@ -188,10 +177,6 @@ class ProductListJsonRenderer
];
}
/**
* Get product images from FAL (sys_file_reference)
* Processes images through ImageService and generates srcset for responsive images
*/
protected function getProductImages(int $productUid): array
{
$queryBuilder = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Database\ConnectionPool::class)
@@ -217,10 +202,8 @@ class ProductListJsonRenderer
$images = [];
foreach ($fileReferences as $fileRefData) {
try {
// Get FAL FileReference object
$fileReference = $resourceFactory->getFileReferenceObject((int)$fileRefData['uid']);
// Define image sizes for srcset
$sizes = [
'small' => ['width' => 400, 'height' => null],
'medium' => ['width' => 800, 'height' => null],
@@ -247,7 +230,6 @@ class ProductListJsonRenderer
];
}
// Get original/default image
$defaultProcessed = $imageService->applyProcessingInstructions(
$fileReference,
['width' => 800, 'crop' => $fileRefData['crop'] ?? null]
@@ -267,7 +249,6 @@ class ProductListJsonRenderer
]
];
} catch (\Exception $e) {
// Skip images that can't be processed
continue;
}
}
@@ -276,8 +257,6 @@ class ProductListJsonRenderer
}
/**
* Get the single Open Graph image (ogimage) for a product, or null.
*
* @return array<string,mixed>|null
*/
protected function getProductOgImage(int $productUid): ?array
@@ -331,9 +310,6 @@ class ProductListJsonRenderer
}
}
/**
* Get categories for a product (resolved sys_category records).
*/
protected function getProductCategories(int $productUid): array
{
$queryBuilder = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Database\ConnectionPool::class)
@@ -369,9 +345,6 @@ class ProductListJsonRenderer
}, $categories);
}
/**
* Get all downloads for a product (resolved via tx_vitec_product_download_mm).
*/
protected function getProductDownloads(int $productUid): array
{
$queryBuilder = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Database\ConnectionPool::class)
@@ -400,7 +373,6 @@ class ProductListJsonRenderer
foreach ($downloads as $download) {
$fileInfo = null;
// Get file information from FAL if file reference exists
if (!empty($download['file'])) {
$fileQueryBuilder = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Database\ConnectionPool::class)
->getQueryBuilderForTable('sys_file_reference');
@@ -448,12 +420,6 @@ class ProductListJsonRenderer
return $result;
}
/**
* Get related products (resolved via tx_vitec_product_related_mm).
*
* Returns a shallow representation (no nested relations) to avoid
* infinite recursion between mutually related products.
*/
protected function getRelatedProducts(int $productUid): array
{
$queryBuilder = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Database\ConnectionPool::class)
@@ -480,8 +446,6 @@ class ProductListJsonRenderer
$result = [];
foreach ($related as $rel) {
$relUid = (int)$rel['uid'];
$images = $this->getProductImages($relUid);
$result[] = [
'uid' => $relUid,
'title' => (string)($rel['title'] ?? ''),
@@ -490,7 +454,7 @@ class ProductListJsonRenderer
'teaser' => (string)($rel['teaser'] ?? ''),
'description' => (string)($rel['description'] ?? ''),
'link' => '/product/' . (string)($rel['slug'] ?? ''),
'images' => $images,
'images' => $this->getProductImages($relUid),
];
}