Jump to content

Stable solution for custom paths/URLs in a multilingual context


Recommended Posts

Posted

Since version 3.0.173, URL/path hooks are thankfully available. I built a solution to overwrite page paths using an input field on pages for the public. If filled, the Page::path hook returns my custom path string, which means URLs etc. are even auto-updated. A second hook checks all incoming requests and delivers the page by matching the defined path/URL.

# ready.php // Change page path
$wire->addHookBefore('Page::path', function($event) {
    $page = $event->object;
    if (!empty($page->page_generic_urlpath)) {
        $event->replace = true;
        $event->return = $page->page_generic_urlpath;
    }
});

# ready.php // Route page path back
$wire->addHook('/([a-zA-Z0-9-/]+)/', function($event) {
    $urlpath = '/' . $event->arguments(1) . '/';
    $page = $event->pages->findOne('page_generic_urlpath=' . $urlpath . ', include=hidden');
    return $page->id && $page->viewable ? $page : $event->return;
});

 This works fine as long as multilanguage support is not required. However, with multiple languages, it becomes much more complicated. The Page::path hook is useless, and the logic needs to be implemented in LanguageSupportPageNames->getPagePath(), which is not hookable. In short, I can't find a good way to map my multilingual custom paths from an input field to the corresponding localized language paths.

I ended up making minimal edits in LanguageSupportPageNames->getPagePath() to replace multilingual paths when necessary. However, I would prefer an elegant solution over a "core module hack". Does anyone have a good idea for changing page language local paths via hooks? In my tests, Page::localPath is neither hookable nor defined with underscores. Changing the deeper internal language logic seems impossible. Or not?! 🙂

custom_urlpath.thumb.png.48507761218593b65448155d5f41ca58.png

core_edits.png

  • Like 1
  • skeltern changed the title to Stable solution for custom paths/URLs in a multilingual context

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...