Question:
How do I redirect to a newly updated page slug after updating the page’s slug?
Situation:
I have a page called Work with subpages called project - each project’s title and url appendix is generated from two fields; Client and project title.
On the project page I can update either client or project title which correctly set the new title and slug but I am unable to redirect to the new page.
Project.yml blueprint:
#
# Blueprint for Projects
#
# page title
title: Project
# options for when creating a new work projects
create:
title: "{{ page.client }} – {{ page.project }}"
fields:
- client
- project
slug: "{{ page.client }}-{{ page.project }}"
# options for available status options
status:
listed: Published
draft: Draft
unlisted: Unlisted
#
# Content
#
tabs:
#
# TAB 1 - content
#
content:
label: Content
icon: text
columns:
# layout
media:
width: 1/2
fields:
#
# Project - media
#
mediaGallery:
type: files
layout: cards
size: medium
label: Project Media
uploads: false
help: select media, to upload see storage tab.
sortable: true
info: "{{ file.template }}"
#
# Project - information
#
information:
width: 1/2
fields:
#
# Project - client
#
client:
label: Project client
width: 1/2
type: select
required: true
placeholder: select client
help: Select client for project, or create new under Work > Settings
options:
type: query
query: site.find("work").clients.toStructure
text: "{{ item.clientname }}"
value: "{{ item.clientname }}"
#
# Project - title
#
project:
label: project title
width: 1/2
type: text
help: Enter the project name
required: true
#
# Project - url
#
project url:
type: headline
help: "{{ page.url }}"
# break
lineA:
type: line
#
# Project - categories
#
tags:
type: tags
label: Categories
min: 1
accept: options
help: Select one or more categories for the project, or create new under Work > Settings
options:
type: query
query: site.find("work").categories.toStructure
text: "{{ item.category }}"
value: "{{ item.category }}"
# break
lineB:
type: line
#
# Project - description
#
description:
type: textarea
size: small
buttons: false
toggleDisplayDescription:
label: Hide description on project page?
type: toggle
help: Hide the description on the project page but will keep it for SEO.
default: "no"
options:
- no
- yes
# break
lineC:
type: line
#
# Project - credit list
#
credits:
label: Credit list
type: structure
fields:
name:
label: name
type: text
role:
label: role
type: text
link:
label: link
type: url
#
# TAB 2 - media storage
#
storage:
label: Media
icon: import
fields:
#
# Media - Image uploading section
#
imageMedia:
label: Image upload
help: Upload project images here.
type: files
layout: cards
size: medium
uploads: image
info: "{{ file.template }}"
# break
lineD:
type: line
#
# Media - Video uploading section
#
videoMedia:
label: Video upload
help: Upload project videos here.
type: files
layout: cards
size: medium
uploads: video
info: "{{ file.template }}"
# break
lineF:
type: line
#
# Media - Audio uploading section
#
audioMedia:
label: Audio upload
help: Upload project audio here.
type: files
layout: cards
size: medium
uploads: audio
info: "{{ file.template }}"
“Page.update:after” hook:
"page.update:after" => function (
Kirby\Cms\Page $newPage,
Kirby\Cms\Page $oldPage
) {
$client = $newPage->client()->value();
$project = $newPage->project()->value();
function convertText($inputText)
{
// Replace spaces with dashes
$textWithDashes = str_replace(" ", "-", $inputText);
// Convert non-English characters to Latin
$latinText = iconv("UTF-8", "ASCII//TRANSLIT", $textWithDashes);
return $latinText;
}
$cleanClient = convertText($client);
$cleanProject = convertText($project);
$newPage->changeTitle($client . " - " . $project);
$newPage->changeSlug($client . " - " . $project);
},
Any suggestions or hints would be greatly appreciated.
Kirby: 4.1.0
PHP: 8.3.3
Browser: Safari 17.3
OS: Mac 14.3