Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

retrieve slug from slugs table if the $slugAttribute field was not changed #2688

Open
wants to merge 1 commit into
base: 3.x
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 23 additions & 4 deletions src/Models/Behaviors/HasSlug.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,10 @@ public function restoreSlugs(): void
*/
public function handleSlugsOnSave(): void
{
$this->disableLocaleSlugs();

$slugParams = $this->twillSlugData !== [] ? $this->twillSlugData : $this->getSlugParams();

$this->disableLocaleSlugs();

foreach ($slugParams as $params) {
if (in_array($params['locale'], config('twill.slug_utf8_languages', []))) {
$params['slug'] = $this->getUtf8Slug($params['slug']);
Expand Down Expand Up @@ -358,9 +358,13 @@ public function getSlugParams(?string $locale = null): ?array
throw new \Exception("You must define the field {$slugAttribute} in your model");
}

$slug = $this->getSlugValue($slugAttribute, $translation->locale)
?? $translation->$slugAttribute
?? $this->$slugAttribute;

$slugParam = [
'active' => $translation->active ?? true,
'slug' => $translation->$slugAttribute ?? $this->$slugAttribute,
'slug' => $slug,
'locale' => $translation->locale,
] + $slugDependenciesAttributes;

Expand Down Expand Up @@ -400,7 +404,7 @@ public function getSingleSlugParams(?string $locale = null): ?array

$slugParam = [
'active' => 1,
'slug' => $this->$slugAttribute,
'slug' => $this->getSlugValue($slugAttribute, $appLocale) ?? $this->$slugAttribute,
'locale' => $appLocale,
] + $slugDependenciesAttributes;

Expand All @@ -415,6 +419,21 @@ public function getSingleSlugParams(?string $locale = null): ?array
return $locale === null ? $slugParams : null;
}

/**
* Returns changed value of slugAttribute field or previous slug value
*
* @param $slugAttribute
* @param $locale
* @return mixed|null
*/
private function getSlugValue($slugAttribute, $locale): mixed
{
return $this->wasChanged($slugAttribute)
Copy link
Contributor

@Tofandel Tofandel Nov 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This fix doesn't work for slugs from translations, because then translations are never detected as changed and it will return the slug before change

? $this->$slugAttribute
: $this->slugs()->where('locale', $locale)
->where('active', true)->value('slug');
}

/**
* Returns the database table name for this model's slugs.
*/
Expand Down
Loading