Issue Feature Closed

#106072Enable Regex in slug field generatorOptions replacements

André Buchmann 02/03/25, 20:45 updated 12/02/25, 14:26

Description

The slug field provides the option to define replacements: https://docs.typo3.org/m/typo3/reference-tca/main/en-us/ColumnsConfig/Type/Slug/Index.html#confval-slug-generatoroptions-replacements

When having to deal with large multilingual sites, there is the typical gender addition on for example job titles: (f/m), (m/w/d), (w/m/d),…
To remove them form slugs, one need to define any of these variations in the replacements array as the replacement method used is: str_replace. Switching to preg_replace would open a lot of new possibilities with regex to define those replacements.
*Replacing str_replace with preg_replace is a breaking change!*

$replaceConfiguration = $this->configuration['generatorOptions']['replacements'] ?? [];
$pieceOfSlug = str_replace(
    array_keys($replaceConfiguration),
    array_values($replaceConfiguration),
    $pieceOfSlug
);

Comparison of definitions

_.str_replace _.preg_replace
            'title with replace' => [
'Product Cow',
'/parent-page/product-pig',
[
'generatorOptions' => [
'fields' => ['title'],
'prefixParentPageSlug' => true,
'replacements' => [
'Cow' => 'pig',
],
],
],
],
'title with slash and replace' => [
'Product/Cow',
'/parent-page/productcow',
[
'generatorOptions' => [
'fields' => ['title'],
'prefixParentPageSlug' => true,
'replacements' => [
'/' => '',
],
],
],
],
'title with slash and replace #2' => [
'Some Job in city1/city2 (m/w)',
'/parent-page/some-job-in-city1-city2',
[
'generatorOptions' => [
'fields' => ['title'],
'prefixParentPageSlug' => true,
'replacements' => [
'(m/w)' => '',
'/' => '-',
],
],
],
],
            'title with replace' => [
'Product Cow',
'/parent-page/product-pig',
[
'generatorOptions' => [
'fields' => ['title'],
'prefixParentPageSlug' => true,
'replacements' => [
'/Cow/' => 'pig',
],
],
],
],
'title with slash and replace' => [
'Product/Cow',
'/parent-page/productcow',
[
'generatorOptions' => [
'fields' => ['title'],
'prefixParentPageSlug' => true,
'replacements' => [
'/\//' => '',
],
],
],
],
'title with slash and replace #2' => [
'Some Job in city1/city2 (m/w)',
'/parent-page/some-job-in-city1-city2',
[
'generatorOptions' => [
'fields' => ['title'],
'prefixParentPageSlug' => true,
'replacements' => [
'/\(m\/w\)/' => '',
'/\//' => '-',
],
],
],
],
'title with slash and replace #3' => [
'Some Job in city1/city2 (m/w)',
'/parent-page/some-job-in-city1-city2',
[
'generatorOptions' => [
'fields' => ['title'],
'prefixParentPageSlug' => true,
'replacements' => [
'/\(.*\)/' => '',
'/\//' => '-',
],
],
],
],

Reviews 3

History 410

  1. André Buchmann changed description 02/03/25, 21:02

  2. Gerrit Code Review pushed patch set 1 for main to change 88005 status New Under Review 02/03/25, 21:41

  3. Gerrit Code Review pushed patch set 2 for main to change 88005 02/04/25, 09:53

  4. Gerrit Code Review pushed patch set 3 for main to change 88005 02/04/25, 09:59

  5. Gerrit Code Review pushed patch set 4 for main to change 88005 02/04/25, 10:11

  6. André Buchmann changed tracker Task Feature TYPO3 Version 14 02/04/25, 10:15

  7. Gerrit Code Review pushed patch set 5 for main to change 88005 03/27/25, 16:56

  8. Gerrit Code Review pushed patch set 6 for main to change 88005 03/27/25, 16:57

  9. André Buchmann commented and changed status Under Review Resolved done 0 % 100 % 03/27/25, 17:15

    Applied in changeset commit:15678f60dde91b2ca69c4a8e82828ff98ef92b1b.

  10. Benni Mack changed status Resolved Closed 12/02/25, 14:26