Jump to content

Recommended Posts

Posted

Hi.

I'm working on ML site and trying to hooks page url/path.

Here the code that I'm using:

$pages->addHookAfter("Page(template=projects-category|project)::path", function (HookEvent $e) {
    $page = $e->object;
    if ($page->matches("has_parent=/projects/")) {
        $e->return = "/projects/$page->name/";
    } elseif ($page->matches("has_parent=/products/")) {
        $e->return = "/products/$page->name/";
    }
});

Nothing fancy and it works when default language is active. But when I switch to non-default langauge I get default page url.

As far as I see the issue is relative to other Page::path hook in LanguageSupportPageNames.module.

I was trying to prioritize hook by passing

array('priority'=>2000)

as last argument, but it doesn't sole the issue.

Any  thoughts?

Posted

Have you tried the Page::localPath hook which is actually the name of the version in the LanguageSupportPageNames.module file.

Posted

Hi @adrian. Thanks for your help.

Are Page:localPath, Page::localUrl hookable? I tried hook after Page::localPath, but it does nothing.

 

Posted

They should be, all seven listed here: https://github.com/ryancramerdesign/ProcessWire/blob/master/wire/modules/LanguageSupport/LanguageSupportPageNames.module#L133

However, there's also this comment a few lines down, that may clash with what you're doing:

        // verify that page path doesn't have mixed languages where it shouldn't
        $redirectURL = $this->verifyPath($this->requestPath);
        if($redirectURL) return wire('session')->redirect($redirectURL); 

In your alternate languages, the pagenames for products and projects are probably different (i.e. produkte / projekte in german etc.).

Posted

Hi @dragan

First of all thank you for your help @adrian @dragan

I was wrong about localPath and localName. They are hookable. The problem was that I never called them in my templates, so didn't get any changes.

$pages->addHookAfter("Page(template=projects-category|project)::localUrl", function (HookEvent $e) {;
    $page = $e->object;
    $pages = wire('pages');
    $user = wire('user');
    $language = $user->language;
    $langSegment = $pages->get("/")->localPath($language);
    $pageName = $page->localName($language);
    if ($page->matches("has_parent=projects")) {
        $e->return = $langSegment . "projects/$pageName/";
    } elseif ($page->matches("has_parent=products")) {
        $e->return = $langSegment . "products/$pageName/";
    }
});

Hook to Page::path also works. The issue was in my "matches" selector )))))

$pages->addHookAfter("Page(template=projects-category|project)::path", function (HookEvent $e) {
    $page = $e->object;
    $pages = wire('pages');
    $user = wire('user');
    $language = $user->language;
    $langSegment = $pages->get("/")->localPath($language);
    $pageName = $page->localName($language);
    if ($page->matches("has_parent=projects")) {
        $e->return = $langSegment . "projects/$pageName/";
    } elseif ($page->matches("has_parent=products")) {
        $e->return = $langSegment . "products/$pageName/";
    };
}, array('priority'=>101));

 

  • Like 1
  • 7 years later...
Posted

Hey @Zeka, hey @adrian,
thank you for your code-examples. They seem to work pretty well for me!

However, I have discovered a strange problem that I can't explain. Maybe one of you knows what I have to do to get the corrected link displayed on the actual page in the backend. I'll try to clarify it with 2 screenshots:

In the page-tree the link is correct:

1010102102_2025-07-0213_57_34-.thumb.png.8dae25dcce13a299ea93760689c51ea8.png

but not on the edit screen:

654558497_2025-07-0213_59_07-.thumb.png.f839703893662a478c31c511e26db258.png

This is my code in ready.php:

$pages->addHookAfter("Page(template=product-item|home-temp)::path", function (HookEvent $e) { 
    $page = $e->object;
    $pages = wire('pages');
    $user = wire('user');
    $language = $user->language;
    $langSegment = $pages->get("/")->localPath($language);
    $pageName = (!empty($page->localName($language))) ? $page->localName($language) : $page->name;
    // Products
    if ($page->matches("has_parent=products")) {
        $parent=$pages->get('name=products')->parent->localName($language);
        $e->replace = true;
        $e->return = $langSegment . "$parent/$pageName/";
    }
    // Lead-Messe
    if ($page->parent->matches("name=lead-pages")) {
        $e->replace = true;
        $e->return = $langSegment .$pageName.'/';
        }
}, array('priority'=>101));

Any ideas?

EDIT: To clarify my question: Which hook can I use to change these links?

1093589525_2025-07-0214_39_58-BearbeiteSeite_POWTECH2025poernhecht.vieregg.designMozillaFirefox.thumb.png.faff6ba1204cd29fc9e9363aaab4e6cf.png

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...