Francesco Pellerone
Francesco Pellerone
TCTwill CMS
Created by Francesco Pellerone on 6/25/2024 in #👊support
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.
2 replies
TCTwill CMS
Created by Francesco Pellerone on 5/16/2024 in #👊support
Creating a front-end for parent-child nested module
Hello, i need help with the frontend controller for a Parent-child modules: i want to create articles in categories, i have successfully created Category and CategoryArticle classes like in the documentation but i cannot make the page routing work web.php
Route::get('/categories/{slug}', [CategoryArticleDisplayController::class, 'show'])->name('frontend.category')->where('slug', '.*');
Route::get('/categories/{slug}', [CategoryArticleDisplayController::class, 'show'])->name('frontend.category')->where('slug', '.*');
CategoryArticleDisplayController.php
<?php

namespace App\Http\Controllers;

use App\Models\Category;
use App\Models\CategoryArticle;
use App\Repositories\CategoryRepository;
use App\Repositories\CategoryArticleRepository;
use Illuminate\Http\Request;

class CategoryArticleDisplayController extends Controller
{
public function show(string $slug, CategoryRepository $categoryRepository, CategoryArticleRepository $articleRepository): View
{

$category = $categoryRepository->forSlug($slug);
if ($category) {
return $this->showCategory($category);////<<<THIS WORKS
}

$article = $articleRepository->forSlug($slug);///THIS CANNOT FIND THE ARTICLE
if ($article) {

return $this->showArticle($article);
}

abort(404);
}

protected function showCategory(Category $category): View
{
dd('category', $category);

return view('site.category', ['item' => $category]);
}


protected function showArticle(CategoryArticle $article): View
{

dd('article', $article);
return view('site.category-article', ['item' => $article]);
}
}
<?php

namespace App\Http\Controllers;

use App\Models\Category;
use App\Models\CategoryArticle;
use App\Repositories\CategoryRepository;
use App\Repositories\CategoryArticleRepository;
use Illuminate\Http\Request;

class CategoryArticleDisplayController extends Controller
{
public function show(string $slug, CategoryRepository $categoryRepository, CategoryArticleRepository $articleRepository): View
{

$category = $categoryRepository->forSlug($slug);
if ($category) {
return $this->showCategory($category);////<<<THIS WORKS
}

$article = $articleRepository->forSlug($slug);///THIS CANNOT FIND THE ARTICLE
if ($article) {

return $this->showArticle($article);
}

abort(404);
}

protected function showCategory(Category $category): View
{
dd('category', $category);

return view('site.category', ['item' => $category]);
}


protected function showArticle(CategoryArticle $article): View
{

dd('article', $article);
return view('site.category-article', ['item' => $article]);
}
}
2 replies