0

So I have my custom entity type, created it by following official tutorial:

https://docs.sulu.io/en/2.2/book/extend-admin.html

However entity I got is not translatable like i.e. standard pages or articles. I also didn't any info on how to make it translatable. Expected behavior is just to work as those standard types. How to achieve that?

MilanG
  • 6,994
  • 2
  • 35
  • 64

1 Answers1

2

There are basically three things to do:

  1. You have to add a new Translation entity for your custom entity. So if you have an Event entity, you need an additional EventTranslation entity. See https://github.com/sulu/sulu-workshop/tree/master/src/Entity

  2. You need to tell Sulu, that your custom entity is translatable by adding the available locales to the view in your AppAdmin class, see https://github.com/sulu/sulu-workshop/blob/master/src/Admin/EventAdmin.php#L74

  3. You need to adjust your custom entity's admin controller (it will receive a locale request parameter now) to persist the localized properties to the CustomEntityTranslation instead of the CustomEntity iself, see https://github.com/sulu/sulu-workshop/blob/master/src/Controller/Admin/EventController.php

So as conclusion, Sulu is only responsible for showing the locale switcher in the upper right corner and appending the current selected locale as locale parameter to your api calls. Everything else is completely up to you, you have to implement that like in a normal symfony application

Luca Rath-Heel
  • 210
  • 1
  • 8
  • Thank you @Luca. I followed that tutorial, but I have one problem with controller class. I'm getting error: Cannot autowire service "App\Common\DoctrineListRepresentationFactory": argument "$listBuilderFactory" of method "__construct()" references class "Sulu\Component\Rest\ListBuilder\Doctrine\DoctrineListBuilderFactory" but no such service exists. Try changing the type-hint to "Sulu\Component\Rest\ListBuilder\Doctrine\DoctrineListBuilderFactoryInterface" instead. Any idea? – MilanG May 31 '21 at 10:23
  • Changing constructor parameter type from DoctrineListBuilderFactory to DoctrineListBuilderFactoryInterface really solved the error. So I have no errors now, but saving in different languages still not working. – MilanG Jun 01 '21 at 06:41
  • @MilanG Like I said, you have to implement the functionality to save your entity in different locales yourself. This is not the responsibility of sulu. If you want me to help you with that, please provide some more information, why saving different languages is not working for you. – Luca Rath-Heel Jun 01 '21 at 12:01
  • It's working now. Beside those 3 items you listed I had also to: 4. adjust the entity it self (https://github.com/sulu/sulu-workshop/blob/master/src/Entity/Event.php) 5. adjust XML file that defines the entity (https://github.com/sulu/sulu-workshop/blob/master/config/lists/events.xml) probably some other things I can't remember any more. Hope this will save someone's else time... – MilanG Jun 01 '21 at 12:59