Skip to content

Commit

Permalink
Support for passing an array of classes as extra attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
pascalbaljet committed Feb 14, 2024
1 parent 5f18aa2 commit 2d2ab08
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/MediaCollections/HtmlableMedia.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Spatie\MediaLibrary\MediaCollections;

use Illuminate\Contracts\Support\Htmlable;
use Illuminate\Support\Arr;
use Spatie\MediaLibrary\Conversions\ConversionCollection;
use Spatie\MediaLibrary\Conversions\ImageGenerators\Image;
use Spatie\MediaLibrary\Conversions\ImageGenerators\ImageGeneratorFactory;
Expand All @@ -23,6 +24,10 @@ public function __construct(

public function attributes(array $attributes): self
{
if (is_array($attributes['class'] ?? null)) {
$attributes['class'] = Arr::toCssClasses($attributes['class']);
}

$this->extraAttributes = $attributes;

return $this;
Expand Down
11 changes: 11 additions & 0 deletions tests/Feature/Media/ToHtmlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,17 @@
);
});

it('can render an array of classes as extra attributes', function () {
$this->assertEquals(
'<img class="rounded border" src="/media/1/conversions/test-thumb.jpg" alt="test">',
Media::first()->img('thumb', ['class' => [
'rounded',
'border' => true,
'border-black' => false,
]]),
);
});

test('a media instance is htmlable', function () {
$media = Media::first();

Expand Down

0 comments on commit 2d2ab08

Please sign in to comment.