diff --git a/CHANGELOG.md b/CHANGELOG.md index 74aa3c8bf..4587a4bd4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,17 @@ All notable changes to `twill` will be documented in this file. +## 3.4.1 + +### Improved + +- Allow media and file library disk configuration using an environement variable by [@antonioribeiro](https://github.com/antonioribeiro) in https://github.com/area17/twill/pull/2676 + +### Fixed + +- Fix #2671: 3.4.0 regression on related browsers previews by [@Tofandel](https://github.com/Tofandel) in https://github.com/area17/twill/pull/2672 +- Fix #2674: 3.4.0 regression on relation column using a one-to-one relationship by [@Tofandel](https://github.com/Tofandel) in https://github.com/area17/twill/pull/2675 + ## 3.4.0 ### Added diff --git a/config/file_library.php b/config/file_library.php index 8f700a4c8..a9b025fca 100644 --- a/config/file_library.php +++ b/config/file_library.php @@ -18,7 +18,7 @@ | - 'A17\Twill\Services\FileLibrary\Disk' | */ - 'disk' => 'twill_file_library', + 'disk' => env('FILE_LIBRARY_DISK', 'twill_file_library'), 'endpoint_type' => env('FILE_LIBRARY_ENDPOINT_TYPE', 'local'), 'cascade_delete' => env('FILE_LIBRARY_CASCADE_DELETE', false), 'local_path' => env('FILE_LIBRARY_LOCAL_PATH', 'uploads'), diff --git a/config/media_library.php b/config/media_library.php index 1211a4441..9ec3d2506 100644 --- a/config/media_library.php +++ b/config/media_library.php @@ -20,7 +20,7 @@ | - 'A17\Twill\Services\MediaLibrary\Local' | */ - 'disk' => 'twill_media_library', + 'disk' => env('MEDIA_LIBRARY_DISK', 'twill_media_library'), 'endpoint_type' => env('MEDIA_LIBRARY_ENDPOINT_TYPE', 'local'), 'cascade_delete' => env('MEDIA_LIBRARY_CASCADE_DELETE', false), 'local_path' => env('MEDIA_LIBRARY_LOCAL_PATH', 'uploads'), diff --git a/package-lock.json b/package-lock.json index 3e45a56fe..0dff252bb 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@area17/twill", - "version": "3.4.0", + "version": "3.4.1", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@area17/twill", - "version": "3.4.0", + "version": "3.4.1", "dependencies": { "@alpinejs/mask": "^3.13.5", "@tiptap/extension-hard-break": "^2.2.1", diff --git a/package.json b/package.json index a557f4ad0..b702ed5fe 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@area17/twill", - "version": "3.4.0", + "version": "3.4.1", "private": true, "scripts": { "inspect": "vue-cli-service inspect --mode production", diff --git a/src/Repositories/Behaviors/HandleRevisions.php b/src/Repositories/Behaviors/HandleRevisions.php index c8a9cbd5e..bb1bef427 100644 --- a/src/Repositories/Behaviors/HandleRevisions.php +++ b/src/Repositories/Behaviors/HandleRevisions.php @@ -157,11 +157,9 @@ public function hydrateRelatedBrowsers(TwillModelContract $object, array $fields foreach ($relatedBrowsers as $browser) { $browserField = $fields['browsers'][$browser['browserName']] ?? []; - + $position = 1; foreach ($browserField as $values) { - $position = 1; - - $relatedBrowserItems->push(RelatedItem::make([ + $relatedBrowserItems->push(new RelatedItem([ 'subject_id' => $object->getKey(), 'subject_type' => $object->getMorphClass(), 'related_id' => $values['id'], diff --git a/src/Repositories/BlockRepository.php b/src/Repositories/BlockRepository.php index ed721d7f7..86dc0ac8a 100644 --- a/src/Repositories/BlockRepository.php +++ b/src/Repositories/BlockRepository.php @@ -5,14 +5,12 @@ use A17\Twill\Facades\TwillBlocks; use A17\Twill\Models\Block; use A17\Twill\Models\Contracts\TwillModelContract; +use A17\Twill\Models\RelatedItem; use A17\Twill\Repositories\Behaviors\HandleFiles; use A17\Twill\Repositories\Behaviors\HandleMedias; use A17\Twill\Services\Blocks\Block as BlockConfig; use Illuminate\Config\Repository as Config; use Illuminate\Support\Collection; -use Illuminate\Support\Facades\Log; -use Illuminate\Support\Facades\Schema; -use ReflectionException; class BlockRepository extends ModuleRepository { @@ -35,42 +33,36 @@ public function getCrops(string $role): array public function hydrate(TwillModelContract $model, array $fields): TwillModelContract { - if (Schema::hasTable(config('twill.related_table', 'twill_related'))) { - $relatedItems = Collection::make(); - - Collection::make($fields['browsers'])->each(function ($items, $browserName) use (&$relatedItems) { - Collection::make($items)->each(function ($item) use ($browserName, &$relatedItems) { - try { - // @todo: Repository could be null. - $repository = $this->getModelRepository($item['endpointType'] ?? $browserName); - $relatedItems->push( - (object) [ - 'related' => $repository->getById($item['id']), - 'browser_name' => $browserName, - ] - ); - } catch (ReflectionException $reflectionException) { - Log::error($reflectionException); - } - }); - }); - - $model->setRelation('relatedItems', $relatedItems); - } + $relatedItems = collect($fields['browsers']) + ->flatMap(fn($items, $browserName) => collect($items) + ->map(fn($item, $position) => new RelatedItem([ + 'subject_id' => $model->getKey(), + 'subject_type' => $model->getMorphClass(), + 'related_id' => $item['id'], + 'related_type' => $item['endpointType'], + 'browser_name' => $browserName, + 'position' => $position, + ]))); + + $model->setRelation('relatedItems', $relatedItems); + $model->loadMissing('relatedItems.related'); return parent::hydrate($model, $fields); } + /** @param Block $model */ public function afterSave(TwillModelContract $model, array $fields): void { - if (Schema::hasTable(config('twill.related_table', 'twill_related'))) { + if (!empty($fields['browsers'])) { + $browserNames = collect($fields['browsers'])->each(function ($items, $browserName) use ($model) { + // This will create items or delete them if they are missing + $model->saveRelated($items, $browserName); + })->keys(); + + // Delete all the related items that were emptied + RelatedItem::query()->whereMorphedTo('subject', $model)->whereNotIn('browser_name', $browserNames)->delete(); + } else { $model->clearAllRelated(); - - if (isset($fields['browsers'])) { - Collection::make($fields['browsers'])->each(function ($items, $browserName) use ($model) { - $model->saveRelated($items, $browserName); - }); - } } parent::afterSave($model, $fields); @@ -82,9 +74,7 @@ public function afterDelete(TwillModelContract $object): void $object->medias()->detach(); $object->files()->detach(); - if (Schema::hasTable(config('twill.related_table', 'twill_related'))) { - $object->clearAllRelated(); - } + $object->clearAllRelated(); } public function buildFromCmsArray(array $block, bool $repeater = false): array diff --git a/src/Services/Listings/Columns/Relation.php b/src/Services/Listings/Columns/Relation.php index 686f7af21..9c6c05ec7 100644 --- a/src/Services/Listings/Columns/Relation.php +++ b/src/Services/Listings/Columns/Relation.php @@ -46,7 +46,7 @@ protected function getRenderValue(TwillModelContract $model): string /** @var \Illuminate\Database\Eloquent\Collection $relation */ $model->loadMissing($this->relation); - $relation = $model->getRelation($this->relation); + $relation = collect($model->getRelation($this->relation)); return $relation->pluck($this->field)->join(', '); } diff --git a/src/TwillServiceProvider.php b/src/TwillServiceProvider.php index 56fcd0570..2b67a9cf6 100644 --- a/src/TwillServiceProvider.php +++ b/src/TwillServiceProvider.php @@ -65,7 +65,7 @@ class TwillServiceProvider extends ServiceProvider * * @var string */ - public const VERSION = '3.4.0'; + public const VERSION = '3.4.1'; /** * Service providers to be registered.