Missing docs for translated nested modules

i had problems with translated nested slugs: even if they work as intended the child will always take the parent component in the default language in the slug. After a lot of fiddling and reverse enginnering i came with a solution: https://twillcms.com/docs/modules/nested-modules.html#content-working-with-self-nested-items Original code:
class PageController extends NestedModuleController
{
//...

protected function form(?int $id, TwillModelContract $item = null): array
{
$item = $this->repository->getById($id, $this->formWith, $this->formWithCount);

$this->permalinkBase = $item->ancestorsSlug;

return parent::form($id, $item);
}
}
class PageController extends NestedModuleController
{
//...

protected function form(?int $id, TwillModelContract $item = null): array
{
$item = $this->repository->getById($id, $this->formWith, $this->formWithCount);

$this->permalinkBase = $item->ancestorsSlug;

return parent::form($id, $item);
}
}
This code will only return the parent slug in only one language so it does NOT work as intended; to fix it we need to implement the getLocalizedPermalinkBase() function that does not have an item. To fix it i came with this solution:
class PageController extends NestedModuleController
{
//...
/// we will store localized slugs here:
protected array $localizedPermalinkBase = [];
protected function form(?int $id, ?TwillModelContract $item = null): array
{
$item = $this->repository->getById($id, $this->formWith, $this->formWithCount);

///default locale ancestor slug
$this->permalinkBase = $item->ancestorsSlug;
///multiple locale ancestor slugs
$locales = config('translatable.locales');
$this->localizedPermalinkBase = [];
foreach (config('translatable.locales') as $locale) {
$this->localizedPermalinkBase[$locale] = $item->getAncestorsSlug($locale);
}

return parent::form($id, $item);
}
protected function getLocalizedPermalinkBase(): array
{
/// this function is called on parent::form so we should be safe
return $this->localizedPermalinkBase;
}
}
class PageController extends NestedModuleController
{
//...
/// we will store localized slugs here:
protected array $localizedPermalinkBase = [];
protected function form(?int $id, ?TwillModelContract $item = null): array
{
$item = $this->repository->getById($id, $this->formWith, $this->formWithCount);

///default locale ancestor slug
$this->permalinkBase = $item->ancestorsSlug;
///multiple locale ancestor slugs
$locales = config('translatable.locales');
$this->localizedPermalinkBase = [];
foreach (config('translatable.locales') as $locale) {
$this->localizedPermalinkBase[$locale] = $item->getAncestorsSlug($locale);
}

return parent::form($id, $item);
}
protected function getLocalizedPermalinkBase(): array
{
/// this function is called on parent::form so we should be safe
return $this->localizedPermalinkBase;
}
}
If you have a more elegant solution or wnat to update the documentation please do so.
Twill
1 Reply
Francesco Pellerone
Furthermore if you translated the path you should also consider adding the permalinkBase
class PageController extends NestedModuleController
{
//...

protected function form(?int $id, ?TwillModelContract $item = null): array
{
$item = $this->repository->getById($id, $this->formWith, $this->formWithCount);

///default locale ancestor slug
$this->permalinkBase = $item->ancestorsSlug;
///multiple locale ancestor slugs
$locales = config('translatable.locales');
$this->localizedPermalinkBase = [];
foreach (config('translatable.locales') as $locale) {
$baseFolder = \Lang::get('routes.articles', [], $locale);///or whatever your translation is called
$slug = $item->getAncestorsSlug($locale);

$this->localizedPermalinkBase[$locale] = $baseFolder . ($slug != '' ? '/' : '') . $slug;
}

return parent::form($id, $item);
}

)
class PageController extends NestedModuleController
{
//...

protected function form(?int $id, ?TwillModelContract $item = null): array
{
$item = $this->repository->getById($id, $this->formWith, $this->formWithCount);

///default locale ancestor slug
$this->permalinkBase = $item->ancestorsSlug;
///multiple locale ancestor slugs
$locales = config('translatable.locales');
$this->localizedPermalinkBase = [];
foreach (config('translatable.locales') as $locale) {
$baseFolder = \Lang::get('routes.articles', [], $locale);///or whatever your translation is called
$slug = $item->getAncestorsSlug($locale);

$this->localizedPermalinkBase[$locale] = $baseFolder . ($slug != '' ? '/' : '') . $slug;
}

return parent::form($id, $item);
}

)
Note: using setPermalinkBase on setupController was ignored and overridden
Want results from more Discord servers?
Add your server