Skip to content

Commit 1e9e0cd

Browse files
[5.3] Fix schema.org with invalid breadcrumb json-ld (#45720)
Co-authored-by: Harald Leithner <[email protected]>
1 parent 23e460f commit 1e9e0cd

File tree

2 files changed

+30
-8
lines changed

2 files changed

+30
-8
lines changed

modules/mod_breadcrumbs/tmpl/default.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@
121121
$wa->addInline(
122122
'script',
123123
json_encode($data, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | $prettyPrint),
124-
['name' => 'inline.mod_breadcrumbs-schemaorg'],
124+
['name' => 'inline.breadcrumbs-schemaorg'],
125125
['type' => 'application/ld+json']
126126
);
127127
}

plugins/system/schemaorg/src/Extension/Schemaorg.php

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
namespace Joomla\Plugin\System\Schemaorg\Extension;
1212

13+
use Joomla\CMS\Event\Application\BeforeCompileHeadEvent as BeforeCompileHeadApplicationEvent;
1314
use Joomla\CMS\Event\Model;
1415
use Joomla\CMS\Event\Plugin\System\Schemaorg\BeforeCompileHeadEvent;
1516
use Joomla\CMS\Event\Plugin\System\Schemaorg\PrepareDataEvent;
@@ -26,6 +27,7 @@
2627
use Joomla\CMS\Schemaorg\SchemaorgServiceInterface;
2728
use Joomla\CMS\Uri\Uri;
2829
use Joomla\CMS\User\UserFactoryAwareTrait;
30+
use Joomla\CMS\WebAsset\Exception\UnknownAssetException;
2931
use Joomla\Database\DatabaseAwareTrait;
3032
use Joomla\Database\ParameterType;
3133
use Joomla\Event\DispatcherAwareInterface;
@@ -268,9 +270,11 @@ public function onContentAfterSave(Model\AfterSaveEvent $event)
268270
*
269271
* @since 5.0.0
270272
*/
271-
public function onBeforeCompileHead(): void
273+
public function onBeforeCompileHead(BeforeCompileHeadApplicationEvent $event): void
272274
{
273-
$app = $this->getApplication();
275+
$app = $event->getApplication();
276+
$doc = $event->getDocument();
277+
$wa = $doc->getWebAssetManager();
274278
$baseType = $this->params->get('baseType', 'organization');
275279

276280
$itemId = (int) $app->getInput()->getInt('id');
@@ -383,11 +387,30 @@ public function onBeforeCompileHead(): void
383387
$webPageSchema['about'] = ['@id' => $baseId];
384388
$webPageSchema['inLanguage'] = $app->getLanguage()->getTag();
385389

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+
}
388410

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
391414
}
392415

393416
$baseSchema['@graph'][] = $webPageSchema;
@@ -440,7 +463,6 @@ public function onBeforeCompileHead(): void
440463
$schemaString = $schema->toString('JSON', ['bitmask' => JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | $prettyPrint]);
441464

442465
if ($schemaString !== '{}') {
443-
$wa = $this->getApplication()->getDocument()->getWebAssetManager();
444466
$wa->addInlineScript($schemaString, ['name' => 'inline.schemaorg'], ['type' => 'application/ld+json']);
445467
}
446468
}

0 commit comments

Comments
 (0)