Search code examples
routesextbasetypo3-9.xtypo3-tca

How to create a dynamic string route enhancer in TYPO3 CMS 9.5 LTS?


I've created a simple aspect for my extension route enhancer like so:

routeEnhancers:
  Trainee:
    type: Extbase
    extension: Dsinstitution
    plugin: Dslisttrainees
    routes:
      - routePath: '/trainee/{trainee-identifier}'
        _controller: 'Trainee::show'
        _arguments:
          trainee-identifier: trainee
    defaultController: 'Trainee::list'
    aspects:
      trainee-identifier:
        type: PersistedPatternMapper
        tableName: 'tx_dsinstitution_domain_model_trainee'
        routeFieldPattern: '^(?<lastname>.+)-(?<prename>.+)-(?<uid>\d+)$'
        routeFieldResult: '{lastname}-{prename}-{uid}'

The problem is if there is someone with a very cryptic name which would destroy the expected url structure (e.g. with & or / in it). For that the extension news uses a path_segment attribute instead of multiple fields.

For that I've extended my ext_tables.sql with that attribute. But how can I force the TCA to auto fill it with the sanitized structure of "lastname-prename-uid"? I don't understand the news extension way.


Solution

  • Answer: Don't!

    In the documentation and according to recommendations from several developers you shouldn't use free text fields in the Persisted Pattern Mapper. Instead you use a slug for that in your TCA.

    For more have a look in the documentation: https://docs.typo3.org/m/typo3/reference-tca/master/en-us/ColumnsConfig/Type/Slug.html

    Adding to your TCA of your model something like:

    'urlslug' => [
        'exclude' => true,
        'label' => 'urlslug',
        'config' => [
            'type' => 'slug',
            'generatorOptions' => [
                'fields' => ['lastname', 'prename', 'uid'],
                'fieldSeparator' => '-',
                'prefixParentPageSlug' => true
            ],
            'fallbackCharacter' => '-',
            'eval' => 'uniqueInSite',
            'default' => ''
        ]
    ]
    

    Remember to add urlslug to your Model and to the ext_tables.sql of your extension as well. Also slugs will only be generated on new objects, only created with TCA (backend).