Zeka Posted December 29, 2017 Posted December 29, 2017 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?
adrian Posted December 30, 2017 Posted December 30, 2017 Have you tried the Page::localPath hook which is actually the name of the version in the LanguageSupportPageNames.module file.
Zeka Posted December 30, 2017 Author Posted December 30, 2017 Hi @adrian. Thanks for your help. Are Page:localPath, Page::localUrl hookable? I tried hook after Page::localPath, but it does nothing.
dragan Posted December 30, 2017 Posted December 30, 2017 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.).
Zeka Posted December 30, 2017 Author Posted December 30, 2017 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)); 1
DV-JF Posted July 2 Posted July 2 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: but not on the edit screen: 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?
DV-JF Posted July 3 Posted July 3 My question has already been answered elsewhere by you @Zeka 😆 Apologize for bothering you @adrian & @Zeka 🤐
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now