Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 26 additions & 4 deletions src/Eccube/EventListener/TwigInitializeListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Doctrine\ORM\NoResultException;
use Eccube\Common\EccubeConfig;
use Eccube\Entity\AuthorityRole;
use Eccube\Entity\BaseInfo;
use Eccube\Entity\Layout;
use Eccube\Entity\Master\DeviceType;
use Eccube\Entity\Member;
Expand All @@ -31,6 +32,7 @@
use Eccube\Repository\PageLayoutRepository;
use Eccube\Repository\PageRepository;
use Eccube\Request\Context;
use Eccube\Service\SiteStructuredDataService;
use Eccube\Service\SystemService;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\ParameterBag;
Expand All @@ -41,6 +43,13 @@

class TwigInitializeListener implements EventSubscriberInterface
{
/**
* サイト共通の構造化データ(WebSite / Organization)を出力するルート名.
*
* トップページと「当サイトについて」の 2 ページに限定する.
*/
private const SITE_STRUCTURED_DATA_ROUTES = ['homepage', 'help_about'];

/**
* @var bool 初期化済かどうか.
*/
Expand All @@ -49,7 +58,7 @@ class TwigInitializeListener implements EventSubscriberInterface
/**
* TwigInitializeListener constructor.
*/
public function __construct(protected Environment $twig, protected BaseInfoRepository $baseInfoRepository, protected PageRepository $pageRepository, protected PageLayoutRepository $pageLayoutRepository, protected BlockPositionRepository $blockPositionRepository, protected DeviceTypeRepository $deviceTypeRepository, private readonly AuthorityRoleRepository $authorityRoleRepository, private EccubeConfig $eccubeConfig, protected Context $requestContext, private readonly MobileDetect $mobileDetector, private readonly UrlGeneratorInterface $router, private readonly LayoutRepository $layoutRepository, protected SystemService $systemService)
public function __construct(protected Environment $twig, protected BaseInfoRepository $baseInfoRepository, protected PageRepository $pageRepository, protected PageLayoutRepository $pageLayoutRepository, protected BlockPositionRepository $blockPositionRepository, protected DeviceTypeRepository $deviceTypeRepository, private readonly AuthorityRoleRepository $authorityRoleRepository, private EccubeConfig $eccubeConfig, protected Context $requestContext, private readonly MobileDetect $mobileDetector, private readonly UrlGeneratorInterface $router, private readonly LayoutRepository $layoutRepository, protected SystemService $systemService, private readonly SiteStructuredDataService $siteStructuredDataService)
{
}

Expand All @@ -63,12 +72,13 @@ public function onKernelRequest(RequestEvent $event): void
return;
}

$this->twig->addGlobal('BaseInfo', $this->baseInfoRepository->get());
$BaseInfo = $this->baseInfoRepository->get();
$this->twig->addGlobal('BaseInfo', $BaseInfo);

if ($this->requestContext->isAdmin()) {
$this->setAdminGlobals($event);
} else {
$this->setFrontVariables($event);
$this->setFrontVariables($event, $BaseInfo);
}

$this->initialized = true;
Expand Down Expand Up @@ -107,8 +117,11 @@ public function setIsAdminLoggedInOnFrontGlobal(RequestEvent $event): void
/**
* @throws NonUniqueResultException
*/
public function setFrontVariables(RequestEvent $event): void
public function setFrontVariables(RequestEvent $event, ?BaseInfo $BaseInfo = null): void
{
// 呼び出し元(onKernelRequest)が取得済みの BaseInfo を渡す。
// 引数は後方互換のため任意にしており、渡されなければここで取得する。
$BaseInfo ??= $this->baseInfoRepository->get();
$request = $event->getRequest();
/** @var ParameterBag $attributes */
$attributes = $request->attributes;
Expand Down Expand Up @@ -177,6 +190,15 @@ public function setFrontVariables(RequestEvent $event): void
$this->twig->addGlobal('title', $Page->getName());
$this->twig->addGlobal('isMaintenance', $this->systemService->isMaintenanceMode());
$this->twig->addGlobal('isDebugMode', env('APP_DEBUG'));
// サイト共通の構造化データ(WebSite / Organization)はトップページと「当サイトについて」にだけ出力する。
// Google の Organization ドキュメントが「トップページか組織を説明する単一ページを推奨。
// サイトの全ページに含める必要はない」としているため、対象外では組み立て自体を行わない。
$this->twig->addGlobal(
'site_json_ld',
in_array($route, self::SITE_STRUCTURED_DATA_ROUTES, true)
? $this->siteStructuredDataService->createWebSiteJsonLd($BaseInfo)
: []
);
}

public function setAdminGlobals(RequestEvent $event): void
Expand Down
3 changes: 3 additions & 0 deletions src/Eccube/Resource/template/default/default_frame.twig
Original file line number Diff line number Diff line change
Expand Up @@ -187,5 +187,8 @@ file that was distributed with this source code.
{{ include('snippet.twig', { snippets: plugin_snippets }) }}
{% endif %}
<script src="{{ asset('assets/js/customize.js', 'user_data') }}"></script>
{% if site_json_ld is defined and site_json_ld %}
<script type="application/ld+json">{{ site_json_ld|json_ld }}</script>
{% endif %}
Comment thread
nanasess marked this conversation as resolved.
</body>
</html>
11 changes: 3 additions & 8 deletions src/Eccube/Service/ProductStructuredDataService.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
*/
class ProductStructuredDataService
{
use StructuredDataDescriptionTrait;

/**
* 画像が無い場合のフォールバック画像ファイル名.
*/
Expand Down Expand Up @@ -124,15 +126,8 @@ private function buildDescription(Product $Product): string
if ($description === null || $description === '') {
$description = $Product->getDescriptionDetail();
}
if ($description === null || $description === '') {
return '';
}

$description = strip_tags($description);
$description = preg_replace('/\s+/u', ' ', $description) ?? $description;
$description = trim($description);

return mb_substr($description, 0, 300);
return $this->normalizeDescription($description);
}

/**
Expand Down
Loading
Loading