Jump to content
a-ok

Appending page names and multi-languages

Recommended Posts

I'm currently appending page names, with a specific template, with a date. This works fine but as I have multi-languages set up it only appears (unless my code is wrong) to be applying it to the default language page name.

Any thoughts on where I'm going wrong?

function customPageName(HookEvent $event) {
	$page = $event->arguments(0);
	if ($page->template->name == 'about-events-single') {
		$eventDate = date("dmY", $page->global_date_end);
		$pageName = wire()->sanitizer->pageName($page->title . '-' . $eventDate, true);
		$page->setOutputFormatting(false);
		$page->name = $pageName;
	}
}
wire()->addHookAfter('Pages::saveReady', null, 'customPageName');

 

Share this post


Link to post
Share on other sites

You need to iterate through all languages and set the name in all of them. After all, they might also have different titles, so you want to use the local title as well. Something like this:

function customPageName(HookEvent $event) {
	$page = $event->arguments(0);
	if ($page->template->name == 'about-events-single') {
        $eventDate = date("dmY", $page->global_date_end);
		$page->of(false);
        foreach (wire('languages') as $lang) {
            $localTitle = $page->getLanguageValue($lang, 'title');
            $localPageName = wire()->sanitizer->pageName($localTitle . '-' . $eventDate, true);
            $page->setLanguageValue($lang, 'name', $localPageName);
        }
	}
}
wire()->addHookAfter('Pages::saveReady', null, 'customPageName');

Quick and untested, might need some adjustments, but you get the idea 🙂

  • Like 2

Share this post


Link to post
Share on other sites
4 hours ago, MoritzLost said:

Quick and untested, might need some adjustments, but you get the idea 🙂

Much appreciated 🙂

Share this post


Link to post
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

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...