Skip to content

Commit

Permalink
Merge branch 'main' into kul_blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
sevbesau committed Oct 17, 2024
2 parents e7e6bb5 + 1b5f333 commit 24575a0
Show file tree
Hide file tree
Showing 19 changed files with 149 additions and 483 deletions.
28 changes: 28 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,34 @@

All notable changes to `laravel-filament-flexible-content-blocks` will be documented in this file.

## v2.1.5 - 2024-10-10

Fix copy blocks to all locales actions

## v2.1.4 - 2024-10-09

Enforce media library v11 because we use the enum Fit in the config file.

## v2.1.3 - 2024-09-30

### What's Changed

* Update VideoBlock.php by @Sindoweb in https://github.com/statikbe/laravel-filament-flexible-content-blocks/pull/46

**Full Changelog**: https://github.com/statikbe/laravel-filament-flexible-content-blocks/compare/v2.1.1...v2.1.3

## v2.1.1 - 2024-09-30

Fix small bug in hasImage function of blocks.

## v2.1.0 - 2024-09-27

This release contains a refactor of the spatie medialibrary block image field. The field no longer stores a Media UUID in the content blocks data but makes now fully use of the block ID to filter out the correct media.

**IMPORTANT:** The refactor required a small change in the Blade view of the Cards Block. In case you would have published this view, please use [the upgrade guide](https://github.com/statikbe/laravel-filament-flexible-content-blocks/blob/main/UPGRADE.md).

**Full Changelog**: https://github.com/statikbe/laravel-filament-flexible-content-blocks/compare/v2.0.3...v2.1.0

## v2.0.3 - 2024-07-10

- Make call to actions more configurable to support the new asset manager package, see https://github.com/statikbe/laravel-filament-flexible-blocks-asset-manager
Expand Down
12 changes: 12 additions & 0 deletions UPGRADE.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
# Upgrades

## v2.1.0

Since v2.1.0 the media UUID is no longer stored in the block data. This has impact on the Cards Block.
If you are using the Cards Block and have published the views, please edit your custom view and change the
`$getCardImageMedia()` to the code blow in the Blade view:

```php
<x-flexible-card :data="$card">
{!! $getCardImageMedia($card->cardId, $card->title, false, ['class' => 'w-full']) !!}
</x-flexible-card>
```

## v2.0.0

To upgrade to Laravel 11, we needed to migrate to spatie-medialibrary v11, which required an upgrade to spatie-image v3.
Expand Down
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,13 @@
"openai-php/laravel": "^0.8.1",
"spatie/laravel-package-tools": "^1.13.0",
"spatie/laravel-sluggable": "^3.4",
"spatie/laravel-translatable": "^6.3"
"spatie/laravel-translatable": "^6.3",
"spatie/laravel-medialibrary": "^11.0"
},
"require-dev": {
"laravel/pint": "^1.0",
"nunomaduro/collision": "^7.0|^8.0",
"nunomaduro/larastan": "^2.0.1",
"larastan/larastan": "^2.0.1",
"orchestra/testbench": "^8.0",
"pestphp/pest": "^2.0",
"pestphp/pest-plugin-laravel": "^2.0",
Expand Down
2 changes: 1 addition & 1 deletion resources/views/content-blocks/tailwind/cards.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
@endphp

<x-flexible-card :data="$card">
{!! $getCardImageMedia($card->imageId, $card->title, false, ['class' => 'w-full']) !!}
{!! $getCardImageMedia($card->cardId, $card->title, false, ['class' => 'w-full']) !!}
</x-flexible-card>
@endforeach
</div>
Expand Down
27 changes: 3 additions & 24 deletions src/ContentBlocks/AbstractContentBlock.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ public function __construct(HasContentBlocks&HasMedia $record, ?array $blockData
$this->blockData = $blockData;

//block id:
if (! isset($this->blockData['block_id']) || ! $this->blockData['block_id']) {
if (! isset($this->blockData[BlockIdField::FIELD]) || ! $this->blockData[BlockIdField::FIELD]) {
//initialise the ID for a new block, then never change it.
$this->blockData['block_id'] = BlockIdField::generateBlockId();
$this->blockData[BlockIdField::FIELD] = BlockIdField::generateBlockId();
}

$this->blockId = $this->blockData['block_id'];
$this->blockId = $this->blockData[BlockIdField::FIELD];
}

/**
Expand Down Expand Up @@ -159,27 +159,6 @@ public function replaceParameters(?string $content): ?string
return FilamentFlexibleContentBlocks::replaceParameters($content);
}

/**
* Sometimes the media UUID is saved as an array instead of a string. This converts array uuids to a string.
*/
protected function getMediaUuid(string|array|null $uuid): ?string
{
if (! $uuid) {
return null;
}

if (is_array($uuid)) {
if (empty($uuid)) {
return null;
}

//take the first value:
return array_values($uuid)[0];
}

return $uuid;
}

public function getBlockId(): string
{
return $this->blockId;
Expand Down
17 changes: 2 additions & 15 deletions src/ContentBlocks/CallToActionBlock.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@ class CallToActionBlock extends AbstractFilamentFlexibleContentBlock

public ?string $text;

public ?string $imageId;

public ?string $imageTitle;

public ?string $imageCopyright;
Expand All @@ -52,7 +50,6 @@ public function __construct(HasContentBlocks&HasMedia $record, ?array $blockData

$this->title = $blockData['title'] ?? null;
$this->text = $blockData['text'] ?? null;
$this->imageId = $this->getMediaUuid($blockData['image']) ?? null;
$this->imageTitle = $blockData['image_title'] ?? null;
$this->imageCopyright = $blockData['image_copyright'] ?? null;
$this->callToActions = $this->createMultipleCallToActions($blockData);
Expand Down Expand Up @@ -125,17 +122,12 @@ public static function addMediaCollectionAndConversion(HasMedia&HasMediaAttribut

public function getImageMedia(array $attributes = []): ?HtmlableMedia
{
return $this->getHtmlableMedia($this->imageId, static::CONVERSION_DEFAULT, $this->imageTitle, $attributes);
return $this->getHtmlableMedia($this->getBlockId(), static::CONVERSION_DEFAULT, $this->imageTitle, $attributes);
}

public function getImageUrl(): ?string
{
return $this->getMediaUrl($this->imageId);
}

public function hasImage(): bool
{
return isset($this->imageId) && ! is_null($this->imageId);
return $this->getMediaUrl(blockId: $this->getBlockId());
}

public function getSearchableContent(): array
Expand All @@ -148,9 +140,4 @@ public function getSearchableContent(): array

return $searchable;
}

public function getImageUuids(): array
{
return $this->imageId ? [$this->imageId] : [];
}
}
30 changes: 6 additions & 24 deletions src/ContentBlocks/CardsBlock.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
use Statikbe\FilamentFlexibleContentBlocks\Filament\Form\Fields\Blocks\BackgroundColourField;
use Statikbe\FilamentFlexibleContentBlocks\Filament\Form\Fields\Blocks\BlockSpatieMediaLibraryFileUpload;
use Statikbe\FilamentFlexibleContentBlocks\Filament\Form\Fields\Blocks\BlockStyleField;
use Statikbe\FilamentFlexibleContentBlocks\Filament\Form\Fields\Blocks\CallToActionField;
use Statikbe\FilamentFlexibleContentBlocks\Filament\Form\Fields\Blocks\CallToActionRepeater;
use Statikbe\FilamentFlexibleContentBlocks\Filament\Form\Fields\Blocks\Data\CardData;
use Statikbe\FilamentFlexibleContentBlocks\Filament\Form\Fields\Blocks\GridColumnsField;
Expand Down Expand Up @@ -120,18 +119,18 @@ public static function addMediaCollectionAndConversion(HasMedia&HasMediaAttribut
});
}

public function getCardImageMedia(?string $imageId, ?string $imageTitle, ?string $conversion = null, array $attributes = []): ?HtmlableMedia
public function getCardImageMedia(?string $cardBlockId, ?string $imageTitle, ?string $conversion = null, array $attributes = []): ?HtmlableMedia
{
if (! $imageId) {
if (! $cardBlockId) {
return null;
}

return $this->getHtmlableMedia($imageId, $this->getImageConversionType($conversion), $imageTitle, $attributes);
return $this->getHtmlableMedia($cardBlockId, $this->getImageConversionType($conversion), $imageTitle, $attributes);
}

public function getCardImageUrl(string $imageId, ?string $conversion = null): ?string
public function getCardImageUrl(string $cardBlockId, ?string $conversion = null): ?string
{
return $this->getMediaUrl(imageId: $imageId, conversion: $this->getImageConversionType($conversion));
return $this->getMediaUrl(blockId: $cardBlockId, conversion: $this->getImageConversionType($conversion));
}

/**
Expand All @@ -144,10 +143,7 @@ private function createCards(array $cardsBlockData): array
try {
$cardData[] = CardData::create(
cardBlockData: $card,
imageUrl: $card['image'] ? $this->getCardImageUrl($this->getMediaUuid($card['image'])) : null,
imageHtml: $card['image'] ? $this->getCardImageMedia($this->getMediaUuid($card['image']), $card['title']) : null,
blockStyle: $this->hasDefaultBlockStyle() ? null : $this->blockStyle,
buttonStyleClasses: CallToActionField::getButtonStyleClasses(static::class)
cardsBlock: $this
);
} catch (LinkableModelNotFoundException $ex) {
$ex->setRecord($this->record);
Expand All @@ -171,18 +167,4 @@ public function getSearchableContent(): array

return $searchable;
}

public function getImageUuids(): array
{
$uuids = [];
if ($this->blockData['cards']) {
foreach ($this->blockData['cards'] as $card) {
if ($card['image']) {
$uuids[] = $card['image'];
}
}
}

return $uuids;
}
}
54 changes: 36 additions & 18 deletions src/ContentBlocks/Concerns/HasImage.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
use Spatie\Image\Enums\Fit;
use Spatie\MediaLibrary\Conversions\Conversion;
use Spatie\MediaLibrary\HasMedia;
use Spatie\MediaLibrary\InteractsWithMedia;
use Spatie\MediaLibrary\MediaCollections\HtmlableMedia;
use Spatie\MediaLibrary\MediaCollections\Models\Collections\MediaCollection;
use Spatie\MediaLibrary\MediaCollections\Models\Media;
use Statikbe\FilamentFlexibleContentBlocks\FilamentFlexibleBlocksConfig;
use Statikbe\FilamentFlexibleContentBlocks\Models\Contracts\HasMediaAttributes;
Expand All @@ -14,28 +16,51 @@
trait HasImage
{
/**
* Returns the media with UUID $imageId
* Returns the first media with block UUID $blockId
*/
protected function getMedia(?string $imageId, ?string $collection = null): ?Media
protected function getMedia(?string $blockId, ?string $collection = null): ?Media
{
if (! $imageId) {
return $this->getAllMedia($blockId, $collection)?->first();
}

protected function getAllMedia(?string $blockId, ?string $collection = null): ?MediaCollection
{
if (! $blockId) {
$blockId = $this->getBlockId();
}

if (! $blockId) {
return null;
}

/* @var HasMedia $recordWithMedia */
/* @var HasMedia&InteractsWithMedia $recordWithMedia */
$recordWithMedia = $this->record;

return $recordWithMedia->getMedia($collection ?? static::getName(), function (Media $media) use ($imageId) {
return $media->uuid === $imageId;
})->first();
return $recordWithMedia->getMedia($collection ?? static::getName(), [
'block' => $blockId,
]);
}

public function hasImage(?string $blockId = null, ?string $collection = null): bool
{
if (! $blockId) {
$blockId = $this->getBlockId();
}

/* @var HasMedia&InteractsWithMedia $recordWithMedia */
$recordWithMedia = $this->record;

return $recordWithMedia->hasMedia($collection ?? static::getName(), [
'block' => $blockId,
]);
}

/**
* Returns an HTML view of the first image
*/
protected function getHtmlableMedia(?string $imageId, string $conversion, ?string $imageTitle, array $attributes = [], ?string $collection = null): ?HtmlableMedia
protected function getHtmlableMedia(?string $blockId, string $conversion, ?string $imageTitle, array $attributes = [], ?string $collection = null): ?HtmlableMedia
{
$media = $this->getMedia($imageId);
$media = $this->getMedia($blockId);
$html = null;

if ($media) {
Expand All @@ -58,9 +83,9 @@ protected function getHtmlableMedia(?string $imageId, string $conversion, ?strin
/**
* Returns the image url for the given UUID.
*/
protected function getMediaUrl(string $imageId, ?string $collection = null, ?string $conversion = null): ?string
protected function getMediaUrl(string $blockId, ?string $collection = null, ?string $conversion = null): ?string
{
$media = $this->getMedia($imageId, $collection);
$media = $this->getMedia($blockId, $collection);

return $media?->getFullUrl();
}
Expand All @@ -86,11 +111,4 @@ protected static function addImageConversion(HasMedia&HasMediaAttributes $record

return $conversion;
}

/**
* Return all image UUIDs of this block.
*
* @return array<string>
*/
abstract public function getImageUuids(): array;
}
18 changes: 3 additions & 15 deletions src/ContentBlocks/ImageBlock.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ class ImageBlock extends AbstractFilamentFlexibleContentBlock
use HasImageConversionType;
use HasImageWidth;

public ?string $imageId;

public ?string $imageTitle;

public ?string $imageCopyright;
Expand All @@ -44,7 +42,6 @@ public function __construct(HasContentBlocks&HasMedia $record, ?array $blockData
{
parent::__construct($record, $blockData);

$this->imageId = $this->getMediaUuid($blockData['image']) ?? null;
$this->imageTitle = $blockData['image_title'] ?? null;
$this->imageCopyright = $blockData['image_copyright'] ?? null;
$this->imagePosition = $blockData['image_position'] ?? null;
Expand Down Expand Up @@ -115,30 +112,21 @@ public static function getImageConversionTypeDefault(): string

public function getImageMedia(?string $conversion = null, array $attributes = []): ?HtmlableMedia
{
return $this->getHtmlableMedia($this->imageId, $this->getImageConversionType($conversion), $this->imageTitle, $attributes);
return $this->getHtmlableMedia($this->getBlockId(), $this->getImageConversionType($conversion), $this->imageTitle, $attributes);
}

public function getImageUrl(?string $conversion = null): ?string
{
return $this->getMediaUrl(imageId: $this->imageId, conversion: $this->getImageConversionType($conversion));
}

public function hasImage(): bool
{
return isset($this->imageId) && ! is_null($this->imageId);
return $this->getMediaUrl(blockId: $this->getBlockId(), conversion: $this->getImageConversionType($conversion));
}

public function getSearchableContent(): array
{
$searchable = [];

$this->addSearchableContent($searchable, $this->imageTitle);
$this->addSearchableContent($searchable, $this->imageCopyright);

return $searchable;
}

public function getImageUuids(): array
{
return $this->imageId ? [$this->imageId] : [];
}
}
Loading

0 comments on commit 24575a0

Please sign in to comment.