vendor/contao/core-bundle/src/EventListener/ContaoJsonLdSchemaListener.php line 26

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4.  * This file is part of Contao.
  5.  *
  6.  * (c) Leo Feyer
  7.  *
  8.  * @license LGPL-3.0-or-later
  9.  */
  10. namespace Contao\CoreBundle\EventListener;
  11. use Contao\CoreBundle\Event\JsonLdEvent;
  12. use Contao\CoreBundle\Routing\ResponseContext\HtmlHeadBag\HtmlHeadBag;
  13. use Contao\CoreBundle\Routing\ResponseContext\JsonLd\ContaoPageSchema;
  14. use Contao\CoreBundle\Routing\ResponseContext\JsonLd\JsonLdManager;
  15. /**
  16.  * Updates the schema.contao.org schema before rendering it
  17.  * with the current data from the HtmlHeadBag.
  18.  */
  19. class ContaoJsonLdSchemaListener
  20. {
  21.     public function __invoke(JsonLdEvent $event): void
  22.     {
  23.         $responseContext $event->getResponseContext();
  24.         if (!$responseContext->has(HtmlHeadBag::class) || !$responseContext->has(JsonLdManager::class)) {
  25.             return;
  26.         }
  27.         /** @var JsonLdManager $jsonLdManager */
  28.         $jsonLdManager $responseContext->get(JsonLdManager::class);
  29.         if (!$jsonLdManager->getGraphForSchema(JsonLdManager::SCHEMA_CONTAO)->has(ContaoPageSchema::class)) {
  30.             return;
  31.         }
  32.         /** @var HtmlHeadBag $htmlHeadBag */
  33.         $htmlHeadBag $responseContext->get(HtmlHeadBag::class);
  34.         /** @var ContaoPageSchema $schema */
  35.         $schema $jsonLdManager->getGraphForSchema(JsonLdManager::SCHEMA_CONTAO)->get(ContaoPageSchema::class);
  36.         $schema->updateFromHtmlHeadBag($htmlHeadBag);
  37.     }
  38. }