generated from spatie/package-skeleton-laravel
-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathHasHeroImageAttributesTrait.php
102 lines (86 loc) · 4.24 KB
/
HasHeroImageAttributesTrait.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
<?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\FilamentFlexibleBlocksConfig;
use Statikbe\FilamentFlexibleContentBlocks\Models\Contracts\HasHeroImageAttributes;
use Statikbe\FilamentFlexibleContentBlocks\Models\Enums\ImageFormat;
/**
* @mixin HasHeroImageAttributes
*/
trait HasHeroImageAttributesTrait
{
use HasMediaAttributesTrait;
use InteractsWithMedia;
public function initializeHasHeroImageAttributesTrait(): void
{
$this->registerHeroImageMediaCollectionAndConversion();
$this->mergeFillable(['hero_image_title', 'hero_image_copyright']);
}
public function heroImage(): MorphMany
{
return $this->media()->where('collection_name', $this->getHeroImageCollection());
}
protected function registerHeroImageMediaCollectionAndConversion()
{
$this->addMediaCollection($this->getHeroImageCollection())
->registerMediaConversions(function (?Media $media) {
$conversion = $this->addMediaConversion($this->getHeroImageConversionName())
->withResponsiveImages()
->fit(Fit::Crop, 1200, 630)
->format(ImageFormat::WEBP->value);
FilamentFlexibleBlocksConfig::mergeConfiguredModelImageConversion(static::class, $this->getHeroImageCollection(), $this->getHeroImageConversionName(), $conversion);
FilamentFlexibleBlocksConfig::addExtraModelImageConversions(static::class, $this->getHeroImageCollection(), $this);
//for filament upload field
$this->addFilamentThumbnailMediaConversion();
//add extra conversion for overview image format, when the hero is used as fallback for the overview image:
if (method_exists($this, 'overviewImage')) {
$overviewConversion = $this->addMediaConversion($this->getOverviewImageConversionName())
->withResponsiveImages()
->fit(Fit::Crop, 600, 600)
->format(ImageFormat::WEBP->value);
FilamentFlexibleBlocksConfig::mergeConfiguredModelImageConversion(static::class, $this->getHeroImageCollection(), $this->getOverviewImageConversionName(), $overviewConversion);
}
//add extra conversion for SEO image format, when the hero is used as fallback for the SEO image:
if (method_exists($this, 'heroImage')) {
$seoConversion = $this->addMediaConversion($this->getSEOImageConversionName())
->fit(Fit::Crop, 1200, 630)
->format(ImageFormat::WEBP->value);
FilamentFlexibleBlocksConfig::mergeConfiguredModelImageConversion(static::class, $this->getHeroImageCollection(), $this->getSEOImageConversionName(), $seoConversion);
}
});
}
public function addHeroImage(string $imagePath): void
{
$this->addMedia($imagePath)
->toMediaCollection($this->getHeroImageCollection());
}
public function getHeroImageConversionName(): string
{
return 'hero_image';
}
public function getHeroImageCollection(): string
{
return 'hero_image';
}
public function getHeroImageUrl(?string $conversion = null): ?string
{
$media = $this->getFallbackImageMedia($this->heroImage->first(), $this->getHeroImageCollection());
return $media?->getUrl($conversion ?? $this->getHeroImageConversionName());
}
public function getHeroImageMedia(?string $conversion = null, array $attributes = []): ?HtmlableMedia
{
return $this->getImageHtml(
$this->getFallbackImageMedia($this->heroImage()->first(), $this->getHeroImageCollection()),
$conversion ?? $this->getHeroImageConversionName(),
$this->hero_image_title,
$attributes);
}
public function hasHeroImage(): bool
{
return $this->hasMedia($this->getHeroImageCollection());
}
}