Skip to content

Commit 7bd0023

Browse files
authored
Merge branch '3.x' into fix-slugs
2 parents 16b67a4 + 53f59c3 commit 7bd0023

File tree

9 files changed

+45
-46
lines changed

9 files changed

+45
-46
lines changed

CHANGELOG.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,17 @@
22

33
All notable changes to `twill` will be documented in this file.
44

5+
## 3.4.1
6+
7+
### Improved
8+
9+
- 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
10+
11+
### Fixed
12+
13+
- Fix #2671: 3.4.0 regression on related browsers previews by [@Tofandel](https://github.com/Tofandel) in https://github.com/area17/twill/pull/2672
14+
- 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
15+
516
## 3.4.0
617

718
### Added

config/file_library.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
| - 'A17\Twill\Services\FileLibrary\Disk'
1919
|
2020
*/
21-
'disk' => 'twill_file_library',
21+
'disk' => env('FILE_LIBRARY_DISK', 'twill_file_library'),
2222
'endpoint_type' => env('FILE_LIBRARY_ENDPOINT_TYPE', 'local'),
2323
'cascade_delete' => env('FILE_LIBRARY_CASCADE_DELETE', false),
2424
'local_path' => env('FILE_LIBRARY_LOCAL_PATH', 'uploads'),

config/media_library.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
| - 'A17\Twill\Services\MediaLibrary\Local'
2121
|
2222
*/
23-
'disk' => 'twill_media_library',
23+
'disk' => env('MEDIA_LIBRARY_DISK', 'twill_media_library'),
2424
'endpoint_type' => env('MEDIA_LIBRARY_ENDPOINT_TYPE', 'local'),
2525
'cascade_delete' => env('MEDIA_LIBRARY_CASCADE_DELETE', false),
2626
'local_path' => env('MEDIA_LIBRARY_LOCAL_PATH', 'uploads'),

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@area17/twill",
3-
"version": "3.4.0",
3+
"version": "3.4.1",
44
"private": true,
55
"scripts": {
66
"inspect": "vue-cli-service inspect --mode production",

src/Repositories/Behaviors/HandleRevisions.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -157,11 +157,9 @@ public function hydrateRelatedBrowsers(TwillModelContract $object, array $fields
157157

158158
foreach ($relatedBrowsers as $browser) {
159159
$browserField = $fields['browsers'][$browser['browserName']] ?? [];
160-
160+
$position = 1;
161161
foreach ($browserField as $values) {
162-
$position = 1;
163-
164-
$relatedBrowserItems->push(RelatedItem::make([
162+
$relatedBrowserItems->push(new RelatedItem([
165163
'subject_id' => $object->getKey(),
166164
'subject_type' => $object->getMorphClass(),
167165
'related_id' => $values['id'],

src/Repositories/BlockRepository.php

Lines changed: 25 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,12 @@
55
use A17\Twill\Facades\TwillBlocks;
66
use A17\Twill\Models\Block;
77
use A17\Twill\Models\Contracts\TwillModelContract;
8+
use A17\Twill\Models\RelatedItem;
89
use A17\Twill\Repositories\Behaviors\HandleFiles;
910
use A17\Twill\Repositories\Behaviors\HandleMedias;
1011
use A17\Twill\Services\Blocks\Block as BlockConfig;
1112
use Illuminate\Config\Repository as Config;
1213
use Illuminate\Support\Collection;
13-
use Illuminate\Support\Facades\Log;
14-
use Illuminate\Support\Facades\Schema;
15-
use ReflectionException;
1614

1715
class BlockRepository extends ModuleRepository
1816
{
@@ -35,42 +33,36 @@ public function getCrops(string $role): array
3533

3634
public function hydrate(TwillModelContract $model, array $fields): TwillModelContract
3735
{
38-
if (Schema::hasTable(config('twill.related_table', 'twill_related'))) {
39-
$relatedItems = Collection::make();
40-
41-
Collection::make($fields['browsers'])->each(function ($items, $browserName) use (&$relatedItems) {
42-
Collection::make($items)->each(function ($item) use ($browserName, &$relatedItems) {
43-
try {
44-
// @todo: Repository could be null.
45-
$repository = $this->getModelRepository($item['endpointType'] ?? $browserName);
46-
$relatedItems->push(
47-
(object) [
48-
'related' => $repository->getById($item['id']),
49-
'browser_name' => $browserName,
50-
]
51-
);
52-
} catch (ReflectionException $reflectionException) {
53-
Log::error($reflectionException);
54-
}
55-
});
56-
});
57-
58-
$model->setRelation('relatedItems', $relatedItems);
59-
}
36+
$relatedItems = collect($fields['browsers'])
37+
->flatMap(fn($items, $browserName) => collect($items)
38+
->map(fn($item, $position) => new RelatedItem([
39+
'subject_id' => $model->getKey(),
40+
'subject_type' => $model->getMorphClass(),
41+
'related_id' => $item['id'],
42+
'related_type' => $item['endpointType'],
43+
'browser_name' => $browserName,
44+
'position' => $position,
45+
])));
46+
47+
$model->setRelation('relatedItems', $relatedItems);
48+
$model->loadMissing('relatedItems.related');
6049

6150
return parent::hydrate($model, $fields);
6251
}
6352

53+
/** @param Block $model */
6454
public function afterSave(TwillModelContract $model, array $fields): void
6555
{
66-
if (Schema::hasTable(config('twill.related_table', 'twill_related'))) {
56+
if (!empty($fields['browsers'])) {
57+
$browserNames = collect($fields['browsers'])->each(function ($items, $browserName) use ($model) {
58+
// This will create items or delete them if they are missing
59+
$model->saveRelated($items, $browserName);
60+
})->keys();
61+
62+
// Delete all the related items that were emptied
63+
RelatedItem::query()->whereMorphedTo('subject', $model)->whereNotIn('browser_name', $browserNames)->delete();
64+
} else {
6765
$model->clearAllRelated();
68-
69-
if (isset($fields['browsers'])) {
70-
Collection::make($fields['browsers'])->each(function ($items, $browserName) use ($model) {
71-
$model->saveRelated($items, $browserName);
72-
});
73-
}
7466
}
7567

7668
parent::afterSave($model, $fields);
@@ -82,9 +74,7 @@ public function afterDelete(TwillModelContract $object): void
8274
$object->medias()->detach();
8375
$object->files()->detach();
8476

85-
if (Schema::hasTable(config('twill.related_table', 'twill_related'))) {
86-
$object->clearAllRelated();
87-
}
77+
$object->clearAllRelated();
8878
}
8979

9080
public function buildFromCmsArray(array $block, bool $repeater = false): array

src/Services/Listings/Columns/Relation.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ protected function getRenderValue(TwillModelContract $model): string
4646

4747
/** @var \Illuminate\Database\Eloquent\Collection $relation */
4848
$model->loadMissing($this->relation);
49-
$relation = $model->getRelation($this->relation);
49+
$relation = collect($model->getRelation($this->relation));
5050

5151
return $relation->pluck($this->field)->join(', ');
5252
}

src/TwillServiceProvider.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ class TwillServiceProvider extends ServiceProvider
6565
*
6666
* @var string
6767
*/
68-
public const VERSION = '3.4.0';
68+
public const VERSION = '3.4.1';
6969

7070
/**
7171
* Service providers to be registered.

0 commit comments

Comments
 (0)