jefsev
03/27/2023, 7:16 PMpublic function forNestedSlug(
string $nestedSlug,
array $with = [],
array $withCount = [],
array $scopes = []
): ?TwillModelContract {
$targetSlug = collect(explode('/', $nestedSlug))->last();
$targetItem = $this->forSlug($targetSlug, $with, $withCount, $scopes);
if (!$targetItem || $nestedSlug !== $targetItem->nestedSlug) {
return null;
}
return $targetItem;
}
When i explode it in my resolveRouteBinding and call forSlug it does work but the slugs in the cms are all wrong:ifox
03/27/2023, 8:46 PMifox
03/27/2023, 8:46 PMjefsev
03/28/2023, 2:33 PMjefsev
03/28/2023, 2:34 PMnamespace App\Http\Controllers\Twill;
use A17\Twill\Http\Controllers\Admin\NestedModuleController as BaseModuleController;
use Mcamara\LaravelLocalization\Facades\LaravelLocalization;
use App\Models\Page;
class PageController extends BaseModuleController
{
protected $moduleName = 'pages';
protected function getLocalizedPermalinkBase(): array
{
return [
"en" => "",
"nl" => "",
];
}
protected $indexOptions = [
'reorder' => true,
];
protected $nestedItemsDepth = 6;
protected function form($id, $item = null): array
{
$item = $this->repository->getById($id, $this->formWith, $this->formWithCount);
$this->permalinkBase = $item->getAncestorsSlug();
return parent::form($id, $item);
}
public function single(Page $page)
{
return view("site.pages.show", compact("page"));
}
}
This is my controllerjefsev
03/28/2023, 2:35 PMpublic function resolveRouteBinding($slug, $field = null)
{
$targetSlug = collect(explode('/', $slug))->last();
$page = app(PageRepository::class)->forSlug($targetSlug);
abort_if(! $page, 404);
return $page;
}
// #region routekey
public function getLocalizedRouteKey($locale)
{
return $this->getNestedSlug($locale);
}
This is currently what i use, i explode it myself to make it somewhat work on the frontend.jefsev
03/28/2023, 2:37 PMjefsev
03/30/2023, 8:34 AMjefsev
03/30/2023, 1:18 PMifox
03/30/2023, 1:19 PMjefsev
03/30/2023, 1:20 PMifox
03/30/2023, 1:20 PMifox
03/30/2023, 1:21 PMpboivin
03/30/2023, 2:24 PMpboivin
03/30/2023, 2:24 PMmysite.com/second instead of mysite.com/first/second ?jefsev
03/30/2023, 2:36 PMjefsev
03/30/2023, 2:38 PMpboivin
03/30/2023, 2:42 PMform method. A quick test I just didpboivin
03/30/2023, 2:42 PMphp
protected function form($id, $item = null)
{
$data = parent::form($id, $item);
$data['baseUrl'] = $data['baseUrl'] . $data['item']->ancestorsSlug . '/';
return $data;
}pboivin
03/30/2023, 2:43 PMjefsev
03/30/2023, 2:47 PMjefsev
03/30/2023, 2:48 PMifox
03/30/2023, 2:49 PMjefsev
03/30/2023, 2:52 PMpboivin
03/30/2023, 2:52 PMjefsev
03/30/2023, 2:52 PMjefsev
03/30/2023, 2:53 PMpboivin
03/30/2023, 2:53 PMpboivin
03/30/2023, 2:53 PMphp
protected function form(?int $id, ?TwillModelContract $item = null): array
{
$data = parent::form($id, $item);
$data['baseUrl'] = $data['baseUrl'] . $data['item']->ancestorsSlug . '/';
return $data;
}jefsev
03/30/2023, 2:54 PMjefsev
03/30/2023, 2:54 PMpboivin
03/30/2023, 2:55 PMpboivin
03/30/2023, 2:55 PMjefsev
03/30/2023, 2:57 PMjefsev
03/30/2023, 2:57 PMjefsev
03/30/2023, 2:57 PMpboivin
03/30/2023, 2:57 PMjefsev
03/30/2023, 2:58 PMjefsev
03/30/2023, 2:58 PMjefsev
03/30/2023, 2:58 PMjefsev
03/30/2023, 2:59 PM// for a specific locale:
$slug = $item->getAncestorsSlug($lang);pboivin
03/30/2023, 3:02 PMprotected function form($id, $item = null)
{
$data = parent::form($id, $item);
$data['localizedPermalinkBase'] = [
"en" => $data['item']->ancestorsSlug,
"nl" => $data['item']->ancestorsSlug,
];
return $data;
}jefsev
03/30/2023, 3:03 PMjefsev
03/30/2023, 3:05 PMpboivin
03/30/2023, 3:06 PMjefsev
03/30/2023, 3:07 PMjefsev
03/30/2023, 3:08 PMjefsev
03/30/2023, 3:08 PMjefsev
03/30/2023, 3:09 PMjefsev
03/30/2023, 3:10 PMjefsev
03/30/2023, 3:10 PMjefsev
03/30/2023, 3:12 PMjefsev
03/30/2023, 3:13 PMifox
03/30/2023, 3:13 PMjefsev
03/30/2023, 3:14 PMjefsev
03/30/2023, 9:04 PMifox
03/30/2023, 9:31 PMjefsev
03/31/2023, 3:14 PMjefsev
03/31/2023, 3:15 PMifox
03/31/2023, 3:16 PMjefsev
03/31/2023, 3:21 PMarray:4 [▼ // resources/views/site/pages/show.blade.php
"oddness" => 0
"duplicates" => 0
"wrong_parent" => 0
"missing_parent" => 0
]jefsev
03/31/2023, 3:21 PMjefsev
03/31/2023, 3:22 PMifox
03/31/2023, 3:23 PMjefsev
03/31/2023, 3:25 PMpublic function resolveRouteBinding($slug, $field = null)
{
$targetSlug = collect(explode('/', $slug))->last();
$page = app(PageRepository::class)->forSlug($targetSlug);
abort_if(! $page, 404);
return $page;
}
public function getLocalizedRouteKey($locale)
{
return $this->getNestedSlug($locale);
}jefsev
03/31/2023, 3:25 PMjefsev
03/31/2023, 3:27 PMjefsev
03/31/2023, 3:31 PM@foreach (LaravelLocalization::getSupportedLocales() as $localeCode => $properties)
<a rel="alternate" hreflang="{{ $localeCode }}"
href="{{ LaravelLocalization::getLocalizedURL($localeCode, null, [], true) }}"
class="block rounded-lg px-4 py-2 text-sm text-gray-500 hover:bg-gray-50 hover:text-gray-700 @if ($localeCode == app()->getLocale()) font-bold @endif">
{{ $properties['native'] }}
</a>
@endforeachjefsev
03/31/2023, 3:33 PMjefsev
03/31/2023, 3:36 PMjefsev
03/31/2023, 3:39 PMpboivin
05/18/2023, 9:07 PMHasNesting::getAncestorsSlug() method.
3.x:
https://github.com/area17/twill/blob/9a172c5ee02b5345e6a868163a6cdf9691cc1309/src/Models/Behaviors/HasNesting.php#L38
2.x:
https://github.com/area17/twill/blob/226fdfe435ac1fdd1941979691a97194d9105952/src/Models/Behaviors/HasNesting.php#L41
Overriding the method on my model to the 2.x version seems to solve the issue in my case. I haven't tested this extensively... but I'm curious to know if you explored this further on your end?jefsev
05/18/2023, 9:11 PMpboivin
05/18/2023, 9:13 PMjefsev
05/22/2023, 8:27 PM/**
* Returns the combined slug for all ancestors of this item.
*
* @param string|null $locale
* @return string
*/
public function getAncestorsSlug($locale = null)
{
return collect($this->ancestors->sortByDesc('position') ?? [])
->map(function ($i) use ($locale) { return $i->getSlug($locale); })
->implode('/');
}
public function resolveRouteBinding($slug, $field = null)
{
$page = app(PageRepository::class)->forNestedSlug($slug);
abort_if(! $page, 404);
return $page;
}
public function getLocalizedRouteKey($locale)
{
return $this->getAncestorsSlug($locale);
}jefsev
05/22/2023, 8:27 PMjefsev
05/22/2023, 8:27 PMjefsev
05/22/2023, 8:28 PMjefsev
05/22/2023, 9:24 PM/**
* Returns the combined slug for all ancestors of this item.
*
* @param string|null $locale
* @return string
*/
public function getAncestorsSlug($locale = null)
{
return collect($this->ancestors->sortByDesc('position') ?? [])
->map(function ($i) use ($locale) { return $i->getSlug($locale); })
->implode('/');
}
/**
* Returns the combined slug for this item including all ancestors.
*
* @param string|null $locale
* @return string
*/
public function getNestedSlug($locale = null)
{
return collect([$this->getAncestorsSlug($locale), $this->getSlug($locale)])
->filter()
->implode('/');
}
public function resolveRouteBinding($slug, $field = null)
{
$page = app(PageRepository::class)->forNestedSlug($slug);
abort_if(! $page, 404);
return $page;
}
public function getLocalizedRouteKey($locale)
{
return $this->getNestedSlug($locale);
}
So i moved getAncestorsSlug from twill 2, which you shared to my model. I also moved getNestedSlug to my model as that is relying on the first one. This seems to have fixed the languas toggle for me and the 404 issue with deeper nesting.jefsev
05/22/2023, 9:25 PMjefsev
05/22/2023, 9:26 PMprotected 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);
}
Probably this part is the issue, i tried a couple things based on previous feedback in this chat but none seem to fix the issuejefsev
05/22/2023, 9:27 PMjefsev
05/22/2023, 9:27 PMpboivin
05/23/2023, 11:56 AMpboivin
05/23/2023, 11:56 AMphp
protected function form(?int $id, ?TwillModelContract $item = null): array
{
$data = parent::form($id, $item);
$data['localizedPermalinkBase'] = [
'en' => $data['item']->getAncestorsSlug('en'),
'fr' => $data['item']->getAncestorsSlug('fr'),
];
return $data;
}jefsev
05/23/2023, 8:22 PMhttps://cdn.discordapp.com/attachments/1089991279334662215/1110664106169618482/Screenshot_2023-05-23_at_22.17.38.png▾
pboivin
05/24/2023, 12:24 PMgetAncestorsSlug()?jefsev
05/24/2023, 12:29 PMpboivin
05/24/2023, 12:35 PM->sortByDesc('position') from the 2.x version and I'm not sure I fully understand it. It came as a community contribution a bit after the --handleNesting feature. I forget what the reasoning was behind this fix.jefsev
05/24/2023, 1:15 PMjefsev
05/24/2023, 1:16 PMifox
05/24/2023, 1:26 PMjefsev
05/24/2023, 1:31 PMjefsev
05/24/2023, 1:31 PMifox
05/24/2023, 1:31 PMjefsev
05/24/2023, 1:56 PM