Internally, $field->slug() uses Str::slug() which has only ‘a-z0-9’ in the allowed list. There should be a way to modify the allowed characters, though. In the worst case, you can use a custom field method. But let me check how to redefine the allowed list.
Edit: No, that won’t help either, because Str::slug() also converts to ascii, thus removing the en dash again, even if it is in the allowed list.
So, I think a custom field method would be the way to go.
The slug field, however, doesn’t use the PHP functions, but the slug helper on the JavaScript side. While the rules seem to be the same, there might a difference in how the en dash is interpreted or so.
Thank you for the insight!
I don’t know if this is the best solution, but maybe it will help someone in the future.
This is what I came up with:
Kirby::plugin('your/plugin', [
'fieldMethods' => [
'customSlug' => function ($field) {
// Check if the field has a value
if (!$field->value()) {
return '';
}
// Get the original field value
$originalValue = $field->value();
// Replace en dashes with regular dashes
$fixedSlug = preg_replace('/\s?–\s?/u', '-', $originalValue);
// Transform the fixed slug into a Kirby-like slug
$finalSlug = Str::slug($fixedSlug);
return $finalSlug;
},
],
]);