generated from spatie/package-skeleton-laravel
-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathHasOverviewAttributesTrait.php
More file actions
122 lines (105 loc) · 4.52 KB
/
HasOverviewAttributesTrait.php
File metadata and controls
122 lines (105 loc) · 4.52 KB
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
116
117
118
119
120
121
122
<?php
namespace Statikbe\FilamentFlexibleContentBlocks\Models\Concerns;
use Illuminate\Database\Eloquent\Relations\MorphMany;
use Spatie\Image\Enums\Fit;
use Spatie\MediaLibrary\InteractsWithMedia;
use Spatie\MediaLibrary\MediaCollections\HtmlableMedia;
use Spatie\MediaLibrary\MediaCollections\Models\Media;
use Statikbe\FilamentFlexibleContentBlocks\Facades\FilamentFlexibleContentBlocks;
use Statikbe\FilamentFlexibleContentBlocks\FilamentFlexibleBlocksConfig;
use Statikbe\FilamentFlexibleContentBlocks\Models\Enums\ImageFormat;
/**
* @property string|null $overview_title
* @property string|null $overview_description
*/
trait HasOverviewAttributesTrait
{
use HasMediaAttributesTrait;
use InteractsWithMedia;
public function initializeHasOverviewAttributesTrait(): void
{
$this->mergeFillable(['overview_title', 'overview_description']);
$this->registerOverviewImageMediaCollectionAndConversion();
}
public function overviewImage(): MorphMany
{
return $this->media()->where('collection_name', $this->getOverviewImageCollection());
}
public function getOverviewTitle(): ?string
{
if (! $this->overview_title && $this->getTitle()) {
return $this->getTitle();
}
if (! $this->overview_title && $this->getSEOTitle()) {
return $this->getSEOTitle();
}
return FilamentFlexibleContentBlocks::replaceParameters($this->overview_title);
}
public function getOverviewDescription(): ?string
{
if (! $this->overview_description && $this->getIntro()) {
return $this->getIntro();
}
if (! $this->overview_description && $this->getSEODescription()) {
return $this->getSEODescription();
}
return FilamentFlexibleContentBlocks::replaceParameters($this->overview_description);
}
protected function registerOverviewImageMediaCollectionAndConversion()
{
$this->addMediaCollection($this->getOverviewImageCollection())
->registerMediaConversions(function (?Media $media) {
$conversion = $this->addMediaConversion($this->getOverviewImageConversionName())
->withResponsiveImages()
->fit(Fit::Crop, 600, 600)
->format(ImageFormat::WEBP->value);
FilamentFlexibleBlocksConfig::mergeConfiguredModelImageConversion(static::class, $this->getOverviewImageCollection(), $this->getOverviewImageConversionName(), $conversion);
FilamentFlexibleBlocksConfig::addExtraModelImageConversions(static::class, $this->getOverviewImageCollection(), $this);
// for filament upload field
$this->addFilamentThumbnailMediaConversion();
});
}
public function addOverviewImage(string $imagePath): void
{
$this->addMedia($imagePath)
->toMediaCollection($this->getOverviewImageCollection());
}
public function getOverviewImageConversionName(): string
{
return 'overview_image';
}
public function getOverviewImageCollection(): string
{
return 'overview_image';
}
public function getOverviewImageUrl(?string $conversion = null): ?string
{
$media = $this->getFallbackImageMedia($this->overviewImage->first(), $this->getOverviewImageCollection());
if ($media) {
return $media->getUrl($conversion ?? $this->getOverviewImageConversionName());
} elseif (method_exists($this, 'heroImage')) {
return $this->getHeroImageUrl($conversion ?? $this->getOverviewImageConversionName());
} else {
return null;
}
}
public function getOverviewImageMedia(?string $conversion = null, array $attributes = []): ?HtmlableMedia
{
if ($overviewMedia = $this->getFallbackImageMedia($this->overviewImage()->first(), $this->getOverviewImageCollection())) {
return $this->getImageHtml(
$overviewMedia,
$conversion ?? $this->getOverviewImageConversionName(),
$this->getOverviewTitle(),
$attributes);
} elseif (method_exists($this, 'heroImage')
&& $heroMedia = $this->getFallbackImageMedia($this->heroImage()->first(), $this->getHeroImageCollection())) {
return $this->getImageHtml(
$heroMedia,
$conversion ?? $this->getOverviewImageConversionName(),
$this->getOverviewTitle(),
$attributes);
} else {
return null;
}
}
}