Skip to content

Commit

Permalink
feat: Added a setting in Plugin Settings -> Tags to specify which sit…
Browse files Browse the repository at this point in the history
…e should be used as the `x-default` for `hreflang` tags ([1162](#1162))
  • Loading branch information
khalwat committed Jun 11, 2024
1 parent 75eba96 commit 540b966
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 11 deletions.
3 changes: 3 additions & 0 deletions src/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,9 @@
// Whether to dynamically include the `x-default` hreflang tags
'addXDefaultHrefLang' => true,

// The site to use for the `x-default` hreflang tag (0 defaults to the Primary site)
'xDefaultSite' => 0,

// Whether to dynamically include hreflang tags on paginated pages
'addPaginatedHreflang' => true,

Expand Down
2 changes: 2 additions & 0 deletions src/controllers/SettingsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -882,6 +882,8 @@ public function actionPlugin(): Response
];
$variables['selectedSubnavItem'] = 'plugin';
$variables['settings'] = Seomatic::$settings;
$sites = ArrayHelper::map(Craft::$app->getSites()->getAllSites(), 'id', 'name');
$variables['sites'] = $sites;

// Render the template
return $this->renderTemplate('seomatic/settings/plugin/_edit', $variables);
Expand Down
3 changes: 2 additions & 1 deletion src/helpers/DynamicMeta.php
Original file line number Diff line number Diff line change
Expand Up @@ -640,14 +640,15 @@ public static function getLocalizedUrls(string $uri = null, int $siteId = null):
$hreflangLanguage = $language;
$hreflangLanguage = strtolower($hreflangLanguage);
$hreflangLanguage = str_replace('_', '-', $hreflangLanguage);
$primary = Seomatic::$settings->xDefaultSite == 0 ? $site->primary : Seomatic::$settings->xDefaultSite == $site->id;
if ($includeUrl) {
$localizedUrls[] = [
'id' => $site->id,
'language' => $language,
'ogLanguage' => $ogLanguage,
'hreflangLanguage' => $hreflangLanguage,
'url' => $url,
'primary' => $site->primary,
'primary' => $primary,
'current' => $thisSite->id === $site->id,
];
}
Expand Down
7 changes: 7 additions & 0 deletions src/models/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,11 @@ class Settings extends VarsModel
*/
public $addXDefaultHrefLang = true;

/**
* @var int The site to use for the `x-default` hreflang tag (0 defaults to the Primary site)
*/
public $xDefaultSite = 0;

/**
* @var bool Whether to dynamically include hreflang tags on paginated pages
*/
Expand Down Expand Up @@ -290,6 +295,8 @@ public function rules(): array
],
'boolean',
],
['xDefaultSite', 'integer'],
['xDefaultSite', 'default', 'value' => 0],
['cspNonce', 'string'],
['cspNonce', 'in', 'range' => [
'',
Expand Down
34 changes: 25 additions & 9 deletions src/templates/settings/plugin/_includes/tags.twig
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,35 @@
id: "addXDefaultHrefLang",
name: "addXDefaultHrefLang",
on: settings.addXDefaultHrefLang,
toggle: ".x-default-wrapper",
warning: configWarning("addXDefaultHrefLang", "seomatic"),
errors: settings.getErrors("addXDefaultHrefLang"),
}) }}

{{ forms.lightswitchField({
label: "Include Paginated `hreflang` Tags"|t("seomatic")|md,
instructions: "Controls whether SEOmatic will automatically include `hreflang` tags on paginated pages. [Learn More](https://moz.com/community/q/hreflang-alternate-pagination)"|t("seomatic"),
id: "addPaginatedHreflang",
name: "addPaginatedHreflang",
on: settings.addPaginatedHreflang,
warning: configWarning("addPaginatedHreflang", "seomatic"),
errors: settings.getErrors("addPaginatedHreflang"),
}) }}
<div class="x-default-wrapper {% if not settings.addXDefaultHrefLang %} hidden{% endif %}">
{{ forms.selectField({
label: "`x-default` site"|md|t("seomatic"),
instructions: "Which site should be used as the `x-default` for the `hreflang` tag (default is the Primary site in Craft)."|md|t("seomatic"),
id: "xDefaultSite",
name: "xDefaultSite",
value: settings.xDefaultSite,
options: {
0: "Primary"|t("seomatic"),
} | merge(sites),
warning: configWarning("xDefaultSite", "seomatic"),
errors: settings.getErrors("xDefaultSite"),
}) }}

{{ forms.lightswitchField({
label: "Include Paginated `hreflang` Tags"|t("seomatic")|md,
instructions: "Controls whether SEOmatic will use `hreflang` tags that point to the paginated page rather than the root page on paginated pages. [Learn More](https://moz.com/community/q/hreflang-alternate-pagination)"|t("seomatic"),
id: "addPaginatedHreflang",
name: "addPaginatedHreflang",
on: settings.addPaginatedHreflang,
warning: configWarning("addPaginatedHreflang", "seomatic"),
errors: settings.getErrors("addPaginatedHreflang"),
}) }}
</div>

{{ forms.lightswitchField({
label: "Generator Enabled"|t("seomatic"),
Expand Down
4 changes: 3 additions & 1 deletion src/translations/en/seomatic.php
Original file line number Diff line number Diff line change
Expand Up @@ -522,5 +522,7 @@
'Body Begin - after the <body> tag ' => 'Body Begin - after the <body> tag ',
'The Asset Transform that should be applied to images before they are added to the sitemap.' => 'The Asset Transform that should be applied to images before they are added to the sitemap.',
'Asset Transform for Images' => 'Asset Transform for Images',
'Sitemap Asset Transform' => 'Sitemap Asset Transform'
'Sitemap Asset Transform' => 'Sitemap Asset Transform',
'Controls whether SEOmatic will use `hreflang` tags that point to the paginated page rather than the root page on paginated pages. [Learn More](https://moz.com/community/q/hreflang-alternate-pagination)' => 'Controls whether SEOmatic will use `hreflang` tags that point to the paginated page rather than the root page on paginated pages. [Learn More](https://moz.com/community/q/hreflang-alternate-pagination)',
'Primary' => 'Primary'
];

0 comments on commit 540b966

Please sign in to comment.