Jump to content

Hook for Page:path on ML site


Zeka
 Share

Recommended Posts

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?

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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
Link to comment
Share on other sites

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
 Share

  • Recently Browsing   0 members

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