a-ok Posted March 7, 2019 Share Posted March 7, 2019 I'm aware you can change the path of a path by hooking into `Page::path` but I need to amend the URL rather than the path (change it to an external URL that's in a field on the page). $pages->addHookAfter('Page::path', function($event) { $page = $event->object; if ($page->products_categories_external) { bd($page); $event->return = "$page->products_categories_external"; } }); Any thoughts? Link to comment Share on other sites More sharing options...
elabx Posted March 7, 2019 Share Posted March 7, 2019 What I've done in this scenarios is adding a new method through a hook to pages, something like $page->customUrl() that checks conditions before outputting the url. Link to comment Share on other sites More sharing options...
a-ok Posted March 7, 2019 Author Share Posted March 7, 2019 20 minutes ago, elabx said: What I've done in this scenarios is adding a new method through a hook to pages, something like $page->customUrl() that checks conditions before outputting the url. You mean something like this? wire()->addHookMethod('Page::customUrl', function($event) { $page = $event->object; if ($page->products_categories_external) { $page->url = $page->products_categories_external } else { $page->url = $page->url; } }); The issue is I'm using `MarkupSimpleNavigation` (so normally I'd just write a check) which is why I was thinking a hook that would modify the URL. Link to comment Share on other sites More sharing options...
adrian Posted March 7, 2019 Share Posted March 7, 2019 Changing the path will result in a changed url so I guess I am missing the point somehow. Here is the classic thread on this topic: https://processwire.com/talk/topic/1799-routes-and-rewriting-urls/ Link to comment Share on other sites More sharing options...
a-ok Posted March 7, 2019 Author Share Posted March 7, 2019 6 minutes ago, adrian said: Changing the path will result in a changed url so I guess I am missing the point somehow. Here is the classic thread on this topic: https://processwire.com/talk/topic/1799-routes-and-rewriting-urls/ I thought so too but all I get is `https://local.dev/jambhttps://www.hawkerantiques.com` Maybe it's related to `MarkupSimpleNavigation` EDIT: It's not... it happens with normal `$page->url` outputs. Link to comment Share on other sites More sharing options...
adrian Posted March 7, 2019 Share Posted March 7, 2019 The issue is that hooking the path affects the relative url, not the full http url. You can do a redirect to that $page->products_categories_external rather than an $event->return. You could do this from your path hook or maybe it might even be better to do it from Page::render Link to comment Share on other sites More sharing options...
a-ok Posted March 7, 2019 Author Share Posted March 7, 2019 21 minutes ago, adrian said: The issue is that hooking the path affects the relative url, not the full http url. You can do a redirect to that $page->products_categories_external rather than an $event->return. You could do this from your path hook or maybe it might even be better to do it from Page::render Do you mean a `$session->redirect()`? Hmmm... Link to comment Share on other sites More sharing options...
adrian Posted March 7, 2019 Share Posted March 7, 2019 2 minutes ago, a-ok said: Do you mean a `$session->redirect()`? Hmmm... Yep! Link to comment Share on other sites More sharing options...
a-ok Posted March 7, 2019 Author Share Posted March 7, 2019 14 minutes ago, adrian said: Yep! I could just do this within the template rather than as a hook. But the URL would still remain as the href and it’s a bit confusing. So currently there’s no way to hook into the full URL? Link to comment Share on other sites More sharing options...
adrian Posted March 7, 2019 Share Posted March 7, 2019 Actually, looking at this, it works as expected for me: 3 Link to comment Share on other sites More sharing options...
Robin S Posted March 7, 2019 Share Posted March 7, 2019 14 minutes ago, adrian said: Actually, looking at this, it works as expected for me Works for me too, but would depend on what $config->urls->root is (e.g. if developing in a subfolder?) 5 hours ago, a-ok said: The issue is I'm using `MarkupSimpleNavigation` (so normally I'd just write a check) which is why I was thinking a hook that would modify the URL. If it's just MSN you are concerned about the simplest thing is to use the features built into MSN for just this purpose. Either then "xitem_tpl" option or a "getItemString" hook: // MSN hook for external_link template $nav->addHookAfter('getItemString', function($event) { $child = $event->arguments('page'); if($child->template == 'external_link') { $event->return = "<a href='{$child->link_url}' target='_blank'>$child->title</a>"; } }); 2 Link to comment Share on other sites More sharing options...
a-ok Posted March 8, 2019 Author Share Posted March 8, 2019 13 hours ago, Robin S said: Works for me too, but would depend on what $config->urls->root is (e.g. if developing in a subfolder?) If it's just MSN you are concerned about the simplest thing is to use the features built into MSN for just this purpose. Either then "xitem_tpl" option or a "getItemString" hook: // MSN hook for external_link template $nav->addHookAfter('getItemString', function($event) { $child = $event->arguments('page'); if($child->template == 'external_link') { $event->return = "<a href='{$child->link_url}' target='_blank'>$child->title</a>"; } }); Thanks. I can't get it to work but my $config->urls->root is "/jamb/" as I am working locally (and my dev server has a similar setup) of sub-folders (root > each project). Would be good if we could amend this regardless (maybe on the roadmap?) but thanks (and @adrian too) for the explanation. Re the hook onto 'getItemString' – I'm actually using this to concat an item... for some reason I didn't think to use it to alter the item itself. So that works. Link to comment Share on other sites More sharing options...
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