Skip to content

Commit 3192f76

Browse files
committed
Fixed translated slugs not generating if slug contains object of null values
1 parent 4ebb1e5 commit 3192f76

File tree

1 file changed

+28
-48
lines changed

1 file changed

+28
-48
lines changed

src/HasSlug.php

Lines changed: 28 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -16,74 +16,54 @@ public static function bootHasSlug()
1616
{
1717
static::saving(function($model) {
1818
$attribute = $model->getSlugStorageAttribute();
19-
20-
if(!is_null($model->attributes[$attribute])) return;
21-
22-
$model->attributes[$attribute] = $model->generateSlug();
19+
20+
if($model->attributeIsTranslatable($attribute)) {
21+
$model->translateSlugs($attribute);
22+
} else {
23+
if(!is_null($model->attributes[$attribute])) return;
24+
25+
$model->attributes[$attribute] = str_slug($model->$sluggable);
26+
}
2327
});
2428
}
2529

2630
/**
27-
* Get the attribute name used to generate the slug from
31+
* Generate translated slugs. Keeps any existing translated slugs.
2832
*
29-
* @return string
33+
* @param string $attribute
3034
*/
31-
public function getSluggable()
35+
public function translateSlugs($attribute)
3236
{
33-
return $this->sluggable;
34-
}
37+
$sluggable = $this->getSluggable();
38+
$value = json_decode($this->attributes[$attribute], true);
3539

36-
/**
37-
* Get the attribute name used to store the slug into
38-
*
39-
* @return string
40-
*/
41-
public function getSlugStorageAttribute()
42-
{
43-
return $this->slugStorageAttribute ?? 'slug';
44-
}
40+
foreach($this->getTranslatedLocales($this->getSluggable()) as $locale) {
41+
if(!isset($value[$locale]) || is_null($value[$locale])) {
42+
$value[$locale] = str_slug($this->getTranslation($sluggable, $locale));
43+
}
44+
}
4545

46-
/**
47-
* Generate the slug.
48-
*
49-
* @return false|string
50-
*/
51-
protected function generateSlug()
52-
{
53-
return $this->slugify($this->getSluggable());
46+
$this->attributes[$attribute] = json_encode($value);
5447
}
5548

5649
/**
57-
* Handle the slug generation for translatable and
58-
* non-translatable attributes.
50+
* Get the attribute name used to generate the slug from
5951
*
60-
* @param string $attribute
61-
* @return false|string
52+
* @return string
6253
*/
63-
protected function slugify($attribute)
54+
public function getSluggable()
6455
{
65-
if($this->attributeIsTranslatable($attribute)) {
66-
return json_encode($this->translatedSlugs($attribute));
67-
}
68-
69-
return str_slug($this->$attribute);
56+
return $this->sluggable;
7057
}
7158

7259
/**
73-
* Handle the generation of translated slugs
60+
* Get the attribute name used to store the slug into
7461
*
75-
* @param string $attribute
76-
* @return array
62+
* @return string
7763
*/
78-
protected function translatedSlugs($attribute)
64+
public function getSlugStorageAttribute()
7965
{
80-
$slugs = [];
81-
82-
foreach($this->getTranslatedLocales($attribute) as $locale) {
83-
$slugs[$locale] = str_slug($this->getTranslation($attribute, $locale));
84-
}
85-
86-
return $slugs;
66+
return $this->slugStorageAttribute ?? 'slug';
8767
}
8868

8969
/**
@@ -135,7 +115,7 @@ public function getSluggedUrlForRoute(Route $route, $locale = null, $fullUrl = t
135115
}
136116

137117
/**
138-
* Get a bound route's parameters with the
118+
* Get a bound route's parameters with the
139119
* model's slug set to the desired locale.
140120
*
141121
* @param Illuminate\Routing\Route $route

0 commit comments

Comments
 (0)