Jump to content

Specify system titles and names


cryostar
 Share

Recommended Posts

Out of the box, ProcessWire’s “Add Page” functionality is a two-step process:

1. Specify a page title

2. Populate the page

Although there are use cases where you wish you don’t have to direct your content editors to specify a title. So it would be great if we can have some settings in the template where we can set a default title and name “on create” and expose OOTB page properties as tokens like %id%, %createdDate%, and so on.

  • Like 1
Link to comment
Share on other sites

This feature sort of exists already, and it's set on the parent template. If a template is allowed to have only a single child template then there is a setting "Name format for children" available on the "Family" tab. Info: https://processwire.com/docs/modules/guides/process-template/

There is a module that extends the feature:

But my preference is to just set a date format like "Y/m/d H:i:s" for "Name format for children" and then set the final title/name in a hook to Pages::added, e.g.

$pages->addHookAfter('added', function(HookEvent $event) {
	/** @var Page $page */
	$page = $event->arguments(0);
	if($page->template == 'your_template') {
		// Set whatever you want the title and name to be
		$page->of(false);
		$page->title = "Page ID: $page->id";
		$page->name = $event->wire()->sanitizer->pageName($page->title, true);
		$page->save();
	}
});

Unfortunately "modified" and "created" cannot be used in a Pages::added hook because these properties are not populated on new pages until the next request. But you could use time(), or subsequently use the modified/created properties to set the title in a Pages::saveReady hook.

Edited by Robin S
Updated code to set both title and name
  • Like 4
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...