Skip to content

Commit

Permalink
Merge branch '3.x' into fix-slugs
Browse files Browse the repository at this point in the history
  • Loading branch information
Tofandel authored Nov 20, 2024
2 parents 16b67a4 + 53f59c3 commit 7bd0023
Show file tree
Hide file tree
Showing 9 changed files with 45 additions and 46 deletions.
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion config/file_library.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand Down
2 changes: 1 addition & 1 deletion config/media_library.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
6 changes: 2 additions & 4 deletions src/Repositories/Behaviors/HandleRevisions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
Expand Down
60 changes: 25 additions & 35 deletions src/Repositories/BlockRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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);
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/Services/Listings/Columns/Relation.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(', ');
}
Expand Down
2 changes: 1 addition & 1 deletion src/TwillServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 7bd0023

Please sign in to comment.