$field->slug() removes en dash

I try to use the $field->slug() and realized that it removes the en dash (–) completely instead of replacing it with a hyphen.

For example:
$field = Year 2022–2023
$field->slug = year-20222023

I would have expected, that the rules are the same as for the slugs which are created for the $pages, which works fine and I get year-2022-2023.

What am I missing? I could not figured it out from the docs.

Can you post the corresponding code from your blueprint?

In my blueprint I have a keyword field with a slug function, where the conversion takes place as expected. I guess you’re only using a text field.

keywords:
  label: Schlüsselwörter
  type: structure
  sortBy: identifier asc
  fields:
    tag:
      label: Schlüsselwort
      type: text
      width: 1/2
    identifier:
      label: konvertierte Version
      type: slug
      sync: tag
      width: 1/2

Yes the slug field does also work for me like that.
But my blueprint looks like this:

pageLinks:
    width: 1/2
    type: structure
    fields:
      pageLink:
        label: Link
        type: select
        options: query
        query: 
          fetch: page.sections.toblocks.filterBy("type", "section-header")
          text: "{{ block.sectionTitle.slug }}"
          value: "{{ block.sectionTitle.slug }}"

I also testet it outside of a blocks query, same result.
And also the same if I test it in a template like $sectionTitle->slug()

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;
        },
    ],
]);