Skip to content

Commit

Permalink
Merge pull request #535 from fritz-c/master
Browse files Browse the repository at this point in the history
Fix: Ensure duplicateAndHydrate works with Fields using static closures
  • Loading branch information
voidgraphics authored Feb 11, 2025
2 parents 68230ed + c6bb15b commit d48d607
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/Layouts/Layout.php
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,14 @@ protected function cloneField(Field $original)
if (! is_a($field->$callable ?? null, \Closure::class)) {
continue;
}
$field->$callable = $field->$callable->bindTo($field);

try {
$field->$callable = $field->$callable->bindTo($field);
} catch (\Throwable $th) {
// Binding an instance to a static closure will fail. Assuming
// that's the cause of the error here, we leave the original
// closure as-is.
}
}

return $field;
Expand Down
37 changes: 37 additions & 0 deletions tests/Unit/Layouts/LayoutTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace Tests\Unit\Layouts;

use PHPUnit\Framework\TestCase;
use Laravel\Nova\Fields\DateTime;
use Laravel\Nova\Fields\Text;
use Whitecube\NovaFlexibleContent\Layouts\Layout;

class LayoutTest extends TestCase
{
public function testDuplicateAndHydrate(): void
{
$layout = new Layout('Test Layout', 'test', [
DateTime::make('Created At'),
Text::make('Name', 'name', static function($resource, $attribute) {
return 'static-name';
}),
]);

// Should not throw any exceptions
$duplicate = $layout->duplicateAndHydrate('keylikegenerated', [
'created_at' => '2023-01-01 00:00:00',
'name' => 'Test'
]);

$this->assertInstanceOf(Layout::class, $duplicate);
$this->assertEquals('keylikegenerated', $duplicate->key());
$this->assertEquals('Test Layout', $duplicate->title());
$this->assertEquals('test', $duplicate->name());

// The text field should resolve the value
$textField = $duplicate->fields()[1];
$textField->resolve(null);
$this->assertEquals('static-name', $textField->value);
}
}

0 comments on commit d48d607

Please sign in to comment.