From a4120691153ec772b4f6fe53b921aa3d802834ee Mon Sep 17 00:00:00 2001 From: Sten Govaerts Date: Fri, 27 Sep 2024 16:52:59 +0200 Subject: [PATCH 01/12] refactor block images to no longer use the media uuid, but make only use of the block id in custom_properties of Media to filter. --- UPGRADE.md | 12 ++++ .../content-blocks/tailwind/cards.blade.php | 2 +- src/ContentBlocks/AbstractContentBlock.php | 27 +------- src/ContentBlocks/CallToActionBlock.php | 17 +---- src/ContentBlocks/CardsBlock.php | 30 ++------ src/ContentBlocks/Concerns/HasImage.php | 54 ++++++++++----- src/ContentBlocks/ImageBlock.php | 18 +---- src/ContentBlocks/TextImageBlock.php | 17 +---- src/ContentBlocks/VideoBlock.php | 14 +--- ...opyContentBlocksToLocalesActionHandler.php | 4 +- .../BlockSpatieMediaLibraryFileUpload.php | 69 +------------------ .../Form/Fields/Blocks/Data/CardData.php | 38 +++++----- ...anslatableSpatieMediaLibraryFileUpload.php | 63 ----------------- .../Concerns/TranslatableWithMedia.php | 48 ------------- .../Concerns/TranslatableWithMedia.php | 48 ------------- 15 files changed, 89 insertions(+), 372 deletions(-) diff --git a/UPGRADE.md b/UPGRADE.md index aa60d29..e34f2bf 100644 --- a/UPGRADE.md +++ b/UPGRADE.md @@ -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 + + {!! $getCardImageMedia($card->cardId, $card->title, false, ['class' => 'w-full']) !!} + +``` + ## v2.0.0 To upgrade to Laravel 11, we needed to migrate to spatie-medialibrary v11, which required an upgrade to spatie-image v3. diff --git a/resources/views/content-blocks/tailwind/cards.blade.php b/resources/views/content-blocks/tailwind/cards.blade.php index b9d7d01..f4ad7b3 100644 --- a/resources/views/content-blocks/tailwind/cards.blade.php +++ b/resources/views/content-blocks/tailwind/cards.blade.php @@ -10,7 +10,7 @@ @endphp - {!! $getCardImageMedia($card->imageId, $card->title, false, ['class' => 'w-full']) !!} + {!! $getCardImageMedia($card->cardId, $card->title, false, ['class' => 'w-full']) !!} @endforeach diff --git a/src/ContentBlocks/AbstractContentBlock.php b/src/ContentBlocks/AbstractContentBlock.php index 58c2764..d519938 100644 --- a/src/ContentBlocks/AbstractContentBlock.php +++ b/src/ContentBlocks/AbstractContentBlock.php @@ -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]; } /** @@ -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; diff --git a/src/ContentBlocks/CallToActionBlock.php b/src/ContentBlocks/CallToActionBlock.php index 4a232e6..9f6db76 100644 --- a/src/ContentBlocks/CallToActionBlock.php +++ b/src/ContentBlocks/CallToActionBlock.php @@ -37,8 +37,6 @@ class CallToActionBlock extends AbstractFilamentFlexibleContentBlock public ?string $text; - public ?string $imageId; - public ?string $imageTitle; public ?string $imageCopyright; @@ -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); @@ -126,17 +123,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 @@ -149,9 +141,4 @@ public function getSearchableContent(): array return $searchable; } - - public function getImageUuids(): array - { - return $this->imageId ? [$this->imageId] : []; - } } diff --git a/src/ContentBlocks/CardsBlock.php b/src/ContentBlocks/CardsBlock.php index fc437c7..b09b23f 100644 --- a/src/ContentBlocks/CardsBlock.php +++ b/src/ContentBlocks/CardsBlock.php @@ -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; @@ -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)); } /** @@ -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); @@ -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; - } } diff --git a/src/ContentBlocks/Concerns/HasImage.php b/src/ContentBlocks/Concerns/HasImage.php index 711b383..aa1515b 100644 --- a/src/ContentBlocks/Concerns/HasImage.php +++ b/src/ContentBlocks/Concerns/HasImage.php @@ -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; @@ -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, ?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) { @@ -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(); } @@ -86,11 +111,4 @@ protected static function addImageConversion(HasMedia&HasMediaAttributes $record return $conversion; } - - /** - * Return all image UUIDs of this block. - * - * @return array - */ - abstract public function getImageUuids(): array; } diff --git a/src/ContentBlocks/ImageBlock.php b/src/ContentBlocks/ImageBlock.php index cfaf59a..43d634f 100644 --- a/src/ContentBlocks/ImageBlock.php +++ b/src/ContentBlocks/ImageBlock.php @@ -29,8 +29,6 @@ class ImageBlock extends AbstractFilamentFlexibleContentBlock use HasImageConversionType; use HasImageWidth; - public ?string $imageId; - public ?string $imageTitle; public ?string $imageCopyright; @@ -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; @@ -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] : []; - } } diff --git a/src/ContentBlocks/TextImageBlock.php b/src/ContentBlocks/TextImageBlock.php index 25ee240..392e73a 100644 --- a/src/ContentBlocks/TextImageBlock.php +++ b/src/ContentBlocks/TextImageBlock.php @@ -35,8 +35,6 @@ class TextImageBlock extends AbstractFilamentFlexibleContentBlock public ?string $text; - public ?string $imageId; - public ?string $imageTitle; public ?string $imageCopyright; @@ -55,7 +53,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->imagePosition = $blockData['image_position'] ?? null; @@ -133,17 +130,12 @@ public static function addMediaCollectionAndConversion(HasMedia&HasMediaAttribut 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 @@ -156,9 +148,4 @@ public function getSearchableContent(): array return $searchable; } - - public function getImageUuids(): array - { - return $this->imageId ? [$this->imageId] : []; - } } diff --git a/src/ContentBlocks/VideoBlock.php b/src/ContentBlocks/VideoBlock.php index 50afae9..3b0457d 100644 --- a/src/ContentBlocks/VideoBlock.php +++ b/src/ContentBlocks/VideoBlock.php @@ -19,8 +19,6 @@ class VideoBlock extends AbstractFilamentFlexibleContentBlock public ?string $embedUrl; - public ?string $overlayImageId; - /** * Create a new component instance. */ @@ -29,7 +27,6 @@ public function __construct(HasContentBlocks&HasMedia $record, ?array $blockData parent::__construct($record, $blockData); $this->embedUrl = $blockData['embed_url'] ?? null; - $this->overlayImageId = $this->getMediaUuid($blockData['overlay_image']) ?? null; } public static function getNameSuffix(): string @@ -102,21 +99,16 @@ public static function addMediaCollectionAndConversion(HasMedia&HasMediaAttribut public function getOverlayImageMedia(array $attributes = []): ?HtmlableMedia { - return $this->getHtmlableMedia($this->overlayImageId, static::CONVERSION_CROP, null, $attributes); + return $this->getHtmlableMedia($this->getBlockId(), static::CONVERSION_CROP, null, $attributes); } public function getOverlayImageUrl(): ?string { - return $this->getMediaUrl($this->overlayImageId); + return $this->getMediaUrl($this->getBlockId()); } public function hasOverlayImage(): bool { - return isset($this->overlayImageId) && ! is_null($this->overlayImageId); - } - - public function getImageUuids(): array - { - return $this->overlayImageId ? [$this->overlayImageId] : []; + return $this->hasMedia($this->getBlockId()); } } diff --git a/src/Filament/Actions/CopyContentBlocksToLocalesActionHandler.php b/src/Filament/Actions/CopyContentBlocksToLocalesActionHandler.php index 6807054..5e485b1 100644 --- a/src/Filament/Actions/CopyContentBlocksToLocalesActionHandler.php +++ b/src/Filament/Actions/CopyContentBlocksToLocalesActionHandler.php @@ -117,8 +117,8 @@ private function convertBlockId(array $contentBlock): array } //generate new block id: - if (isset($dataBlock['block_id']) && $dataBlock['block_id']) { - $dataBlock['block_id'] = BlockIdField::generateBlockId(); + if (isset($dataBlock[BlockIdField::FIELD]) && $dataBlock[BlockIdField::FIELD]) { + $dataBlock[BlockIdField::FIELD] = BlockIdField::generateBlockId(); } //handle all block IDs in deeper data structures, e.g. cards. diff --git a/src/Filament/Form/Fields/Blocks/BlockSpatieMediaLibraryFileUpload.php b/src/Filament/Form/Fields/Blocks/BlockSpatieMediaLibraryFileUpload.php index 5937c2b..c3ed04f 100644 --- a/src/Filament/Form/Fields/Blocks/BlockSpatieMediaLibraryFileUpload.php +++ b/src/Filament/Form/Fields/Blocks/BlockSpatieMediaLibraryFileUpload.php @@ -6,11 +6,9 @@ use Filament\Forms\Components\Concerns\CanBeValidated; use Filament\Forms\Components\SpatieMediaLibraryFileUpload; use Filament\Forms\Get; -use Illuminate\Database\Eloquent\Model; use Illuminate\Support\Collection; use Illuminate\Support\Facades\Validator; use Livewire\Features\SupportFileUploads\TemporaryUploadedFile; -use Spatie\MediaLibrary\HasMedia; use Spatie\MediaLibrary\MediaCollections\Models\Media; use Statikbe\FilamentFlexibleContentBlocks\Filament\Form\Fields\BlockIdField; use Statikbe\FilamentFlexibleContentBlocks\Filament\Form\Fields\Concerns\HasImageEditor; @@ -42,69 +40,6 @@ protected function setUp(): void return $item->getCustomProperty('block') === $get(BlockIdField::FIELD); }); }); - - //temp until PR is merged: - $this->loadStateFromRelationshipsUsing(static function (SpatieMediaLibraryFileUpload $component, HasMedia $record): void { - /** @var Model&HasMedia $record */ - $media = $record->load('media')->getMedia($component->getCollection()) - ->when( - $component->hasMediaFilter(), - fn (Collection $media) => $component->filterMedia($media) - ) - ->when( - ! $component->isMultiple(), - fn (Collection $media): Collection => $media->take(1), - ) - ->mapWithKeys(function (Media $media): array { - $uuid = $media->getAttributeValue('uuid'); - - return [$uuid => $uuid]; - }) - ->toArray(); - - $component->state($media); - }); - - //Make sure the uuid of the image is added to the form data - $this->dehydrated(true); - - //TODO remove after filament release >3.2.72 - $this->getUploadedFileUsing(static function (SpatieMediaLibraryFileUpload $component, string $file): ?array { - if (! $component->getRecord()) { - return null; - } - - /** @var ?Media $media */ - $media = $component->getRecord()->getRelationValue('media')->firstWhere('uuid', $file); - - $url = null; - - if ($component->getVisibility() === 'private') { - $conversion = $component->getConversion(); - - try { - $url = $media?->getTemporaryUrl( - now()->addMinutes(5), - (filled($conversion) && $media->hasGeneratedConversion($conversion)) ? $conversion : '', - ); - } catch (Throwable $exception) { - // This driver does not support creating temporary URLs. - } - } - - if ($component->getConversion() && $media?->hasGeneratedConversion($component->getConversion())) { - $url ??= $media->getUrl($component->getConversion()); - } - - $url ??= $media?->getUrl(); - - return [ - 'name' => $media?->getAttributeValue('name') ?? $media?->getAttributeValue('file_name'), - 'size' => $media?->getAttributeValue('size'), - 'type' => $media?->getAttributeValue('mime_type'), - 'url' => $url, - ]; - }); } public function getState(): mixed @@ -149,10 +84,12 @@ public function getValidationRules(): array $name = $this->getName(); + $validationMessages = $this->getValidationMessages(); + $validator = Validator::make( [$name => $files], ["{$name}.*" => ['file', ...$this->getValidationRulesTrait()]], //Changed to load validation rules of trait (see on top of class) instead of the rules from BaseFileUpload. - [], + $validationMessages ? ["{$name}.*" => $validationMessages] : [], ["{$name}.*" => $this->getValidationAttribute()], ); diff --git a/src/Filament/Form/Fields/Blocks/Data/CardData.php b/src/Filament/Form/Fields/Blocks/Data/CardData.php index 4f0dbda..a721c71 100644 --- a/src/Filament/Form/Fields/Blocks/Data/CardData.php +++ b/src/Filament/Form/Fields/Blocks/Data/CardData.php @@ -2,51 +2,45 @@ namespace Statikbe\FilamentFlexibleContentBlocks\Filament\Form\Fields\Blocks\Data; -use Illuminate\Support\Arr; -use Illuminate\Support\Str; +use Statikbe\FilamentFlexibleContentBlocks\ContentBlocks\CardsBlock; use Statikbe\FilamentFlexibleContentBlocks\Exceptions\CallToActionNotDefinedException; use Statikbe\FilamentFlexibleContentBlocks\Exceptions\LinkableModelNotFoundException; use Statikbe\FilamentFlexibleContentBlocks\Filament\Form\Fields\BlockIdField; +use Statikbe\FilamentFlexibleContentBlocks\Filament\Form\Fields\Blocks\CallToActionField; class CardData { - public ?string $imageId; - /** * @param array|null $callToActions */ public function __construct( - public string $block_id, + public string $cardId, public ?string $title, public ?string $text, public ?array $callToActions, - string|array|null $imageId, + public bool $hasImage = false, public ?string $imageUrl = null, public ?string $imageHtml = null, public ?string $blockStyle = null, - ) { - //clean up image id if it was saved as an array with keys uuid: - if (is_array($imageId)) { - $this->imageId = Arr::first(array_keys($imageId)); - //if the id is not a UUID, let's try the values: - if (! Str::isUuid($this->imageId)) { - $this->imageId = Arr::first(array_values($imageId)); - } - } else { - $this->imageId = $imageId; - } - } + ) {} public function hasImage(): bool { - return isset($this->imageId) && ! is_null($this->imageId); + return $this->hasImage; } /** * @throws LinkableModelNotFoundException */ - public static function create(array $cardBlockData, ?string $imageUrl, ?string $imageHtml, ?string $blockStyle, array $buttonStyleClasses): self + public static function create(array $cardBlockData, CardsBlock $cardsBlock): self { + $cardId = $cardBlockData[BlockIdField::FIELD] ?? BlockIdField::generateBlockId(); + $hasImage = $cardsBlock->hasImage($cardId); + $imageUrl = $hasImage ? $cardsBlock->getCardImageUrl($cardId) : null; + $imageHtml = $hasImage ? $cardsBlock->getCardImageMedia($cardId, $cardBlockData['title']) : null; + $blockStyle = $cardsBlock->hasDefaultBlockStyle() ? null : $cardsBlock->blockStyle; + $buttonStyleClasses = CallToActionField::getButtonStyleClasses($cardsBlock::class); + $callToActions = []; if (! empty($cardBlockData['card_call_to_action'])) { foreach ($cardBlockData['card_call_to_action'] as $callToAction) { @@ -59,11 +53,11 @@ public static function create(array $cardBlockData, ?string $imageUrl, ?string $ } return new self( - block_id: $cardBlockData[BlockIdField::FIELD] ?? BlockIdField::generateBlockId(), + cardId: $cardId, title: $cardBlockData['title'] ?? null, text: $cardBlockData['text'] ?? null, callToActions: $callToActions, - imageId: ! empty($cardBlockData['image']) ? $cardBlockData['image'] : null, + hasImage: $hasImage, imageUrl: $imageUrl, imageHtml: $imageHtml, blockStyle: $blockStyle diff --git a/src/Filament/Form/Fields/TranslatableSpatieMediaLibraryFileUpload.php b/src/Filament/Form/Fields/TranslatableSpatieMediaLibraryFileUpload.php index 12b3301..efb1097 100644 --- a/src/Filament/Form/Fields/TranslatableSpatieMediaLibraryFileUpload.php +++ b/src/Filament/Form/Fields/TranslatableSpatieMediaLibraryFileUpload.php @@ -3,13 +3,10 @@ namespace Statikbe\FilamentFlexibleContentBlocks\Filament\Form\Fields; use Filament\Forms\Components\SpatieMediaLibraryFileUpload; -use Illuminate\Database\Eloquent\Model; use Illuminate\Support\Collection; use Livewire\Component as Livewire; -use Spatie\MediaLibrary\HasMedia; use Spatie\MediaLibrary\MediaCollections\Models\Media; use Statikbe\FilamentFlexibleContentBlocks\Filament\Form\Fields\Concerns\HasImageEditor; -use Throwable; class TranslatableSpatieMediaLibraryFileUpload extends SpatieMediaLibraryFileUpload { @@ -32,66 +29,6 @@ protected function setUp(): void return $item->getCustomProperty('locale') == $filter['locale']; }); }); - - //temp until PR is merged: - $this->loadStateFromRelationshipsUsing(static function (SpatieMediaLibraryFileUpload $component, HasMedia $record): void { - /** @var Model&HasMedia $record */ - $media = $record->load('media')->getMedia($component->getCollection()) - ->when( - $component->hasMediaFilter(), - fn (Collection $media) => $component->filterMedia($media) - ) - ->when( - ! $component->isMultiple(), - fn (Collection $media): Collection => $media->take(1), - ) - ->mapWithKeys(function (Media $media): array { - $uuid = $media->getAttributeValue('uuid'); - - return [$uuid => $uuid]; - }) - ->toArray(); - - $component->state($media); - }); - - //TODO remove after filament release >3.2.72 - $this->getUploadedFileUsing(static function (SpatieMediaLibraryFileUpload $component, string $file): ?array { - if (! $component->getRecord()) { - return null; - } - - /** @var ?Media $media */ - $media = $component->getRecord()->getRelationValue('media')->firstWhere('uuid', $file); - - $url = null; - - if ($component->getVisibility() === 'private') { - $conversion = $component->getConversion(); - - try { - $url = $media?->getTemporaryUrl( - now()->addMinutes(5), - (filled($conversion) && $media->hasGeneratedConversion($conversion)) ? $conversion : '', - ); - } catch (Throwable $exception) { - // This driver does not support creating temporary URLs. - } - } - - if ($component->getConversion() && $media?->hasGeneratedConversion($component->getConversion())) { - $url ??= $media->getUrl($component->getConversion()); - } - - $url ??= $media?->getUrl(); - - return [ - 'name' => $media?->getAttributeValue('name') ?? $media?->getAttributeValue('file_name'), - 'size' => $media?->getAttributeValue('size'), - 'type' => $media?->getAttributeValue('mime_type'), - 'url' => $url, - ]; - }); } private function getCurrentLocaleFilter(Livewire $livewire): array diff --git a/src/Filament/Pages/CreateRecord/Concerns/TranslatableWithMedia.php b/src/Filament/Pages/CreateRecord/Concerns/TranslatableWithMedia.php index d245d61..0a21b49 100644 --- a/src/Filament/Pages/CreateRecord/Concerns/TranslatableWithMedia.php +++ b/src/Filament/Pages/CreateRecord/Concerns/TranslatableWithMedia.php @@ -5,9 +5,7 @@ use Filament\Resources\Pages\CreateRecord\Concerns\Translatable; use Illuminate\Database\Eloquent\Model; use Illuminate\Support\Arr; -use Illuminate\Support\Str; use Illuminate\Validation\ValidationException; -use Statikbe\FilamentFlexibleContentBlocks\Filament\Form\Fields\ContentBlocksField; trait TranslatableWithMedia { @@ -78,11 +76,6 @@ public function updatedActiveLocale(string $newActiveLocale): void ...$this->otherLocaleData[$this->activeLocale] ?? [], ]); - //handle images in content blocks field: - /*if (isset($this->data[ContentBlocksField::FIELD])) { - $this->data[ContentBlocksField::FIELD] = $this->transformContentBlocksImagesToArray($this->data[ContentBlocksField::FIELD]); - }*/ - unset($this->otherLocaleData[$this->activeLocale]); } @@ -98,45 +91,4 @@ private function getTranslatedAttributesWithTranslatableMedia(): array return $translatableAttributes; } - - //TODO remove double code from other TranslatableWithMedia: - private function transformContentBlocksImagesToArray(array $contentBlocks): array - { - $transformedBlocks = []; - foreach ($contentBlocks as $key => &$contentBlock) { - if (is_array($contentBlock)) { - if (is_int($key)) { - $key = Str::uuid()->toString(); - } - $transformedBlocks[$key] = $this->transformOneContentBlocksImagesToArray($contentBlock); - } - } - - return $transformedBlocks; - } - - private function transformOneContentBlocksImagesToArray(array &$contentBlock): array - { - $dataBlock = &$contentBlock; - if (isset($contentBlock['data'])) { - $dataBlock = &$contentBlock['data']; - } - - //TODO configure image fields - $fields = ['image']; - foreach ($fields as $field) { - if (isset($dataBlock[$field]) && ! is_array($dataBlock[$field])) { - //put file fields in an array: - $dataBlock[$field] = [$dataBlock[$field] => $dataBlock[$field]]; - } - } - - foreach ($dataBlock as $var => $data) { - if (is_array($data) && ! in_array($var, $fields)) { - $dataBlock[$var] = $this->transformOneContentBlocksImagesToArray($data); - } - } - - return $contentBlock; - } } diff --git a/src/Filament/Pages/EditRecord/Concerns/TranslatableWithMedia.php b/src/Filament/Pages/EditRecord/Concerns/TranslatableWithMedia.php index f712300..1438eac 100644 --- a/src/Filament/Pages/EditRecord/Concerns/TranslatableWithMedia.php +++ b/src/Filament/Pages/EditRecord/Concerns/TranslatableWithMedia.php @@ -5,9 +5,7 @@ use Filament\Resources\Pages\EditRecord\Concerns\Translatable; use Illuminate\Database\Eloquent\Model; use Illuminate\Support\Arr; -use Illuminate\Support\Str; use Illuminate\Validation\ValidationException; -use Statikbe\FilamentFlexibleContentBlocks\Filament\Form\Fields\ContentBlocksField; use Statikbe\FilamentFlexibleContentBlocks\Models\Contracts\HasTranslatableMedia; trait TranslatableWithMedia @@ -104,11 +102,6 @@ public function updatedActiveLocale(): void ...$this->otherLocaleData[$this->activeLocale] ?? [], ]); - //handle images in content blocks field: - /*if (isset($this->data[ContentBlocksField::FIELD])) { - $this->data[ContentBlocksField::FIELD] = $this->transformContentBlocksImagesToArray($this->data[ContentBlocksField::FIELD]); - }*/ - unset($this->otherLocaleData[$this->activeLocale]); } @@ -123,45 +116,4 @@ private function getTranslatedAttributesWithTranslatableMedia(): array return $translatableAttributes; } - - //TODO remove double code from other TranslatableWithMedia: - private function transformContentBlocksImagesToArray(array $contentBlocks): array - { - $transformedBlocks = []; - foreach ($contentBlocks as $key => &$contentBlock) { - if (is_array($contentBlock)) { - if (is_int($key)) { - $key = Str::uuid()->toString(); - } - $transformedBlocks[$key] = $this->transformOneContentBlocksImagesToArray($contentBlock); - } - } - - return $transformedBlocks; - } - - private function transformOneContentBlocksImagesToArray(array &$contentBlock): array - { - $dataBlock = &$contentBlock; - if (isset($contentBlock['data'])) { - $dataBlock = &$contentBlock['data']; - } - - //TODO configure image fields - $fields = ['image']; - foreach ($fields as $field) { - if (isset($dataBlock[$field]) && ! is_array($dataBlock[$field])) { - //put file fields in an array: - $dataBlock[$field] = [$dataBlock[$field] => $dataBlock[$field]]; - } - } - - foreach ($dataBlock as $var => $data) { - if (is_array($data) && ! in_array($var, $fields)) { - $dataBlock[$var] = $this->transformOneContentBlocksImagesToArray($data); - } - } - - return $contentBlock; - } } From 9285256829b0b281069baf9b9c3d751a154e9cc2 Mon Sep 17 00:00:00 2001 From: sten Date: Fri, 27 Sep 2024 14:57:00 +0000 Subject: [PATCH 02/12] Update CHANGELOG --- CHANGELOG.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5c1b21b..e4e6f29 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,14 @@ All notable changes to `laravel-filament-flexible-content-blocks` will be documented in this file. +## 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 From 1438e997f3d14a22d8bb81051d2baf1b1632f9bd Mon Sep 17 00:00:00 2001 From: Sten Govaerts Date: Mon, 30 Sep 2024 13:24:26 +0200 Subject: [PATCH 03/12] Fix small bug in hasImage function --- src/ContentBlocks/Concerns/HasImage.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ContentBlocks/Concerns/HasImage.php b/src/ContentBlocks/Concerns/HasImage.php index aa1515b..861fd44 100644 --- a/src/ContentBlocks/Concerns/HasImage.php +++ b/src/ContentBlocks/Concerns/HasImage.php @@ -41,7 +41,7 @@ protected function getAllMedia(?string $blockId, ?string $collection = null): ?M ]); } - public function hasImage(?string $blockId, ?string $collection = null): bool + public function hasImage(?string $blockId = null, ?string $collection = null): bool { if (! $blockId) { $blockId = $this->getBlockId(); From 9253c69c12ce9fc610a8b85ed6c72139488ad829 Mon Sep 17 00:00:00 2001 From: sten Date: Mon, 30 Sep 2024 11:26:08 +0000 Subject: [PATCH 04/12] Update CHANGELOG --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index e4e6f29..a6f35a4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ All notable changes to `laravel-filament-flexible-content-blocks` will be documented in this file. +## 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. From 1c7c749cf455712492661f390db4eab94511f96b Mon Sep 17 00:00:00 2001 From: Dominique Vermeersch <1379550+Sindoweb@users.noreply.github.com> Date: Mon, 30 Sep 2024 16:24:04 +0200 Subject: [PATCH 05/12] Update VideoBlock.php The method hasMedia does not exist - replaced with hasImage and tested to return true or false. --- src/ContentBlocks/VideoBlock.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ContentBlocks/VideoBlock.php b/src/ContentBlocks/VideoBlock.php index 3b0457d..69b914d 100644 --- a/src/ContentBlocks/VideoBlock.php +++ b/src/ContentBlocks/VideoBlock.php @@ -109,6 +109,6 @@ public function getOverlayImageUrl(): ?string public function hasOverlayImage(): bool { - return $this->hasMedia($this->getBlockId()); + return $this->hasImage($this->getBlockId()); } } From 66cb45157eb5b6e3faccd48f75caec79cb5f765e Mon Sep 17 00:00:00 2001 From: sten Date: Mon, 30 Sep 2024 15:06:43 +0000 Subject: [PATCH 06/12] Update CHANGELOG --- CHANGELOG.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index a6f35a4..60bda7a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,14 @@ All notable changes to `laravel-filament-flexible-content-blocks` will be documented in this 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. From 7b93714eb477ed8e43137b0908b687b00b4ca84e Mon Sep 17 00:00:00 2001 From: Sten Govaerts Date: Wed, 9 Oct 2024 17:28:53 +0200 Subject: [PATCH 07/12] enforce media library v11 because we use the enum Fit in the config file --- composer.json | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/composer.json b/composer.json index ae17cab..e2e8aa6 100644 --- a/composer.json +++ b/composer.json @@ -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", From 7cb2ef3eb6539a0d631c222a93cf5f1a094d0e93 Mon Sep 17 00:00:00 2001 From: sten Date: Wed, 9 Oct 2024 15:30:31 +0000 Subject: [PATCH 08/12] Update CHANGELOG --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 60bda7a..971c2b9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ All notable changes to `laravel-filament-flexible-content-blocks` will be documented in this file. +## 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 From 01ea74c11627c04d5e2bea542652bdc0f4095a69 Mon Sep 17 00:00:00 2001 From: Sten Govaerts Date: Thu, 10 Oct 2024 17:20:45 +0200 Subject: [PATCH 09/12] Fix copy locales block action to support refactored block media field --- ...opyContentBlocksToLocalesActionHandler.php | 131 ++++-------------- .../CopyContentBlocksToLocalesAction.php | 5 +- 2 files changed, 27 insertions(+), 109 deletions(-) diff --git a/src/Filament/Actions/CopyContentBlocksToLocalesActionHandler.php b/src/Filament/Actions/CopyContentBlocksToLocalesActionHandler.php index 5e485b1..63f9baf 100644 --- a/src/Filament/Actions/CopyContentBlocksToLocalesActionHandler.php +++ b/src/Filament/Actions/CopyContentBlocksToLocalesActionHandler.php @@ -4,9 +4,10 @@ use Filament\Notifications\Notification; use Illuminate\Database\Eloquent\Model; -use Illuminate\Support\Arr; +use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\Log; use Livewire\Component; +use Spatie\MediaLibrary\HasMedia; use Spatie\MediaLibrary\MediaCollections\Models\Media; use Statikbe\FilamentFlexibleContentBlocks\Filament\Form\Fields\BlockIdField; use Statikbe\FilamentFlexibleContentBlocks\Filament\Form\Fields\ContentBlocksField; @@ -17,11 +18,7 @@ */ class CopyContentBlocksToLocalesActionHandler { - private array $imageFields = ['image']; - - private array $fileUploadFields = []; - - public function handle(Model $record, Component $livewire, ?array $contentBlocks): void + public function handle(Model&HasMedia $record, Component $livewire, ?array $contentBlocks): void { if ($contentBlocks) { //check if the LocaleSwitch action is implemented: @@ -48,17 +45,19 @@ public function handle(Model $record, Component $livewire, ?array $contentBlocks $currentLocale = $livewire->getActiveFormsLocale(); $otherLocales = collect(FilamentFlexibleContentBlocks::getLocales())->diff([$currentLocale]); + DB::beginTransaction(); //copy content blocks foreach ($otherLocales as $otherLocale) { - $convertedContentBlocks = $this->convertContentBlockIsAndImages($record, $contentBlocks, $otherLocale); + $convertedContentBlocks = $this->convertContentBlockIdAndImages($record, $contentBlocks, $otherLocale); $record->setTranslation(ContentBlocksField::FIELD, $otherLocale, $convertedContentBlocks); //update form data: - $livewire->otherLocaleData[$otherLocale][ContentBlocksField::FIELD] = $this->transformToFileUploadFormData($convertedContentBlocks); + $livewire->otherLocaleData[$otherLocale][ContentBlocksField::FIELD] = $convertedContentBlocks; } if ($otherLocales->isNotEmpty()) { $record->save(); } + DB::commit(); Notification::make() ->title(trans('filament-flexible-content-blocks::filament-flexible-content-blocks.form_component.copy_content_blocks_to_other_locales.success')) @@ -70,6 +69,7 @@ public function handle(Model $record, Component $livewire, ?array $contentBlocks } catch (\Exception $exception) { Log::error($exception); + DB::rollBack(); Notification::make() ->title(trans('filament-flexible-content-blocks::filament-flexible-content-blocks.form_component.copy_content_blocks_to_other_locales.error', ['error' => $exception->getMessage()])) ->danger() @@ -78,38 +78,17 @@ public function handle(Model $record, Component $livewire, ?array $contentBlocks } } - private function convertContentBlockIsAndImages(Model $record, array $contentBlocks, string $locale): array + private function convertContentBlockIdAndImages(Model&HasMedia $record, array $contentBlocks, string $locale): array { $convertedBlocks = []; foreach ($contentBlocks as $block) { - $block = $this->convertBlockId($block); - - $convertedBlocks[] = $this->copyImagesToBlock($record, $block); + $convertedBlocks[] = $this->convertBlockIdAndCopyImagesToBlock($record, $block); } return $convertedBlocks; } - /** - * Add extra image fields to convert. - * Useful if you have custom blocks with images that use other field names than `image`. - * - * @param array $extraImageFields - */ - public function addImageFields(array $extraImageFields): void - { - $this->imageFields = array_merge($this->imageFields, $extraImageFields); - } - - /** - * Add file upload field names used in the custom blocks to be able to transform the localized form data. - */ - public function addFileUploadField(array $fileUploadFields): void - { - $this->fileUploadFields = array_merge($this->fileUploadFields, $fileUploadFields); - } - - private function convertBlockId(array $contentBlock): array + private function convertBlockIdAndCopyImagesToBlock(Model&HasMedia $record, array $contentBlock): array { $dataBlock = &$contentBlock; if (isset($dataBlock['data'])) { @@ -118,57 +97,29 @@ private function convertBlockId(array $contentBlock): array //generate new block id: if (isset($dataBlock[BlockIdField::FIELD]) && $dataBlock[BlockIdField::FIELD]) { - $dataBlock[BlockIdField::FIELD] = BlockIdField::generateBlockId(); - } - - //handle all block IDs in deeper data structures, e.g. cards. - foreach ($dataBlock as $var => $data) { - if (is_array($data)) { - $dataBlock[$var] = $this->convertBlockId($data); + $oldBlockId = $dataBlock[BlockIdField::FIELD]; + $newBlockId = BlockIdField::generateBlockId(); + $dataBlock[BlockIdField::FIELD] = $newBlockId; + + //copy media to new block: + $oldBlockMedia = $record->getMedia('*', ['block' => $oldBlockId]); + foreach($oldBlockMedia as $oldBlockMediaItem) { + $this->copyImage($record, $oldBlockMediaItem, $newBlockId); } - } - return $contentBlock; - } - - private function copyImagesToBlock(Model $record, array $contentBlock): array - { - $dataBlock = &$contentBlock; - if (isset($dataBlock['data'])) { - $dataBlock = &$contentBlock['data']; - } - - foreach ($this->imageFields as $imageField) { - if (isset($dataBlock[$imageField])) { - $blockId = $dataBlock[BlockIdField::FIELD] ?? null; - $imageId = $dataBlock[$imageField]; - if (is_array($imageId)) { - $imageId = array_keys($imageId); - //for multiple image upload, keep it the array, otherwise only save the single UUID: - if (count($imageId) === 1) { - $imageId = Arr::first($imageId); - } - //TODO support fields with multiple images + foreach ($dataBlock as $var => $data) { + if (is_array($data)) { + $dataBlock[$var] = $this->convertBlockIdAndCopyImagesToBlock($record, $data); } - $dataBlock[$imageField] = $this->copyImage($record, $imageId, $blockId); - } - } - - foreach ($dataBlock as $var => $data) { - if (is_array($data)) { - $blockId = $dataBlock[BlockIdField::FIELD] ?? null; - $dataBlock[$var] = $this->copyImagesToBlock($record, $data, $blockId); } } return $contentBlock; } - private function copyImage(Model $record, string $imageUuid, ?string $blockId): string + private function copyImage(Model&HasMedia $record, Media $oldMediaItem, ?string $blockId): string { - $image = Media::findByUuid($imageUuid); - - $copiedImage = $image->copy($record, $image->collection_name, $image->disk); + $copiedImage = $oldMediaItem->copy($record, $oldMediaItem->collection_name, $oldMediaItem->disk); //set block ID: if ($blockId) { @@ -178,38 +129,4 @@ private function copyImage(Model $record, string $imageUuid, ?string $blockId): return $copiedImage->uuid; } - - private function transformToFileUploadFormData(array $contentBlocks): array - { - foreach ($contentBlocks as &$block) { - $this->transformBlockToFileUploadFormData($block); - } - - return $contentBlocks; - } - - private function transformBlockToFileUploadFormData(array &$contentBlock): array - { - $dataBlock = &$contentBlock; - if (isset($dataBlock['data'])) { - $dataBlock = &$contentBlock['data']; - } - - $fields = array_unique(array_merge($this->imageFields, $this->fileUploadFields)); - - foreach ($fields as $fileField) { - if (isset($dataBlock[$fileField]) && ! is_array($dataBlock[$fileField])) { - //put file fields in an array: - $dataBlock[$fileField] = [$dataBlock[$fileField] => $dataBlock[$fileField]]; - } - } - - foreach ($dataBlock as $var => $data) { - if (is_array($data) && ! in_array($var, $fields)) { - $dataBlock[$var] = $this->transformBlockToFileUploadFormData($data); - } - } - - return $contentBlock; - } } diff --git a/src/Filament/Resource/Pages/Actions/CopyContentBlocksToLocalesAction.php b/src/Filament/Resource/Pages/Actions/CopyContentBlocksToLocalesAction.php index 21e2762..d9dd1c8 100644 --- a/src/Filament/Resource/Pages/Actions/CopyContentBlocksToLocalesAction.php +++ b/src/Filament/Resource/Pages/Actions/CopyContentBlocksToLocalesAction.php @@ -5,6 +5,7 @@ use Filament\Pages\Actions\Action; use Filament\Resources\Pages\EditRecord; use Illuminate\Database\Eloquent\Model; +use Spatie\MediaLibrary\HasMedia; use Statikbe\FilamentFlexibleContentBlocks\Filament\Actions\CopyContentBlocksToLocalesActionHandler; use Statikbe\FilamentFlexibleContentBlocks\Models\Contracts\HasContentBlocks; @@ -33,9 +34,9 @@ public function setUp(): void $this->action(function () { /** @var EditRecord $page * */ $page = $this->livewire; - /* @var Model&HasContentBlocks $record */ + /* @var Model&HasContentBlocks&HasMedia $record */ $record = $page->getRecord(); - $handler = new CopyContentBlocksToLocalesActionHandler; + $handler = new CopyContentBlocksToLocalesActionHandler(); $handler->handle($record, $this->livewire, $record->content_blocks); }); } From e98809900048d01e39449d5a28b42955f4949913 Mon Sep 17 00:00:00 2001 From: sten Date: Thu, 10 Oct 2024 15:21:28 +0000 Subject: [PATCH 10/12] Fix styling --- .../Actions/CopyContentBlocksToLocalesActionHandler.php | 2 +- .../Resource/Pages/Actions/CopyContentBlocksToLocalesAction.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Filament/Actions/CopyContentBlocksToLocalesActionHandler.php b/src/Filament/Actions/CopyContentBlocksToLocalesActionHandler.php index 63f9baf..eff78e3 100644 --- a/src/Filament/Actions/CopyContentBlocksToLocalesActionHandler.php +++ b/src/Filament/Actions/CopyContentBlocksToLocalesActionHandler.php @@ -103,7 +103,7 @@ private function convertBlockIdAndCopyImagesToBlock(Model&HasMedia $record, arra //copy media to new block: $oldBlockMedia = $record->getMedia('*', ['block' => $oldBlockId]); - foreach($oldBlockMedia as $oldBlockMediaItem) { + foreach ($oldBlockMedia as $oldBlockMediaItem) { $this->copyImage($record, $oldBlockMediaItem, $newBlockId); } diff --git a/src/Filament/Resource/Pages/Actions/CopyContentBlocksToLocalesAction.php b/src/Filament/Resource/Pages/Actions/CopyContentBlocksToLocalesAction.php index d9dd1c8..dac5158 100644 --- a/src/Filament/Resource/Pages/Actions/CopyContentBlocksToLocalesAction.php +++ b/src/Filament/Resource/Pages/Actions/CopyContentBlocksToLocalesAction.php @@ -36,7 +36,7 @@ public function setUp(): void $page = $this->livewire; /* @var Model&HasContentBlocks&HasMedia $record */ $record = $page->getRecord(); - $handler = new CopyContentBlocksToLocalesActionHandler(); + $handler = new CopyContentBlocksToLocalesActionHandler; $handler->handle($record, $this->livewire, $record->content_blocks); }); } From 994a04e5d8fc305ef9623074b8a0e0c24ee2a975 Mon Sep 17 00:00:00 2001 From: Sten Govaerts Date: Thu, 10 Oct 2024 21:50:46 +0200 Subject: [PATCH 11/12] Fix deprecated action parent --- src/Filament/Form/Actions/CopyContentBlocksToLocalesAction.php | 3 +++ .../Pages/Actions/CopyContentBlocksToLocalesAction.php | 3 +-- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/Filament/Form/Actions/CopyContentBlocksToLocalesAction.php b/src/Filament/Form/Actions/CopyContentBlocksToLocalesAction.php index 058468c..4dc3c76 100644 --- a/src/Filament/Form/Actions/CopyContentBlocksToLocalesAction.php +++ b/src/Filament/Form/Actions/CopyContentBlocksToLocalesAction.php @@ -10,6 +10,9 @@ use Statikbe\FilamentFlexibleContentBlocks\Filament\Actions\CopyContentBlocksToLocalesActionHandler; use Statikbe\FilamentFlexibleContentBlocks\Filament\Form\Fields\ContentBlocksField; +/** + * This action can be added as a form component. + */ class CopyContentBlocksToLocalesAction extends Actions { public static function create(): static diff --git a/src/Filament/Resource/Pages/Actions/CopyContentBlocksToLocalesAction.php b/src/Filament/Resource/Pages/Actions/CopyContentBlocksToLocalesAction.php index d9dd1c8..6f6dc4d 100644 --- a/src/Filament/Resource/Pages/Actions/CopyContentBlocksToLocalesAction.php +++ b/src/Filament/Resource/Pages/Actions/CopyContentBlocksToLocalesAction.php @@ -2,7 +2,7 @@ namespace Statikbe\FilamentFlexibleContentBlocks\Filament\Resource\Pages\Actions; -use Filament\Pages\Actions\Action; +use Filament\Actions\Action; use Filament\Resources\Pages\EditRecord; use Illuminate\Database\Eloquent\Model; use Spatie\MediaLibrary\HasMedia; @@ -11,7 +11,6 @@ class CopyContentBlocksToLocalesAction extends Action { - //TODO refactor to create function to support filament v3 see CopyContentBlocksToLocalesAction for resources. public static function getDefaultName(): ?string { return 'copy_content_blocks_to_other_locales_page_action'; From 1b5f333c2881b52dcb488ef2babdc1df3d653ea6 Mon Sep 17 00:00:00 2001 From: sten Date: Thu, 10 Oct 2024 19:53:48 +0000 Subject: [PATCH 12/12] Update CHANGELOG --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 971c2b9..034b28c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ 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.