Jump to content

Set page title automatically


Robin S
 Share

Recommended Posts

My project has a page that I want to store archived versions of. This isn't for version control or anything like that - it's just a page that gets regularly updated and visitors should be able to browse the previous versions of it.

So my idea is to have a page structure like this:

  • My Page
    • My Page - 16 April 2016
    • My Page - 9 April 2016
    • My Page - 28 March 2016

The top "My Page" just pulls in the content from the most recent child page and then displays a list of links to all children but the most recent.

To create a new version of the page an editor will create a new child of "My Page". But I don't want them to have to enter a page title - the title should always be "My Page - [creation date]".

I hadn't had reason to use the "Name format for children" template option before and I thought it was going to be the solution. But now I see that it just sets the name and not the title of child pages. Which puzzles me a bit. Is this option mostly intended for if you change Title to be non-global and then remove it from the template? Wouldn't it be better if this option automatically set the title rather than the name and then derived the name from the title as per normal?

Anyway, main question: is there something I can hook into to either

  • Fill out the title field in the first step of the Add New page process (and ideally make it non-editable by the user)
  • Or fill out the title field in the second step of the Add New page process. Then I can skip the first step via the "Name format for children" option and set the title field visibility to locked so it's not editable. This might be the better option.
Link to comment
Share on other sites

hi robin,

as simple as this: (put it in your /site/ready.php file and adjust to your needs)

$this->pages->addHookAfter('added', function($event) {
    $page = $event->arguments[0];
    if($page->template != 'your-template-name') return;
    $page->setAndSave('title', 'my page - ' . date("YmdHis"));
});

set page name format for children to something like Y/m/d H:i:s and set title to locked

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