Magento – get url redirect for a cms page

cmsenterprise-1.14magento-enterpriseurl-rewrite

I'm currently implementing a multilanguage store in magento enterprise using german and french store views. one of the requirements is that paths are translated. as an example, I'm trying to link the cms page for the ToS in the layout's footer:

defined cms pages:

  • "AGB" (german, url key: 'page-info-tos', store view 'de')
  • "CGV" (french, url key: 'page-info-tos', store view 'fr')

defined redirects:

  • "agb" → "page-info-tos"; store view 'de'; no redirect
  • "cgv" → "page-info-tos"; store view 'fr'; no redirect
  • "cgv" → "agb"; store view 'de'; 301-redirect
  • "agb" → "cgv"; store view 'fr'; 301-redirect

code in footer.phtml:

<a href="<?php echo $this->getUrl('page-info-tos');"><?php echo $this->__('ToS'); ?></a>

resulting html:

<a href="/de/page-info-tos">AGB</a> resp <a href="/fr/page-info-tos">CGV</a>

wanted html:

<a href="/de/agb">AGB</a> resp <a href="/fr/cgv">CGV</a>


how can I lookup the correct request path for the given cms page url key?

Best Answer

by querying the Enterprise_UrlRewrite Url_Rewrite model, it's possible to get the translated url.

prerequisite: defined redirects:

  • "agb" → "page-info-tos"; store view 'de'; no redirect
  • "cgv" → "page-info-tos"; store view 'fr'; no redirect
  • "page-info-tos" → "agb"; store view 'de'; 301-redirect (only used for lookup)
  • "page-info-tos" → "cgv"; store view 'fr'; 301-redirect (only used for lookup)

php code to get the translated url in footer.phtml:

function getTranslatedPath($key)
{
    $urw = Mage::getModel('enterprise_urlrewrite/url_rewrite');
    /* @var $urw Enterprise_UrlRewrite_Model_Url_Rewrite */

    if ($urw->loadByRequestPath(['request' => $key])->getId())
        return $urw->getTargetPath();
    else
        return $key; // no rewrite found or matched
}

?>
<a href="<?php echo $this->getUrl(getTranslatedPath('page-info-tos')); ?>">...</a>

although it works - href is http://example.com/de/agb/ - this feels like an awkward solution. shouldn't rather $this->getUrl('page-info-tos', ['_use_redirect' => true]) do this lookup for me?