Jump to content

Custom Page name


Jon
 Share

Recommended Posts

Hello,

I am having a bit trouble figuring this out. What I have is a number of different cruises and each cruise has a number of children relating to different dates. For these child pages I would like to generate a custom title. Which would consist of the Parent Cruise Title(Parent Page) - Cruise Date(Child) - and finally Boat Name(Child). This would give me a unique page name.

How would I go about achieving this I realize that this would need to be done after the save so the information can be picked up from the child fields. Ive had a look into some hooks couldnt get my head around them

Cheers

Jon 

Link to comment
Share on other sites

Well you might wanna check into the docs a bit for how to create a simple module that hooks before save. The logic is that you will change the data on the page and then when it is finished saving (as you will be modifying the data on the page before it is comitted) your data will be present.

Good place to start:

http://processwire.com/api/modules/

or http://modules.processwire.com/modules/helloworld/

Also are you trying to create this page title so that you can display it as the pages title in your head? If so you can also just echo out those fields. 

<head>
<?php 

$pageTitle = $page->title;
$pageTitle .= ' | '.$page->fieldName;
$pageTitle .= ' | '.$page->fieldName2;
?>

<title><?=$pageTitle;?></title>
</head>

Or are you trying to create this so that you can view the pages a certain way in your admin, if so then they have you covered here. Go to the template that you are wanting to see the title in a certain format and go to the advanced tab. 

post-2490-0-55938200-1449453600_thumb.pn

  • Like 3
Link to comment
Share on other sites

Try something like this in your ready.php: 

$this->pages->addHookAfter('saveReady', null, 'buildName');

function buildName($event) {
    $page = $event->arguments[0];
    if($page->template != 'cruise') return;
    $page->title = '$page->parent->title . '-' . $page->date . '-' . $page->boat_name';
    $page->name = wire('sanitizer')->pageName($page->title,true);
}

EDIT: You may also want to make use of the Name Format for Children option so that the page title/name initial dialog is not needed. You could have this set to create a dummy name since the code above will rename after anyway.

post-985-0-74040700-1449457678_thumb.png

  • Like 5
Link to comment
Share on other sites

Thanks guys for the help guys. Adrian solution was exactly what I was looking to achieve. I wasn't far way with the module I tried creating initially one thing I was using is the save hook rather than saveReady Can you explain what the difference is between them?

Also are they any benefits regarding doing something like this in a separate module or ready.php?  

Much appreciated guys thanks again :)

Cheers

Jon

Link to comment
Share on other sites

There is a little info on SaveReady here: https://github.com/ryancramerdesign/ProcessWire/blob/dev/wire/core/Pages.php#L2293

With save you need to be careful you don't end up going recursive which can happen when hooking after save because your hook function then needs to call save again, which triggers the hook again :)

Maybe hookbefore save would be ok - I don't actually recall - I just tend to default to saveReady because it usually suits my needs!

  • Like 1
Link to comment
Share on other sites

Sorry Jon - just realized I didn't answer the last part of your question. To read more about ready.php, take a look here:

https://processwire.com/blog/posts/processwire-2.6.7-core-updates-and-more/

I think it is much easier than setting up a module for custom tweaks like this and I don't think there are any disadvantages.

Actually, for admin specific stuff (assuming you aren't saving your pages via the API), you might actually want to put that code in /templates/admin.php so it is only called on the backend.

  • Like 1
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

×
×
  • Create New...