Skip to content

Commit

Permalink
[4.1] mod_breadcrumbs - convert microdata to JSON-LD (#30727)
Browse files Browse the repository at this point in the history
* ♻️ convert microdata to JSON-LD

* ♻️ move script to WebAssetManager

* Update modules/mod_breadcrumbs/tmpl/default.php

Co-authored-by: SharkyKZ <sharkykz@gmail.com>

* Update modules/mod_breadcrumbs/tmpl/default.php

Co-authored-by: SharkyKZ <sharkykz@gmail.com>

* removal non used variable

* Update modules/mod_breadcrumbs/tmpl/default.php

Co-authored-by: SharkyKZ <sharkykz@gmail.com>

* replace Uri::current() with Uri::getInstance()

Co-authored-by: SharkyKZ <sharkykz@gmail.com>
  • Loading branch information
hans2103 and SharkyKZ committed Oct 27, 2020
1 parent 12d2aa9 commit 59319ff
Showing 1 changed file with 42 additions and 15 deletions.
57 changes: 42 additions & 15 deletions modules/mod_breadcrumbs/tmpl/default.php
Expand Up @@ -9,12 +9,15 @@

defined('_JEXEC') or die;

use Joomla\CMS\HTML\HTMLHelper;
use Joomla\CMS\Language\Text;
use Joomla\CMS\Router\Route;
use Joomla\CMS\Uri\Uri;
use Joomla\CMS\WebAsset\WebAssetManager;

?>
<nav role="navigation" aria-label="<?php echo $module->title; ?>">
<ol itemscope itemtype="https://schema.org/BreadcrumbList" class="mod-breadcrumbs breadcrumb">
<ol class="mod-breadcrumbs breadcrumb">
<?php if ($params->get('showHere', 1)) : ?>
<li class="mod-breadcrumbs__here float-left">
<?php echo Text::_('MOD_BREADCRUMBS_HERE'); ?>&#160;
Expand All @@ -37,32 +40,56 @@

// Find last and penultimate items in breadcrumbs list
end($list);
$last_item_key = key($list);
$last_item_key = key($list);
prev($list);
$penult_item_key = key($list);

// Make a link if not the last item in the breadcrumbs
$show_last = $params->get('showLast', 1);

$class = null;

// Generate the trail
foreach ($list as $key => $item) :
if ($key !== $last_item_key) :
if (!empty($item->link)) :
$breadcrumbItem = '<a itemprop="item" href="' . Route::_($item->link) . '" class="pathway"><span itemprop="name">' . $item->name . '</span></a>';
$breadcrumbItem = HTMLHelper::_('link', Route::_($item->link), '<span>' . $item->name . '</span>', ['class' => 'pathway']);
else :
$breadcrumbItem = '<span itemprop="name">' . $item->name . '</span>';
$breadcrumbItem = '<span>' . $item->name . '</span>';
endif;
// Render all but last item - along with separator ?>
<li itemprop="itemListElement" itemscope itemtype="https://schema.org/ListItem" class="mod-breadcrumbs__item breadcrumb-item"><?php echo $breadcrumbItem; ?>
<meta itemprop="position" content="<?php echo $key + 1; ?>">
</li>
<?php elseif ($show_last) :
$breadcrumbItem = '<span itemprop="name">' . $item->name . '</span>';
// Render last item if required. ?>
<li aria-current="page" itemprop="itemListElement" itemscope itemtype="https://schema.org/ListItem" class="mod-breadcrumbs__item breadcrumb-item active"><?php echo $breadcrumbItem; ?>
<meta itemprop="position" content="<?php echo $key + 1; ?>">
</li>
<?php endif;

elseif ($show_last) :
// Render last item if required.
$breadcrumbItem = '<span>' . $item->name . '</span>';
$class = ' active';
endif;

echo '<li class="mod-breadcrumbs__item breadcrumb-item' . $class . '">' . $breadcrumbItem . '</li>';
endforeach; ?>
</ol>
<?php

// Structured data as JSON
$data = [
'@context' => 'https://schema.org',
'@type' => 'BreadcrumbList',
'itemListElement' => []
];

foreach ($list as $key => $item)
{
$data['itemListElement'][] = [
'@type' => 'ListItem',
'position' => $key + 1,
'item' => [
'@id' => $item->link ? Route::_($item->link, true, Route::TLS_IGNORE, true) : Route::_(Uri::getInstance()),
'name' => $item->name
]
];
}

/** @var WebAssetManager $wa */
$wa = $app->getDocument()->getWebAssetManager();
$wa->addInline('script', json_encode($data, JSON_UNESCAPED_UNICODE), [], ['type' => 'application/ld+json']);
?>
</nav>

0 comments on commit 59319ff

Please sign in to comment.