Merge branch '2.x' into 3.x

This commit is contained in:
Dan Harrin
2023-05-21 14:51:56 +02:00
3 changed files with 29 additions and 2 deletions
@@ -67,7 +67,9 @@ class SpatieMediaLibraryImageColumn extends ImageColumn
return $state;
}
return $record->getFirstMediaUrl($this->getCollection(), $this->getConversion());
$firstMediaUrl = $record->getFirstMediaUrl($this->getCollection(), $this->getConversion());
return filled($firstMediaUrl) ? $firstMediaUrl : $this->getDefaultImageUrl();
}
public function applyEagerLoading(Builder | Relation $query): Builder | Relation
@@ -75,6 +75,17 @@ ImageColumn::make('author.avatar')
->size(40)
```
## Adding a default image URL
You can display a placeholder image if one doesn't exist yet, by passing a URL to the `defaultImageUrl()` method:
```php
use Filament\Tables\Columns\ImageColumn;
ImageColumn::make('avatar')
->defaultImageUrl(url('/images/placeholder.png'))
```
## Custom attributes
You may customize the extra HTML attributes of the image using `extraImgAttributes()`:
+15 -1
View File
@@ -34,6 +34,8 @@ class ImageColumn extends Column
*/
protected array $extraImgAttributes = [];
protected string | Closure | null $defaultImageUrl = null;
protected function setUp(): void
{
parent::setUp();
@@ -124,12 +126,19 @@ class ImageColumn extends Column
return $height;
}
public function defaultImageUrl(string | Closure | null $url): static
{
$this->defaultImageUrl = $url;
return $this;
}
public function getImagePath(): ?string
{
$state = $this->getState();
if (! $state) {
return null;
return $this->getDefaultImageUrl();
}
if (filter_var($state, FILTER_VALIDATE_URL) !== false) {
@@ -160,6 +169,11 @@ class ImageColumn extends Column
return $storage->url($state);
}
public function getDefaultImageUrl(): ?string
{
return $this->evaluate($this->defaultImageUrl);
}
public function getVisibility(): string
{