Skip to content

Commit

Permalink
Added a **Include Homepage in Breadcrumbs** general plugin setting to…
Browse files Browse the repository at this point in the history
… control whether the generated JSON-LD for Breadcrumbs includes the homepage or not ([#957](#957)) & ([#747](#747))

Signed-off-by: Andrew Welch <andrew@nystudio107.com>
  • Loading branch information
khalwat committed Aug 25, 2021
1 parent 2dc89f8 commit d3d0311
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 30 deletions.
3 changes: 3 additions & 0 deletions src/config.php
Expand Up @@ -47,6 +47,9 @@
// Should sitemaps be submitted to search engines automatically whenever there are changes?
'submitSitemaps' => true,

// Should the homepage be included in the generated Breadcrumbs JSON-LD?
'includeHomepageInBreadcrumbs' => true,

// Should SEOmatic add to the http response headers?
'headersEnabled' => true,

Expand Down
63 changes: 34 additions & 29 deletions src/helpers/DynamicMeta.php
Expand Up @@ -435,34 +435,38 @@ public static function addMetaJsonLdBreadCrumbs(int $siteId = null)
'name' => 'Breadcrumbs',
'description' => 'Breadcrumbs list',
]);
/** @var Element $element */
$element = Craft::$app->getElements()->getElementByUri('__home__', $siteId);
if ($element) {
$uri = $element->uri === '__home__' ? '' : ($element->uri ?? '');
try {
$id = UrlHelper::siteUrl($uri, null, null, $siteId);
} catch (Exception $e) {
$id = $siteUrl;
Craft::error($e->getMessage(), __METHOD__);
// Include the Homepage in the breadcrumbs, if includeHomepageInBreadcrumbs is true
$element = null;
if (Seomatic::$settings->includeHomepageInBreadcrumbs) {
/** @var Element $element */
$element = Craft::$app->getElements()->getElementByUri('__home__', $siteId);
if ($element) {
$uri = $element->uri === '__home__' ? '' : ($element->uri ?? '');
try {
$id = UrlHelper::siteUrl($uri, null, null, $siteId);
} catch (Exception $e) {
$id = $siteUrl;
Craft::error($e->getMessage(), __METHOD__);
}
$item = UrlHelper::stripQueryString($id);
$item = UrlHelper::absoluteUrlWithProtocol($item);
$listItem = MetaJsonLd::create('ListItem', [
'position' => $position,
'name' => $element->title,
'item' => $item,
'@id' => $id,
]);
$crumbs->itemListElement[] = $listItem;
} else {
$item = UrlHelper::stripQueryString($siteUrl ?? '/');
$item = UrlHelper::absoluteUrlWithProtocol($item);
$crumbs->itemListElement[] = MetaJsonLd::create('ListItem', [
'position' => $position,
'name' => 'Homepage',
'item' => $item,
'@id' => $siteUrl,
]);
}
$item = UrlHelper::stripQueryString($id);
$item = UrlHelper::absoluteUrlWithProtocol($item);
$listItem = MetaJsonLd::create('ListItem', [
'position' => $position,
'name' => $element->title,
'item' => $item,
'@id' => $id,
]);
$crumbs->itemListElement[] = $listItem;
} else {
$item = UrlHelper::stripQueryString($siteUrl ?? '/');
$item = UrlHelper::absoluteUrlWithProtocol($item);
$crumbs->itemListElement[] = MetaJsonLd::create('ListItem', [
'position' => $position,
'name' => 'Homepage',
'item' => $item,
'@id' => $siteUrl,
]);
}
// Build up the segments, and look for elements that match
$uri = '';
Expand Down Expand Up @@ -502,8 +506,9 @@ public static function addMetaJsonLdBreadCrumbs(int $siteId = null)
}
$uri .= '/';
}

Seomatic::$plugin->jsonLd->add($crumbs);
if (!empty($crumbs->itemListElement)) {
Seomatic::$plugin->jsonLd->add($crumbs);
}
}

/**
Expand Down
6 changes: 6 additions & 0 deletions src/models/Settings.php
Expand Up @@ -55,6 +55,11 @@ class Settings extends VarsModel
*/
public $submitSitemaps = true;

/**
* @var bool Should the homepage be included in the generated Breadcrumbs JSON-LD?
*/
public $includeHomepageInBreadcrumbs = true;

/**
* @var bool Should SEOmatic add to the http response headers?
*/
Expand Down Expand Up @@ -234,6 +239,7 @@ public function rules(): array
'sitemapsEnabled',
'regenerateSitemapsAutomatically',
'submitSitemaps',
'includeHomepageInBreadcrumbs',
'headersEnabled',
'generatorEnabled',
'addHrefLang',
Expand Down
10 changes: 10 additions & 0 deletions src/templates/settings/plugin/_includes/general.twig
Expand Up @@ -54,6 +54,16 @@
errors: settings.getErrors("submitSitemaps"),
}) }}

{{ forms.lightswitchField({
label: "Include Homepage in Breadcrumbs"|t("seomatic"),
instructions: "Should the homepage be included in the generated Breadcrumbs JSON-LD?"|t("seomatic"),
id: "includeHomepageInBreadcrumbs",
name: "includeHomepageInBreadcrumbs",
on: settings.includeHomepageInBreadcrumbs,
warning: configWarning("includeHomepageInBreadcrumbs", "seomatic"),
errors: settings.getErrors("includeHomepageInBreadcrumbs"),
}) }}

{{ forms.lightswitchField({
label: "Manually Set SEOmatic Environment"|t("seomatic"),
instructions: "If off, SEOmatic will automatically attempt to determine the current environment. Turn this on to manually set the environment."|t("seomatic"),
Expand Down
9 changes: 8 additions & 1 deletion src/translations/en/seomatic.php
Expand Up @@ -486,5 +486,12 @@
'The [SEO Settings]({url}) fields now feature **Override** light switches next to each setting, letting you explicitly override SEO settings on a per-entry basis.' => 'The [SEO Settings]({url}) fields now feature **Override** light switches next to each setting, letting you explicitly override SEO settings on a per-entry basis.',
'The search engine friendly URL that you want the search engines to treat as authoritative.' => 'The search engine friendly URL that you want the search engines to treat as authoritative.',
'The [SEO Settings]({url}) fields now feature **Override** lightswitches next to each setting, letting you explicitly override SEO settings on a per-entry basis.' => 'The [SEO Settings]({url}) fields now feature **Override** lightswitches next to each setting, letting you explicitly override SEO settings on a per-entry basis.',
'Override' => 'Override'
'Override' => 'Override',
'SEOmatic - SEO Settings fields' => 'SEOmatic - SEO Settings fields',
'The [SEO Settings]({url}) fields in SEOmatic 3.4 now feature **Override** light switches next to each setting, to make it easier to use effectively.' => 'The [SEO Settings]({url}) fields in SEOmatic 3.4 now feature **Override** light switches next to each setting, to make it easier to use effectively.',
'SEOmatic 3.4 - SEO Settings fields' => 'SEOmatic 3.4 - SEO Settings fields',
'The [SEO Settings]({url}) fields in SEOmatic now feature **Override** light switches next to each setting, to improve the content authoring experience.' => 'The [SEO Settings]({url}) fields in SEOmatic now feature **Override** light switches next to each setting, to improve the content authoring experience.',
'Body text with [link]({url}).' => 'Body text with [link]({url}).',
'Include Homepage in Breadcrumbs' => 'Include Homepage in Breadcrumbs',
'Should the homepage be included in the generated Breadcrumbs JSON-LD?' => 'Should the homepage be included in the generated Breadcrumbs JSON-LD?'
];

0 comments on commit d3d0311

Please sign in to comment.