英文:
How to create a dynamic string route enhancer in TYPO3 CMS 9.5 LTS?
问题
我已经为我的扩展路由增强器创建了一个简单的方面,如下所示:
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}'
问题是,如果有人有一个非常复杂的名字,会破坏预期的URL结构(例如,其中包含&或/)。 为此,扩展news 使用了path_segment 属性而不是多个字段。
为此,我已经扩展了我的ext_tables.sql 文件,添加了该属性。但是我如何强制TCA自动填充它以带有“lastname-prename-uid”的清理结构?我不理解news 扩展的方法。
英文:
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.
答案1
得分: 0
Answer: 不要!根据文档和一些开发者的建议,你不应该在持久化模式映射器中使用自由文本字段。相反,你可以在 TCA 中使用 slug。详细信息请查看文档:https://docs.typo3.org/m/typo3/reference-tca/master/en-us/ColumnsConfig/Type/Slug.html
在你的模型的 TCA 中添加类似以下内容:
'urlslug' => [
    'exclude' => true,
    'label' => 'urlslug',
    'config' => [
        'type' => 'slug',
        'generatorOptions' => [
            'fields' => ['lastname', 'prename', 'uid'],
            'fieldSeparator' => '-',
            'prefixParentPageSlug' => true
        ],
        'fallbackCharacter' => '-',
        'eval' => 'uniqueInSite',
        'default' => ''
    ]
]
请记得将 urlslug 添加到你的模型中,同时也要在你的扩展的 ext_tables.sql 中添加相应的字段。另外,slug 只会在新对象上生成,只有通过 TCA(后台)创建的对象才会生成 slug。
英文:
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).
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论