Skip to content

Commit

Permalink
bugfix for use of ContentBlocks with GalleryField
Browse files Browse the repository at this point in the history
  • Loading branch information
reinvanoyen committed Jan 13, 2023
1 parent e05af4a commit 70b7e44
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/Cmf.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public function __construct(ModuleRegistry $modules)
*/
public function getVersion(): string
{
return '0.1.27';
return '0.1.28';
}

/**
Expand Down
26 changes: 22 additions & 4 deletions src/Components/ContentBlocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use ReinVanOyen\Cmf\Http\Resources\ContentBlockResource;
use ReinVanOyen\Cmf\Http\Resources\ModelResource;
use ReinVanOyen\Cmf\RelationshipMetaGuesser;
Expand Down Expand Up @@ -104,6 +105,12 @@ public function save(Model $model, Request $request)
});
}

/**
* @param Model $model
* @param $byId
* @param $byOrder
* @return void
*/
private function removeBlocks(Model $model, $byId, $byOrder)
{
$foreignModelClassname = $this->meta::getModel();
Expand All @@ -118,16 +125,26 @@ private function removeBlocks(Model $model, $byId, $byOrder)
}
}

/**
* @param Model $model
* @return void
*/
private function fixOrderOfBlocks(Model $model)
{
// Fix the orders of the remaining items
$currentItems = $model->{$this->getName()}()->orderBy($this->foreignOrderColumn)->get();

foreach ($currentItems as $index => $currentItem) {
$currentItem->{$this->foreignOrderColumn} = $index;
$currentItem->save();
}
}

/**
* @param Model $model
* @param $items
* @return void
*/
private function updateOrCreateBlocks(Model $model, $items)
{
$foreignModelClassname = $this->meta::getModel();
Expand All @@ -141,15 +158,16 @@ private function updateOrCreateBlocks(Model $model, $items)
$components = $this->blocks[$type]['components'];

// Find the model by order or create a new one
$foreignModel = $model->{$this->getName()}()->where($this->foreignOrderColumn, $order)->first() ?: new $foreignModelClassname();

$newRequest = new Request();
$newRequest->merge($item);
$foreignModel = $model->{$this->getName()}()->where('id', $id)->first() ?: new $foreignModelClassname();

// Save the type column with the new type
$foreignModel->{$this->foreignTypeColumn} = $type;
$foreignModel->{$this->foreignOrderColumn} = $order;

// Create a new request with the data to save to the component
$newRequest = new Request();
$newRequest->merge($item);

// Save the fields of this blocks to the model
foreach ($components as $component) {
$component->save($foreignModel, $newRequest);
Expand Down
6 changes: 6 additions & 0 deletions src/Components/GalleryField.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace ReinVanOyen\Cmf\Components;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Log;
use ReinVanOyen\Cmf\Http\Resources\MediaFileResource;
use ReinVanOyen\Cmf\Http\Resources\ModelResource;
use ReinVanOyen\Cmf\Support\Str;
Expand Down Expand Up @@ -96,7 +97,12 @@ public function save(Model $model, $request)
}

$model::saved(function ($model) use ($fileIds) {

// Sync the file ids to the relationship
$model->{$this->name}()->sync($fileIds);

// Forget about the event listener we just registered, so it only fires once
$model::flushEventListeners();
});
}
}
Expand Down

0 comments on commit 70b7e44

Please sign in to comment.