Issue Task Under Review Orphaned

#95722Why does TYPO3 have a slug-helper?

Rozbeh Chiryai Sharahi 10/21/21, 13:59 updated 04/17/24, 22:39

Description

Hello Community,

this is just to have a general understanding of a pattern on TYPO3 code, which I do not really understand.

Since TYPO3 9 we have inhouse pretty-urls. For this, as usual these days, we have fortunately slug fields on the entities. Thank you for all this.

TYPO3 is a very mature system and it is comprehensible that we have some legacy codes here and there that look weird from today’s perspective.

Now looking at the slug-helper I kind of get lost, why these kind of code-styles are still introduced on TYPO3. The slug-helper cannot be that old and at the same time TYPO3 is now enforcing dependency injection with non-public services. Therefore accessing containers and GeneralUtility::makeInstance is advised not to be done.

Well with a Helper like this it will be quite hard to obey these conventions:

$slugHelper = GeneralUtility::makeInstance(
    SlugHelper::class,
    $tableName,
    $slugFieldName,
    $fieldConfig
);

$slugHelper->generate($hereSomeArray, int $pid);
...

This way constructor injection is very hard up to not possible imho. Also it is not very intuitive to use. Why do we introduce a slug-helper in this pattern?

Imo the right way to do this would be for instance something like this:

$slugService = new SlugService();
$slugService->getGenerator($table, $fieldName)->generateByUid(int $uid)
// or
$slugService->generate($table, $fieldname, $uid)
// or 
$slugService->generate(SlugGenerateConfigurationObject)
// or
...

This is just one of many possible patterns, nevertheless this way we could

- use enforced constructor injection
- dismiss usage of general utitlity
- write tests easily by simple injection instead of registering instances on containers, …
- understand usage simply by auto-complete

I have that feeling, that there should be some code patterns which are enforced on TYPO3 Core Code. I have this feeling, that in 5 years there will be a strong dependency on this slug-helper and there will be soon a lot of work to deprecate them.

Nevertheless I don’t see exactly what kind of recommended common pattern the current implementation is following.

Please correct me with literature if I am wrong and this pattern has some advantages.

Thank you for all your great work.

Kind regards
Rozbeh Chiryai Sharahi

Reviews 1

History 362

Show 337 earlier messages
  1. Oliver Hader commented <span data-processor="relativetime" data-value="2021-10-21T14:15:45+00:00" > 10/21/21, 14:15 </span>

    Rozbeh Chiryai Sharahi wrote:
    > Nevertheless I don’t see exactly what kind of recommended common pattern the current implementation is following.

    SlugHelper does not follow any pattern by the book. And (spoiler alert), that’s unfortunately not the only class in TYPO3 that mixes multiple patterns and approaches in one component – e.g. have a look into DataHandler.

    So, please go ahead with implementing current SlugHelper as stateless SlugService and I’ll happily look into it and review it.

    Personally I’d prefer $slugService->generate(SlugGenerateConfiguration) as it seems to be as closed as possible, but still open and flexible to future changes in the SlugGenerateConfiguration DTO, e.g. adding additional properties.

  2. Benni Mack commented and changed status New Closed <span data-processor="relativetime" data-value="2021-11-03T20:21:20+00:00" > 11/03/21, 20:21 </span>

    Hey Rozbeh Chiryai Sharahi,

    this is all my fault, I should not have created the feature in the first place.

    I can come up with excuses why I build it this in 2019 way – simply put I took the liberty of making it work “somehow” instead of coming up with a pattern due to time constraints. But now I know better and would build it differently – using all the concepts by the book. Now we have to stick with this code until someone will come up with a better alternative.

    If you find the time, I’m more than happy if you can improve the concepts in this area.

    I hope this answers your initial question. If you have any further questions, feel free to reach out to me directly at benni@typo3.org

    All the best,
    Benni.

  3. Gerrit Code Review pushed patch set 1 for main to change 74781 status Closed Under Review <span data-processor="relativetime" data-value="2022-05-28T14:17:20+00:00" > 05/28/22, 14:17 </span>

  4. Rozbeh Chiryai Sharahi commented and changed priority Should have Could have <span data-processor="relativetime" data-value="2022-06-25T11:13:37+00:00" > 06/25/22, 11:13 </span>

    Hello Benni Mack, Hello Oliver Hader,

    Thank you for your replies to the topic. Late … but … I took the time to do my contribution.

    Of course I learned that it was much much more work than expected, and looking at my initial message from today’s perspective … I was arrogant, not being aware of the amount of details considered behind a simple slug builder :D. I don’t want to imagine doing this with time constraints.

    However, i pushed a possible approach and maybe it or some parts of it are useful.

    Kind Regards
    Rozbeh Chiryai Sharahi