Skip to content

Commit

Permalink
Fixed Layout duplication (cloning fields properly)
Browse files Browse the repository at this point in the history
  • Loading branch information
toonvandenbos authored Jun 17, 2020
1 parent 453f03e commit d5ff8d5
Showing 1 changed file with 23 additions and 3 deletions.
26 changes: 23 additions & 3 deletions src/Layouts/Layout.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use ArrayAccess;
use JsonSerializable;
use Laravel\Nova\Fields\Field;
use Laravel\Nova\Fields\FieldCollection;
use Laravel\Nova\Http\Requests\NovaRequest;
use Whitecube\NovaFlexibleContent\Flexible;
Expand Down Expand Up @@ -218,9 +219,9 @@ public function duplicate($key)
*/
public function duplicateAndHydrate($key, array $attributes = [])
{
$fields = array_map(function($field) {
return clone $field;
}, $this->fields->toArray());
$fields = $this->fields->map(function($field) {
return $this->cloneField($field);
});

return new static(
$this->title,
Expand All @@ -231,6 +232,25 @@ public function duplicateAndHydrate($key, array $attributes = [])
);
}

/**
* Create a working field clone instance
*
* @param \Laravel\Nova\Fields\Field $original
* @return \Laravel\Nova\Fields\Field
*/
protected function cloneField(Field $original) {
$field = clone $original;

$callables = ['displayCallback','resolveCallback','fillCallback','requiredCallback'];

foreach ($callables as $callable) {
if(!is_a($field->$callable, \Closure::class)) continue;
$field->$callable = $field->$callable->bindTo($field);
}

return $field;
}

/**
* Resolve fields using given attributes.
*
Expand Down

0 comments on commit d5ff8d5

Please sign in to comment.