Jump to content

could i use other url pattern for a page creation ?


adrianmak
 Share

Recommended Posts

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.

  • Like 2
Link to comment
Share on other sites

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

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

  • Like 2
Link to comment
Share on other sites

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();
  • Like 2
Link to comment
Share on other sites

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

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

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.

  • Like 2
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...