斜纹中继器可翻译

Twill repeaters translatable

我正在为 laravel

使用斜纹布

我需要一个像这样的可翻译复读机

所以我创建了中继器模型

php artisan twill:make:module TechnologyBenefits -P

如果我不启用翻译模块并禁用 translate 键,则一切正常。

我不明白如何使用翻译来完成这项工作。

如果我尝试在启用翻译的情况下更新我的模型(如果数据库中有行转发器),这就是我在数据库中看到的 但是编辑器还是空的

相反,如果我尝试在没有一行的情况下更新我的模型,则会出现此错误

exception: "TypeError"
file: "/srv/www/mysite/current/vendor/laravel/framework/src/Illuminate/Database/Grammar.php"
line: 136
message: "Illuminate\Database\Grammar::parameterize(): Argument #1 ($values) must be of type array, int given, called in /srv/www/mysite/current/vendor/laravel/framework/src/Illuminate/Database/Query/Grammars/Grammar.php on line 920"

所以我也在尝试使用 jsonRepeaters

我在我的模型中添加了一个名为“field_repeaters”的列

public function afterSave($object, $fields)
{   
    $tech_repeaters = [];
    $tech_repeaters['benefits'] = [
        'title' => collect($fields['repeaters']['info_benefits_repeater'] ?? [])->pluck('title'),
        'description' => collect($fields['repeaters']['info_benefits_repeater'] ?? [])->pluck('desc'),
    ];
    $tech_repeaters['materials'] = [
        'name' => collect($fields['repeaters']['info_materials_repeater'] ?? [])->pluck('name'),
        'description' => collect($fields['repeaters']['info_materials_repeater'] ?? [])->pluck('description'),
        'media' => collect($fields['repeaters']['info_materials_repeater'] ?? [])->pluck('media'),
        'ideal_for' => collect($fields['repeaters']['info_materials_repeater'] ?? [])->pluck('ideal_for'),
    ];
    $tech_repeaters['videos'] = [
        'embed' => collect($fields['repeaters']['info_materials_repeater'] ?? [])->pluck('embed'),
    ];
    $object->field_repeaters = json_encode($tech_repeaters, true);
    $object->save();

    parent::afterSave($object, $fields);
}

这保存在 field_repeater 一个包含所有转发器和翻译的数组中

{'benefit': {'title': {'en' => 'english title', ru => 'ru title'}, 'desc': {'en': 'desc', 'ru': 'desc_ru'}}, ... }

现在我需要用 getFormFields

填充字段
public function getFormFields($object)
{
    $fields = parent::getFormFields($object);

    if(isset($fields['field_repeaters'])){
        $repeaters = json_decode($fields['field_repeaters'], true);
        foreach($repeaters as $repeaterName =>$serializedData){
            // $fields = $this->getJsonRepeater($fields, $repeaterName, $serializedData);
            $repeatersFields = [];
            $repeatersBrowsers = [];
            $repeatersList = app(BlockCollection::class)->getRepeaterList()->keyBy('name');

            foreach ($serializedData as $index => $repeaterItem) {
                $id = $repeaterItem['id'] ?? $index;

                $repeaters[] = [
                    'id' => $id,
                    'type' => $repeatersList[$repeaterName]['component'],
                    'title' => $repeatersList[$repeaterName]['title'],
                    'titleField' => $repeatersList[$repeaterName]['titleField'],
                    'hideTitlePrefix' => $repeatersList[$repeaterName]['hideTitlePrefix'],
                ];

                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']);

                var_dump($itemFields)
                foreach ($itemFields as $index => $value) {
                    $repeatersFields[] = [
                        'name' => "blocks[$id][$index]",
                        'value' => $value,
                    ];
                }
            }

            $fields['repeaters'][$repeaterName] = $repeaters;
            $fields['repeaterFields'][$repeaterName] = $repeatersFields;
            $fields['repeaterBrowsers'][$repeaterName] = $repeatersBrowsers;
        }
    }

我不明白如何正确创建翻译字段, 通过互联网我找不到任何可以向我解释如何拥有可翻译中继器的东西

似乎 jsonRepeaters 是可行的方法,而且显然 this dude achieved it. I use jsonRepeaters feature myself and I also had to set the following on my model's repository (note about it in the docs)。

protected $jsonRepeaters = ['my_json_field'];