|
10 | 10 |
|
11 | 11 | namespace Joomla\Plugin\System\Schemaorg\Extension; |
12 | 12 |
|
| 13 | +use Joomla\CMS\Event\Application\BeforeCompileHeadEvent as BeforeCompileHeadApplicationEvent; |
13 | 14 | use Joomla\CMS\Event\Model; |
14 | 15 | use Joomla\CMS\Event\Plugin\System\Schemaorg\BeforeCompileHeadEvent; |
15 | 16 | use Joomla\CMS\Event\Plugin\System\Schemaorg\PrepareDataEvent; |
|
26 | 27 | use Joomla\CMS\Schemaorg\SchemaorgServiceInterface; |
27 | 28 | use Joomla\CMS\Uri\Uri; |
28 | 29 | use Joomla\CMS\User\UserFactoryAwareTrait; |
| 30 | +use Joomla\CMS\WebAsset\Exception\UnknownAssetException; |
29 | 31 | use Joomla\Database\DatabaseAwareTrait; |
30 | 32 | use Joomla\Database\ParameterType; |
31 | 33 | use Joomla\Event\DispatcherAwareInterface; |
@@ -268,9 +270,11 @@ public function onContentAfterSave(Model\AfterSaveEvent $event) |
268 | 270 | * |
269 | 271 | * @since 5.0.0 |
270 | 272 | */ |
271 | | - public function onBeforeCompileHead(): void |
| 273 | + public function onBeforeCompileHead(BeforeCompileHeadApplicationEvent $event): void |
272 | 274 | { |
273 | | - $app = $this->getApplication(); |
| 275 | + $app = $event->getApplication(); |
| 276 | + $doc = $event->getDocument(); |
| 277 | + $wa = $doc->getWebAssetManager(); |
274 | 278 | $baseType = $this->params->get('baseType', 'organization'); |
275 | 279 |
|
276 | 280 | $itemId = (int) $app->getInput()->getInt('id'); |
@@ -383,11 +387,30 @@ public function onBeforeCompileHead(): void |
383 | 387 | $webPageSchema['about'] = ['@id' => $baseId]; |
384 | 388 | $webPageSchema['inLanguage'] = $app->getLanguage()->getTag(); |
385 | 389 |
|
386 | | - // We support Breadcrumb linking |
387 | | - $breadcrumbs = ModuleHelper::getModule('mod_breadcrumbs'); |
| 390 | + // Support Breadcrumb Schema linking |
| 391 | + try { |
| 392 | + try { |
| 393 | + $breadcrumbsAsset = $wa->getRegistry()->get('script', 'inline.breadcrumbs-schemaorg'); |
| 394 | + } catch (UnknownAssetException $e) { |
| 395 | + // Fallback for older versions of the breadcrumbs module |
| 396 | + $breadcrumbsAsset = $wa->getRegistry()->get('script', 'inline.mod_breadcrumbs-schemaorg'); |
| 397 | + trigger_deprecation( |
| 398 | + 'joomla/schemaorg', |
| 399 | + '5.4', |
| 400 | + 'The inline.mod_breadcrumbs-schemaorg asset name is deprecated. Please use the generic inline.breadcrumbs-schemaorg asset name instead.' |
| 401 | + ); |
| 402 | + } |
| 403 | + |
| 404 | + $breadcrumbs = json_decode($breadcrumbsAsset->getOption('content'), true, 512, JSON_THROW_ON_ERROR); |
| 405 | + |
| 406 | + if ($breadcrumbs['@type'] !== 'BreadcrumbList') { |
| 407 | + trigger_error('The breadcrumbs schema is not of type BreadcrumbList', E_USER_WARNING); |
| 408 | + throw new UnknownAssetException(); |
| 409 | + } |
388 | 410 |
|
389 | | - if (!empty($breadcrumbs->id)) { |
390 | | - $webPageSchema['breadcrumb'] = ['@id' => $domain . '#/schema/BreadcrumbList/' . (int) $breadcrumbs->id]; |
| 411 | + $webPageSchema['breadcrumbs'] = ['@id' => $breadcrumbs['@id']]; |
| 412 | + } catch (UnknownAssetException $e) { |
| 413 | + // No Breadcrumbs Schema found, so we don't add it |
391 | 414 | } |
392 | 415 |
|
393 | 416 | $baseSchema['@graph'][] = $webPageSchema; |
@@ -440,7 +463,6 @@ public function onBeforeCompileHead(): void |
440 | 463 | $schemaString = $schema->toString('JSON', ['bitmask' => JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | $prettyPrint]); |
441 | 464 |
|
442 | 465 | if ($schemaString !== '{}') { |
443 | | - $wa = $this->getApplication()->getDocument()->getWebAssetManager(); |
444 | 466 | $wa->addInlineScript($schemaString, ['name' => 'inline.schemaorg'], ['type' => 'application/ld+json']); |
445 | 467 | } |
446 | 468 | } |
|
0 commit comments