productRepository = $productRepository; } /** * Inject the TitleProvider * * @param \Evomedien\Vitec\PageTitle\ProductPageTitleProvider $titleProvider */ public function injectTitleProvider(ProductPageTitleProvider $titleProvider): void { $this->titleProvider = $titleProvider; } /** * @var LinkService */ protected $linkService; /** * Constructor * * @param ProductRepository $productRepository * @param ProductPageTitleProvider $titleProvider * @param LinkService $linkService */ public function __construct( ProductRepository $productRepository, ProductPageTitleProvider $titleProvider, ) { $this->productRepository = $productRepository; $this->titleProvider = $titleProvider; } /** * action index * * @return \Psr\Http\Message\ResponseInterface */ public function indexAction(): \Psr\Http\Message\ResponseInterface { return $this->htmlResponse(); } /** * action list * * @return \Psr\Http\Message\ResponseInterface */ public function listAction(): \Psr\Http\Message\ResponseInterface { // Extract and sanitize category UIDs from FlexForm $categoryUids = array_filter( array_map('intval', explode(',', (string)($this->settings['categories'] ?? ''))) ); // Get filtered products based on categories and visibility flags $products = $this->productRepository->findFilteredProducts($categoryUids); $this->view->assign('products', $products); // Check if we're in headless mode (JSON request) $request = $this->request; $pageType = $request->getAttribute('routing')?->getPageType() ?? 0; if ($pageType === 834) { // Headless JSON page type return $this->jsonResponse(json_encode([ 'products' => $this->serializeProducts($products) ])); } return $this->htmlResponse(); } /** * Serialize products to array for JSON output */ protected function serializeProducts($products): array { $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 $productsData; } /** * action show * * @param \Evomedien\Vitec\Domain\Model\Product $product * @return \Psr\Http\Message\ResponseInterface */ public function showAction(\Evomedien\Vitec\Domain\Model\Product $product): \Psr\Http\Message\ResponseInterface { $metaTagManager = GeneralUtility::makeInstance(MetaTagManagerRegistry::class)->getManagerForProperty('og:title'); $metaTagManager->addProperty('og:title', ($product->getSeotitle())); $metaTagManager->addProperty('og:description', ($product->getDescription())); //$metaTagManager->addProperty('og:url', $this->request->getRequestUri()); $metaTagManager->addProperty('og:type', 'product'); if ($product->getOgimage()) { $ogImageUrl = $product->getOgimage()->getOriginalResource()->getPublicUrl(); $metaTagManager->addProperty('og:image', $ogImageUrl); } // Access the layout setting from FlexForm $layout = $this->settings['layout'] ?? 0; // Pass the layout to the view $this->view->assign('layout', $layout); $this->view->assign('product', $product); $this->titleProvider->setSeoTitle($product->getSeotitle()); return $this->htmlResponse(); } }