-
Notifications
You must be signed in to change notification settings - Fork 576
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement a new behavior to allow saving repeaters into json columns
This trait is not intended to replace main repeaters but to give a quick and easy alternative for simple elements where creating a new table might be an overkill. Simply define an array with repeater names on your repository: protected $jsonRepeaters = [ 'REPEATER_NAME_1', 'REPEATER_NAME_2', ... ]
- Loading branch information
1 parent
eb41a1b
commit 85f9630
Showing
1 changed file
with
101 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
<?php | ||
|
||
namespace A17\Twill\Repositories\Behaviors; | ||
|
||
use Carbon\Carbon; | ||
use Illuminate\Support\Arr; | ||
use Illuminate\Support\Collection; | ||
use Illuminate\Support\Str; | ||
|
||
/** | ||
* | ||
* Save repeaters in a json column instead of a new model. | ||
* | ||
* This trait is not intended to replace main repeaters but to give a quick | ||
* and easy alternative for simple elements where creating a new table might be an overkill. | ||
* | ||
* Simply define an array with the repeater names on your repository: | ||
* protected $jsonRepeaters = [ 'REPEATER_NAME_1', 'REPEATER_NAME_2', ... ] | ||
* | ||
* Names must be the same as the ones you added in your `repeaters` attribute on `config\twill.php` | ||
* | ||
* Supported: Input, WYSIWYG, textarea, browsers. | ||
* Not supported: Medias, Files, repeaters. | ||
* | ||
*/ | ||
|
||
trait HandleJsonRepeaters | ||
{ | ||
|
||
/** | ||
* @param \A17\Twill\Models\Model $object | ||
* @param array $fields | ||
* @return string[] | ||
*/ | ||
public function prepareFieldsBeforeSaveHandleJsonRepeaters($object, $fields) | ||
{ | ||
foreach ($this->jsonRepeaters as $repeater) { | ||
if (isset($fields['repeaters'][$repeater])) { | ||
$fields[$repeater] = $fields['repeaters'][$repeater]; | ||
} | ||
} | ||
|
||
return $fields; | ||
} | ||
|
||
/** | ||
* @param \A17\Twill\Models\Model $object | ||
* @param array $fields | ||
* @return string[] | ||
*/ | ||
public function getFormFieldsHandleJsonRepeaters($object, $fields) | ||
{ | ||
|
||
foreach($this->jsonRepeaters as $repeater) { | ||
if (isset($fields[$repeater]) && !empty($fields[$repeater])) { | ||
$fields = $this->getJsonRepeater($fields, $repeater, $fields[$repeater]); | ||
} | ||
} | ||
|
||
return $fields; | ||
} | ||
|
||
public function getJsonRepeater($fields, $repeaterName, $serializedData) { | ||
$repeatersFields = []; | ||
$repeatersBrowsers = []; | ||
$repeatersConfig = config('twill.block_editor.repeaters'); | ||
|
||
|
||
foreach($serializedData as $index => $repeaterItem) { | ||
$id = $repeaterItem['id'] ?? $index; | ||
|
||
$repeaters[] = [ | ||
'id' => $id, | ||
'type' => $repeatersConfig[$repeaterName]['component'], | ||
'title' => $repeatersConfig[$repeaterName]['title'], | ||
]; | ||
|
||
if (isset($repeaterItem['browsers'])) { | ||
foreach ($repeaterItem['browsers'] as $key => $values) { | ||
$repeatersBrowsers["blocks[$id][$key]"] = $values; | ||
} | ||
} | ||
|
||
$itemFields = Arr::except($repeaterItem, ['id', 'repeaters', 'files', 'medias', 'browsers', 'blocks']); | ||
|
||
foreach($itemFields as $index => $value) { | ||
$repeatersFields[] = [ | ||
'name' => "blocks[$id][$index]", | ||
'value' => $value | ||
]; | ||
} | ||
} | ||
|
||
$fields['repeaters'][$repeaterName] = $repeaters; | ||
$fields['repeaterFields'][$repeaterName] = $repeatersFields; | ||
$fields['repeaterBrowsers'][$repeaterName] = $repeatersBrowsers; | ||
|
||
return $fields; | ||
} | ||
|
||
} |