Skip to content

Commit df6622d

Browse files
authored
Merge pull request #5772 from Laravel-Backpack/fix-translatable-fields
remove translatable fields from casting and add tests
2 parents b581e36 + eb12f28 commit df6622d

File tree

5 files changed

+109
-1
lines changed

5 files changed

+109
-1
lines changed

src/app/Library/CrudPanel/Traits/Input.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,7 @@ public function decodeJsonCastedAttributes($input, $model = false)
218218
$model = $model ? $model : $this->model;
219219
$fields = $this->getCleanStateFields();
220220
$casted_attributes = $model->getCastedAttributes();
221+
$translatableAttributes = $model->translationEnabled() ? $model->getTranslatableAttributes() : [];
221222

222223
foreach ($fields as $field) {
223224
// Test the field is castable
@@ -226,7 +227,7 @@ public function decodeJsonCastedAttributes($input, $model = false)
226227
$jsonCastables = ['array', 'object', 'json'];
227228
$fieldCasting = $casted_attributes[$field['name']];
228229

229-
if (in_array($fieldCasting, $jsonCastables) && isset($input[$field['name']]) && ! empty($input[$field['name']]) && ! is_array($input[$field['name']])) {
230+
if (in_array($fieldCasting, $jsonCastables) && isset($input[$field['name']]) && ! empty($input[$field['name']]) && ! is_array($input[$field['name']]) && ! in_array($field['name'], $translatableAttributes)) {
230231
try {
231232
$input[$field['name']] = json_decode($input[$field['name']]);
232233
} catch (\Exception $e) {

tests/BaseTestClass.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ protected function getPackageProviders($app)
4141
BackpackServiceProvider::class,
4242
AlertsServiceProvider::class,
4343
TestsServiceProvider::class,
44+
\Spatie\Translatable\TranslatableServiceProvider::class,
4445
];
4546
}
4647

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
namespace Backpack\CRUD\Tests\Unit\CrudPanel;
4+
5+
use Backpack\CRUD\Tests\config\CrudPanel\BaseDBCrudPanel;
6+
use Backpack\CRUD\Tests\config\Models\TranslatableModel;
7+
8+
/**
9+
* @covers Backpack\CRUD\app\Models\Traits\HasTranslatableFields
10+
*/
11+
class TranslatableFieldsTest extends BaseDBCrudPanel
12+
{
13+
/**
14+
* Setup function for each test.
15+
*/
16+
protected function setUp(): void
17+
{
18+
parent::setUp();
19+
20+
$this->crudPanel->setModel(TranslatableModel::class);
21+
22+
$this->crudPanel->field('title');
23+
$this->crudPanel->field('description');
24+
}
25+
26+
public function testFieldsGetsTranslated()
27+
{
28+
$this->crudPanel->create([
29+
'title' => 'english title',
30+
'description' => 'english description',
31+
]);
32+
33+
$model = TranslatableModel::first();
34+
35+
$this->assertEquals('english title', $model->title);
36+
37+
config(['app.locale' => 'fr']);
38+
39+
$this->assertEquals('english title', $model->title);
40+
41+
$this->crudPanel->update(1, ['title' => 'french title']);
42+
43+
$model->refresh();
44+
45+
$this->assertEquals('french title', $model->title);
46+
}
47+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace Backpack\CRUD\Tests\Config\Models;
4+
5+
use Backpack\CRUD\app\Models\Traits\CrudTrait;
6+
use Backpack\CRUD\app\Models\Traits\SpatieTranslatable\HasTranslations;
7+
use Illuminate\Database\Eloquent\Model;
8+
9+
class TranslatableModel extends Model
10+
{
11+
use CrudTrait;
12+
use HasTranslations;
13+
14+
protected $fillable = [
15+
'title',
16+
'description',
17+
];
18+
19+
protected $translatable = [
20+
'title',
21+
'description',
22+
];
23+
24+
public $timestamps = false;
25+
26+
protected $table = 'translatable';
27+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
class CreateTranslatableTable extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*
12+
* @return void
13+
*/
14+
public function up()
15+
{
16+
Schema::create('translatable', function (Blueprint $table) {
17+
$table->increments('id');
18+
$table->json('title');
19+
$table->json('description')->nullable();
20+
});
21+
}
22+
23+
/**
24+
* Reverse the migrations.
25+
*
26+
* @return void
27+
*/
28+
public function down()
29+
{
30+
Schema::drop('translatable');
31+
}
32+
}

0 commit comments

Comments
 (0)