1

I'm trying to create a mechanism for informing users that a particular entry is not available in the selected language, and providing a link to read the article in an alternative language.

Therefore, if one of my secondary locales returns a 404 for a particular URL and goes to a localised 404 error template, is there a way to check in the 404 template if the corresponding URL path matches an entry in my main locale, so I can link to that entry from the 404 page?

I think what I want is a method for searching for an entry using a string containing the complete URL path, but I don't think that’s available, so what would be the best alternative?

4
  • I don't think this is possible unless you store every request from each user in a session and check the last one in a 404 request. My question: why do you even show/display links to a 404 request. It's better to not link non existing entries or link them in another local (with a hint) from the beginning. Internal 404 links are no good Oct 23, 2017 at 4:36
  • Yes, that's a good point.
    – Alex H
    Oct 23, 2017 at 8:47
  • The reason I'm linking to a 404 is because each locale link in the language switcher module is designed to link to the same article in the alternative language, and the client doesn't want to redirect to the locale homepage but to a page that informs them that that page isn't available in the selected language, and provides alternatives. I'll have a think.
    – Alex H
    Oct 23, 2017 at 8:55
  • 1
    Then you have to create your custom getUrl() function.. Go to BaseElementModel->getUrl() and you'll see what happens there.. just copy/paste the function in your pluginVariable.php (with the entryId param as) and include a check whether the entry for that local exists and try to fetch the one in your local if not $url = UrlHelper::getSiteUrl($path, null, null, "your new local eg. fr or en or whatever the handle is"); In your template you just use {{ craft.pluginHandle.getUrl(entryId) }} Oct 23, 2017 at 9:06

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.