-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHasTranslatedSlugAttributeTrait.php
115 lines (99 loc) · 3.71 KB
/
HasTranslatedSlugAttributeTrait.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
<?php
namespace Statikbe\FilamentFlexibleContentBlocks\Models\Concerns;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Relations\Relation;
use Spatie\Sluggable\HasTranslatableSlug;
use Spatie\Sluggable\SlugOptions;
use Statikbe\FilamentFlexibleContentBlocks\Events\SlugChanged;
use Statikbe\FilamentFlexibleContentBlocks\FilamentFlexibleContentBlocks;
trait HasTranslatedSlugAttributeTrait
{
use HasTranslatableSlug;
use HasTranslatedAttributesTrait;
public function initializeHasTranslatedSlugAttributeTrait(): void
{
$this->mergeTranslatable(['slug']);
}
public function addParentSlug()
{
$this->slugOptions = $this->getSlugOptions();
$this->addSlug();
}
protected static function bootHasTranslatedSlugAttributeTrait(): void
{
//dispatch event when slug changes for published models:
static::updating(function (self $record) {
$newSlugs = $record->getTranslations('slug');
$existingSlugs = $record->getOriginal('slug');
$changedSlugs = [];
foreach ($existingSlugs as $locale => $existingSlug) {
if (! isset($newSlugs[$locale])) {
$changedSlugs[] = [
'locale' => $locale,
'oldSlug' => $existingSlug,
'newSlug' => null,
];
} elseif ($newSlugs[$locale] !== $existingSlug) {
$changedSlugs[] = [
'locale' => $locale,
'oldSlug' => $existingSlug,
'newSlug' => $newSlugs[$locale],
];
}
}
/* Update slugs. If slug was empty before */
$oldTranslations = $record->getTranslations('slug');
$record->addParentSlug();
$newTranslations = $record->getTranslations('slug');
$record->setTranslations('slug', array_merge($newTranslations, $oldTranslations));
if (! empty($changedSlugs)) {
$published = true;
if (method_exists($record, 'isPublishedForDates')) {
$published = $record->isPublishedForDates($record->getOriginal('publishing_begins_at'), $record->getOriginal('publishing_ends_at'));
}
//dispatch event:
SlugChanged::dispatch($record, $changedSlugs, $published);
}
});
}
/**
* Get the options for generating the slug.
*/
public function getSlugOptions(): SlugOptions
{
return SlugOptions::create()
->generateSlugsFrom('title')
->saveSlugsTo('slug')
->doNotGenerateSlugsOnUpdate();
}
/**
* Get the route key for the model.
*
* @return string
*/
public function getRouteKeyName()
{
return 'slug';
}
public function getLocalizedRouteKey($locale)
{
return $this->getTranslation('slug', $locale);
}
/**
* This method is overwritten to make filament resolve the model with a translated slug key.
* {@inheritDoc}
*/
public function resolveRouteBindingQuery($query, $value, $field = null): Builder|Relation
{
$field = $field ?? $this->getRouteKeyName();
if (! $this->isTranslatableAttribute($field)) {
return parent::resolveRouteBindingQuery($query, $value, $field);
}
return $query->where(function (Builder $query) use ($field, $value) {
foreach (array_values(FilamentFlexibleContentBlocks::getLocales()) as $locale) {
$query->orWhere("{$field}->{$locale}", $value);
}
return $query;
});
}
}