Skip to content

Commit

Permalink
[Added] Added a getLocalizedUrls Twig filter/function that returns an…
Browse files Browse the repository at this point in the history
… array of localized URLs for the current request
  • Loading branch information
khalwat committed Jun 20, 2016
1 parent d779a24 commit 94d938b
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions releases.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
48 changes: 48 additions & 0 deletions services/SeomaticService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
-------------------------------------------------------------------------------- */
Expand Down
5 changes: 3 additions & 2 deletions templates/_seo_meta.twig
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@
<meta name="description" content="{{ seomaticMeta.seoDescription |raw }}" />
<meta name="generator" content="SEOmatic" />
<link rel="canonical" href="{{ seomaticMeta.canonicalUrl }}" />
{% for locale in craft.i18n.getSiteLocales() %}
{% if craft.config.siteUrl is iterable %}{% if craft.config.siteUrl[locale.getId()] is defined %}<link rel="alternate" hreflang="{{ locale.getId() |lower |replace ('_', '-') }}" href="{{ craft.config.siteUrl[locale.getId()] ~ craft.request.path }}" />{% endif%}{% endif %}
{% set localizedUrls = getLocalizedUrls() %}
{% for key, value in localizedUrls %}
<link rel="alternate" hreflang="{{ key |lower |replace ('_', '-') }}" href="{{ value }}" />
{% endfor %}
{% if seomaticIdentity.address.addressRegion is defined and seomaticIdentity.address.addressRegion %}
<meta name="geo.region" content="{{ seomaticIdentity.address.addressRegion |raw }}" />
Expand Down
13 changes: 13 additions & 0 deletions twigextensions/SeomaticTwigExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
);
}
Expand All @@ -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'),
);
}
Expand Down Expand Up @@ -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
-------------------------------------------------------------------------------- */
Expand Down
11 changes: 11 additions & 0 deletions variables/SeomaticVariable.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
-------------------------------------------------------------------------------- */
Expand Down

0 comments on commit 94d938b

Please sign in to comment.