adrianmak Posted February 23, 2015 Share Posted February 23, 2015 By default, a page title will be used for page url, could I use other url pattern (must be assigned automatically) like page id Link to comment Share on other sites More sharing options...
mr-fan Posted February 23, 2015 Share Posted February 23, 2015 There you find your answer.... https://processwire.com/talk/topic/1648-ok-to-change-page-name-path-after-save/#entry15232 best regards mr-fan 1 Link to comment Share on other sites More sharing options...
Soma Posted February 23, 2015 Share Posted February 23, 2015 Just set $page->name manually when creating a page. Link to comment Share on other sites More sharing options...
adrian Posted February 23, 2015 Share Posted February 23, 2015 There is this module: http://modules.processwire.com/modules/page-auto-name/ that lets you define patterns for each template (or sitewide) for automatic page renaming. Another option you might be interested in is: http://modules.processwire.com/modules/process-redirect-ids/ which allows you to access pages by their ID (no matter what else is in the URL), even though they haven't been renamed. 2 Link to comment Share on other sites More sharing options...
LostKobrakai Posted February 23, 2015 Share Posted February 23, 2015 There's no option for this in the core, but you could hook page::saveReady to change it, if it's not the id. The problem about creating the page with the id in the url is, that before the page is actually saved it has no id, but it has to have a name. So you really have to do this after the page creation. Link to comment Share on other sites More sharing options...
mr-fan Posted February 23, 2015 Share Posted February 23, 2015 or you use simply this little snippet in your admin.php: <?php //ignore /** * change page name on publishing a post and add the created date to the name */ $pages->addHook('saveReady', null, 'buildUrl'); function buildUrl(HookEvent $event) { $page = $event->arguments[0]; if($page->template != 'aktiv') return; //for this template only if($page->id && $page->isChanged('status') && !$page->is(Page::statusUnpublished)) { $result = wire('db')->query("SELECT status FROM pages WHERE id={$page->id}"); list($status) = $result->fetch_row(); if($status & Page::statusUnpublished) { // page is about to be published $dateCreated = date("Y-m-d", $page->created); $page->name = $page->title."-".$dateCreated; $page->message($page->created . $page->name); } } } it changes the url only until the page is published...i use this with the awesome module from adrian: http://modules.processwire.com/modules/page-rename-options/ so user could 1. setup a page change the title == changes the name until he publish the page 2. snippet run and change the output that is needed....finish pageurl is protected and stays forever and ever.... ....the modules mentioned are good as well...but i like more and more the power of a few lines of code in PW regards mr-fan 2 Link to comment Share on other sites More sharing options...
adrian Posted February 23, 2015 Share Posted February 23, 2015 EDIT - ignore this and see posts below I love direct sql as much as the next guy, but: $result = wire('db')->query("SELECT status FROM pages WHERE id={$page->id}"); list($status) = $result->fetch_row(); if($status & Page::statusUnpublished) { can be replaced by: if($page->status & Page::statusUnpublished) { You might also find this useful: $page->isNew(); 2 Link to comment Share on other sites More sharing options...
Soma Posted February 23, 2015 Share Posted February 23, 2015 The thing here is to get the previous status the page had before saving. 2 Link to comment Share on other sites More sharing options...
Soma Posted February 23, 2015 Share Posted February 23, 2015 The first question would be why you need Id as the name and not how to do it blindly. Most of th time theres a specific reason for such questions and without knowing more one can't help competently. It might be a complete wrong approach to something cause of lack of experience Link to comment Share on other sites More sharing options...
adrian Posted February 23, 2015 Share Posted February 23, 2015 The thing here is to get the previous status the page had before saving. Thanks soma, and sorry mr-fan! I hadn't thought things through fully - obviously Some more reading on it, in case anyone is interested: https://processwire.com/talk/topic/1705-need-to-send-email-notification-after-a-page-has-been-published/ Link to comment Share on other sites More sharing options...
hellomoto Posted February 23, 2015 Share Posted February 23, 2015 The first question would be why you need Id as the name and not how to do it blindly. Most of th time theres a specific reason for such questions and without knowing more one can't help competently. It might be a complete wrong approach to something cause of lack of experience I was wondering how to do this too before, and ended up using ProcessRedirectIds... hadn't known about AutoPageName. But I wanted to have the page name be its ID for URL purposes, since the title of the listing may be subject to change... although I guess PagePathHistory could take care of that as well. In the aforementioned case though do you think there is a better alternative? Because in that case I would have no other use for the listings' name field, besides to identify it, for which I would use its actual ID [field] anyway... so I really have no use for the name field and might as well just replace it with the ID for the URL, anything else would be over the top. I think. Link to comment Share on other sites More sharing options...
adrianmak Posted February 23, 2015 Author Share Posted February 23, 2015 what is the page name ($page->name) refer to ? the page url ? Link to comment Share on other sites More sharing options...
adrianmak Posted February 24, 2015 Author Share Posted February 24, 2015 is there such a hook of something like after save, such that a post's id is ready , then I could alter the page url/name with this id Link to comment Share on other sites More sharing options...
adrian Posted February 24, 2015 Share Posted February 24, 2015 Well if you use the Page Auto Name module and put "id" in the Pattern field, you'll see that it successfully renames the page to the ID of page, so you could use that, or you could make use of the same hook they use, which is: before Page::save - https://github.com/conclurer/PageAutoName/blob/master/PageAutoName.module#L114 but you could also use saveReady, like mr-fan describes above. Page creation in PW is normally a two step process - first create the title and save - this automatically generates the name and id, and then you add other field content, and/or simply publish or save as unpublished. You need that initial save to have the id generated. 2 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