Jump to content

Using a different field to generate page URLs


statestreet
 Share

Recommended Posts

I'm in the process of building an internal tool using ProcessWire for my team to write and manage Agile user stories. (It actually started out as a quick prototype, but you know how things go with ProcessWire, I kept going for a few hours and suddenly had a pretty full-featured system. :) )

The way we do user stories, we have a story ID, such as "OP100", and then a title, such as "As an admin, I want to upload a PDF to attach to a user's account." I have these as separate fields, with the latter being the actual page title.

These titles can get somewhat long, however, and can change throughout the lifecycle of a user story, breaking links from Pivotal Tracker. Ideally, I'd like to just use the story ID as the name / URL, so instead of "/as-an-admin-i-want-to-upload-a-pdf-to-attach-to-a-users-account/", the page would just be "/op100/".

Further complicating this is that the story ID is optional (since a story can start out as just a concept that turns into an actual story later). So my ideal workflow would actually be one where the title creates the URL initially, but once there is a story ID, the URL changes to that (or even better, aliases to that). 

One more related thing I'm hoping to do is to concatenate the story ID and title in the admin. Any ideas on how to do this?

  • Like 1
Link to comment
Share on other sites

If you're will to use page ids for the story id, I think this will help out:

http://modules.processwire.com/modules/process-redirect-ids/

Hmm, I think that's out of scope here - @statestreet wants to manually specify the page name, and not base it on the page ID.

I think it might be a better idea to create a new module that hooks onto the page save event. That callback would then check to see if the user/editor has entered a page name (in a separate field on the page's editor), and if he/she has, then change the page name to that field's content.

Yes?

  • Like 2
Link to comment
Share on other sites

maybe the 1-step-adding-of-pages helps? https://processwire.com/talk/topic/3768-processwire-dev-branch/page-9#entry58107

got the link from this thread: https://processwire.com/talk/topic/2106-creating-a-page-without-a-title/

didn't read this one, but could also be relating? https://processwire.com/talk/topic/1648-ok-to-change-page-name-path-after-save/

[edit] ok i think it's not exactly what you are looking for but maybe you can adjust the workflow with a simple hook (never done one yet but everyone says that's simple :D )

[edit2] here is the "more" link from ryans screenshot: http://processwire.com/api/modules/process-template/

you can also write "OP" in the "name format for new children" field and PW will automatically increment it to a unique url like OP-8, OP-9...

don't know if that's ok for you or you have to define your project id manually...

Link to comment
Share on other sites

 These titles can get somewhat long, however, and can change throughout the lifecycle of a user story, breaking links from Pivotal Tracker. Ideally, I'd like to just use the story ID as the name / URL, so instead of "/as-an-admin-i-want-to-upload-a-pdf-to-attach-to-a-users-account/", the page would just be "/op100/".

Further complicating this is that the story ID is optional (since a story can start out as just a concept that turns into an actual story later). So my ideal workflow would actually be one where the title creates the URL initially, but once there is a story ID, the URL changes to that (or even better, aliases to that). 

One more related thing I'm hoping to do is to concatenate the story ID and title in the admin. Any ideas on how to do this?

regarding the first part I think @Mike_Anthonys solution is the best one.

regarding that with the alias maybe you can use the page-path-history module?

and if I get it right, that with the concatenation in the admin, (you mean customizing the PageTree?) you may have a look here.

Link to comment
Share on other sites

Thanks, everyone. Adrian's solution should take care of my main problem, simply having a short permalink that doesn't have an old version of the title stuck in it. I'll see if I can find a URL scheme that includes the story ID that's also not too confusing, but it's not critical.

Horst, I will look into that link on customizing the PageTree view. 

Link to comment
Share on other sites

  • 2 weeks later...

I had no idea modules were so easy! :)

Once I understood the basics of what was going on, I was able to quickly adapt the PageTree thumbnails module:

	public function addPageListLabelItems($event) {
		$page = $event->arguments('page');
		if(count($page->story_id)) {
			$event->return = $page->story_id . " - " . $event->return;
		}
	}

Then for the story ID URLs, I added a hook before save:

	public function init() {
		$this->pages->addHookBefore('save', $this, 'changeName'); 
	}

	public function changeName($event) {
		$page = $event->arguments[0];
		if ($page->template=='story' && $page->story_id!="") {
			$page->name = $page->story_id;
		}		
	}

However, this does still leave the edge case of someone entering an existing story ID and then having a name collision. What I want to do is check if the story ID already exists, and then increment this page's URL (the second instance of OP100 becomes OP100-2). However, my attempt at page search within a module is clearly not the right way:

if (count($pages->find("story_id=" . $page->story_id))) { // this doesn't work
Link to comment
Share on other sites

Thanks Adrian!

I decided to just tack ProcessWire's page ID on the end if the story ID is a duplicate:

	public function changeName($event) {
		$page = $event->arguments[0];
		if ($page->template=='story' && $page->story_id!="") {
			if (count($this->pages->find("story_id=" . $page->story_id))) { 
				// uh-oh, we found another page with this story ID
				$page->name = $page->story_id . "-" . $page->id;
			} else {
				$page->name = $page->story_id;
			}
		}		
	}
  • Like 1
Link to comment
Share on other sites

  • 3 years later...

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

×
×
  • Create New...