DEV CommiDEV Committ
This commit is contained in:
1 parent
4541c58fe7
commit
c6759186cb
145 files changed
+2742
-579
No files matched your search
@@ -5,6 +5,7 @@ declare(strict_types=1);
|
||||
namespace Evomedien\Vitec\UserFunc;
|
||||
|
||||
|
||||
use Evomedien\Vitec\Service\StructuredDataService;
|
||||
use TYPO3\CMS\Core\Attribute\AsAllowedCallable;
|
||||
use TYPO3\CMS\Core\Database\ConnectionPool;
|
||||
use TYPO3\CMS\Core\Service\FlexFormService;
|
||||
@@ -12,7 +13,7 @@ use TYPO3\CMS\Core\Utility\GeneralUtility;
|
||||
use TYPO3\CMS\Core\Resource\ResourceFactory;
|
||||
use TYPO3\CMS\Core\Imaging\ImageManipulation\CropVariantCollection;
|
||||
use TYPO3\CMS\Core\Resource\FileReference;
|
||||
use TYPO3\CMS\Core\Imaging\ImageService;
|
||||
use TYPO3\CMS\Extbase\Service\ImageService;
|
||||
use Doctrine\DBAL\ParameterType;
|
||||
|
||||
/**
|
||||
@@ -176,7 +177,7 @@ class ProductShowJsonRenderer
|
||||
{
|
||||
$uid = (int)$product['uid'];
|
||||
|
||||
return [
|
||||
$data = [
|
||||
'uid' => $uid,
|
||||
|
||||
'title' => (string)($product['title'] ?? ''),
|
||||
@@ -215,6 +216,79 @@ class ProductShowJsonRenderer
|
||||
'videofile' => $this->getProductVideoFile($uid),
|
||||
'relatedprodukt' => $this->getRelatedProducts($uid),
|
||||
];
|
||||
|
||||
// schema.org JSON-LD (Product + optional VideoObject) as a ready-to-emit
|
||||
// @graph string. The frontend injects it as <script type="application/ld+json">.
|
||||
// Supersedes the manual `structureddata` field above (kept for backwards
|
||||
// compatibility until the frontend has switched over).
|
||||
$data['jsonLd'] = $this->buildJsonLd($product, $data);
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Build the product's structured-data @graph from the already-serialized
|
||||
* product array. Exception-safe: returns '' on any failure.
|
||||
*
|
||||
* @param array<string,mixed> $rawProduct raw DB row (for crdate/tstamp)
|
||||
* @param array<string,mixed> $data serialized product
|
||||
*/
|
||||
protected function buildJsonLd(array $rawProduct, array $data): string
|
||||
{
|
||||
try {
|
||||
$service = GeneralUtility::makeInstance(StructuredDataService::class);
|
||||
$base = $this->resolveCanonicalBase();
|
||||
|
||||
$imageUrls = [];
|
||||
foreach (($data['images'] ?? []) as $img) {
|
||||
if (!empty($img['url'])) {
|
||||
$imageUrls[] = $service->absUrl((string)$img['url'], $base);
|
||||
}
|
||||
}
|
||||
if ($imageUrls === [] && !empty($data['ogimage']['url'])) {
|
||||
$imageUrls[] = $service->absUrl((string)$data['ogimage']['url'], $base);
|
||||
}
|
||||
|
||||
$hasOrganization = trim((string)($this->siteSettings()?->get('seo.organization.name') ?? '')) !== '';
|
||||
|
||||
$product = $service->buildProduct($data, $base, $imageUrls, $hasOrganization);
|
||||
|
||||
$uploadTs = (int)($rawProduct['crdate'] ?? $rawProduct['tstamp'] ?? 0);
|
||||
$video = $service->buildVideoObject($data, $base, $imageUrls, $uploadTs);
|
||||
|
||||
return $service->encodeGraph([$product, $video]);
|
||||
} catch (\Throwable $e) {
|
||||
return '';
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Site settings of the current request, or null outside a site context.
|
||||
*/
|
||||
protected function siteSettings(): ?\TYPO3\CMS\Core\Site\Entity\SiteSettings
|
||||
{
|
||||
$request = $GLOBALS['TYPO3_REQUEST'] ?? null;
|
||||
return $request?->getAttribute('site')?->getSettings();
|
||||
}
|
||||
|
||||
/**
|
||||
* Canonical absolute base (e.g. "https://www.vitec.com"). Prefers the
|
||||
* explicit site setting, falls back to the request host.
|
||||
*/
|
||||
protected function resolveCanonicalBase(): string
|
||||
{
|
||||
$configured = trim((string)($this->siteSettings()?->get('seo.site.canonicalBase') ?? ''));
|
||||
if ($configured !== '') {
|
||||
return rtrim($configured, '/');
|
||||
}
|
||||
|
||||
$request = $GLOBALS['TYPO3_REQUEST'] ?? null;
|
||||
$normalizedParams = $request?->getAttribute('normalizedParams');
|
||||
if ($normalizedParams !== null) {
|
||||
return rtrim($normalizedParams->getSiteUrl(), '/');
|
||||
}
|
||||
|
||||
return '';
|
||||
}
|
||||
|
||||
protected function getProductImages(int $productUid): array
|
||||
|
||||
Reference in new issue
Block a user