From 94d938baae4a5d1f68bf776c5bf1e1d251a29efa Mon Sep 17 00:00:00 2001 From: Andrew Welch Date: Mon, 20 Jun 2016 16:04:37 -0400 Subject: [PATCH] [Added] Added a getLocalizedUrls Twig filter/function that returns an array of localized URLs for the current request --- README.md | 1 + releases.json | 1 + services/SeomaticService.php | 48 ++++++++++++++++++++++++ templates/_seo_meta.twig | 5 ++- twigextensions/SeomaticTwigExtension.php | 13 +++++++ variables/SeomaticVariable.php | 11 ++++++ 6 files changed, 77 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 56da61c..2b40f51 100755 --- a/README.md +++ b/README.md @@ -55,6 +55,7 @@ Some things to do, and ideas for potential features: ### 1.1.21 -- 2016.06.16 +* [Added] Added a `getLocalizedUrls` Twig filter/function that returns an array of localized URLs for the current request * [Improved] The SEOmetrics window will now remember its open/closed state while in Live Preview * [Improved] Some minor tweaks to the SEOmetrics CSS * [Improved] The current locale is now included in the hreflang for localized sites diff --git a/releases.json b/releases.json index e864d92..8d4bb8b 100644 --- a/releases.json +++ b/releases.json @@ -4,6 +4,7 @@ "downloadUrl": "https://github.com/nystudio107/seomatic/archive/master.zip", "date": "2016-06-16T11:00:00-11:00", "notes": [ + "[Added] Added a getLocalizedUrls Twig filter/function that returns an array of localized URLs for the current request", "[Improved] The SEOmetrics window will now remember its open/closed state while in Live Preview", "[Improved] Some minor tweaks to the SEOmetrics CSS", "[Improved] The current locale is now included in the hreflang for localized sites", diff --git a/services/SeomaticService.php b/services/SeomaticService.php index c5cad8b..13dd2e7 100644 --- a/services/SeomaticService.php +++ b/services/SeomaticService.php @@ -2313,6 +2313,54 @@ public function sanitizeMetaVars(&$metaVars) } /* -- sanitizeMetaVars */ +/* -------------------------------------------------------------------------------- + Returns an array of localized URLs for the current request +-------------------------------------------------------------------------------- */ + +public function getLocalizedUrls() +{ + $localizedUrls = array(); + $requestUri = craft()->request->getRequestUri(); + if (craft()->isLocalized()) + { + $element = craft()->urlManager->getMatchedElement(); + if ($element) + { + $unsortedLocalizedUrls = array(); + $_rows = craft()->db->createCommand() + ->select('locale') + ->addSelect('uri') + ->from('elements_i18n') + ->where(array('elementId' => $element->id, 'enabled' => 1)) + ->queryAll(); + + foreach ($_rows as $row) + { + $path = ($row['uri'] == '__home__') ? '' : $row['uri']; + $unsortedLocalizedUrls[$row['locale']] = UrlHelper::getSiteUrl($path, null, null, $row['locale'] ); + } + + $locales = craft()->i18n->getSiteLocales(); + foreach ($locales as $locale) + { + $localeId = $locale->getId(); + $localizedUrls[$localeId] = $unsortedLocalizedUrls[$localeId]; + } + } + else + { + $locales = craft()->i18n->getSiteLocales(); + foreach ($locales as $locale) + { + $localeId = $locale->getId(); + $localizedUrls[$localeId] = UrlHelper::getSiteUrl($requestUri, null, null, $localeId); + + } + } + } + return $localizedUrls; +} /* -- getLocalizedUrls */ + /* -------------------------------------------------------------------------------- Get a fully qualified URL based on the siteUrl, if no scheme/host is present -------------------------------------------------------------------------------- */ diff --git a/templates/_seo_meta.twig b/templates/_seo_meta.twig index 6675822..1dccf62 100755 --- a/templates/_seo_meta.twig +++ b/templates/_seo_meta.twig @@ -13,8 +13,9 @@ -{% for locale in craft.i18n.getSiteLocales() %} -{% if craft.config.siteUrl is iterable %}{% if craft.config.siteUrl[locale.getId()] is defined %}{% endif%}{% endif %} +{% set localizedUrls = getLocalizedUrls() %} +{% for key, value in localizedUrls %} + {% endfor %} {% if seomaticIdentity.address.addressRegion is defined and seomaticIdentity.address.addressRegion %} diff --git a/twigextensions/SeomaticTwigExtension.php b/twigextensions/SeomaticTwigExtension.php index 62087ad..bb47eed 100755 --- a/twigextensions/SeomaticTwigExtension.php +++ b/twigextensions/SeomaticTwigExtension.php @@ -56,6 +56,7 @@ public function getFilters() 'truncateStringOnWord' => new \Twig_Filter_Method($this, 'truncateStringOnWord'), 'encodeEmailAddress' => new \Twig_Filter_Method($this, 'encodeEmailAddress'), 'extractTextFromMatrix' => new \Twig_Filter_Method($this, 'extractTextFromMatrix'), + 'getLocalizedUrls' => new \Twig_Filter_Method($this, 'getLocalizedUrls'), 'getFullyQualifiedUrl' => new \Twig_Filter_Method($this, 'getFullyQualifiedUrl'), ); } @@ -73,6 +74,7 @@ public function getFunctions() 'truncateStringOnWord' => new \Twig_Function_Method($this, 'truncateStringOnWord'), 'encodeEmailAddress' => new \Twig_Function_Method($this, 'encodeEmailAddress'), 'extractTextFromMatrix' => new \Twig_Function_Method($this, 'extractTextFromMatrix'), + 'getLocalizedUrls' => new \Twig_Function_Method($this, 'getLocalizedUrls'), 'getFullyQualifiedUrl' => new \Twig_Function_Method($this, 'getFullyQualifiedUrl'), ); } @@ -176,6 +178,17 @@ public function extractTextFromMatrix($matrixBlocks) return $result; } /* -- extractTextFromMatrix */ +/* -------------------------------------------------------------------------------- + Returns an array of localized URLs for the current request +-------------------------------------------------------------------------------- */ + + public function getLocalizedUrls() + { + $result = craft()->seomatic->getLocalizedUrls(); + + return $result; + } /* -- getLocalizedUrls */ + /* -------------------------------------------------------------------------------- Get a fully qualified URL based on the siteUrl, if no scheme/host is present -------------------------------------------------------------------------------- */ diff --git a/variables/SeomaticVariable.php b/variables/SeomaticVariable.php index 7a86ea0..e6d37f0 100755 --- a/variables/SeomaticVariable.php +++ b/variables/SeomaticVariable.php @@ -107,6 +107,17 @@ public function extractTextFromMatrix($matrixBlocks) return $result; } /* -- extractTextFromMatrix */ +/* -------------------------------------------------------------------------------- + Returns an array of localized URLs for the current request +-------------------------------------------------------------------------------- */ + + public function getLocalizedUrls() + { + $result = craft()->seomatic->getLocalizedUrls(); + + return $result; + } /* -- getLocalizedUrls */ + /* -------------------------------------------------------------------------------- Get a fully qualified URL based on the siteUrl, if no scheme/host is present -------------------------------------------------------------------------------- */