简体   繁体   English

斜纹中继器可翻译

[英]Twill repeaters translatable

I'm using twill for laravel我正在为 laravel 使用斜纹布

I need to have a translatable repeater like this我需要一个像这样的可翻译中继器可翻译中继器

So i created the repeater model所以我创建了中继器 model

php artisan twill:make:module TechnologyBenefits -P

If I do not enabled translation module and disable from form the translate key all works.如果我没有启用翻译模块并从翻译键中禁用所有功能。

I don't understand how to make this work with the translations.我不明白如何使用翻译来完成这项工作。

If I try to update my model (if there is a row repeater in db) with translation enabled this is what i see in db如果我尝试在启用翻译的情况下更新我的 model(如果数据库中有行转发器),这就是我在数据库中看到的空数据库 But the editor still empty但是编辑器还是空的

Instead if I try to update my model without a row this error appear相反,如果我尝试在没有一行的情况下更新我的 model,则会出现此错误

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"

So i'm also trying to use jsonRepeaters所以我也在尝试使用 jsonRepeaters

I save add in my model a column named "field_repeaters"我在我的 model 中保存了一个名为“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);
}

This save in field_repeater an array with all the repeaters and translations这在 field_repeater 中保存了一个包含所有转发器和翻译的数组

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

Now I need to populate field with getFormFields现在我需要用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;
        }
    }

I don't understand how to create the translated field correctly, through the inte.net I can't find anything that can explain to me how to have translatable repeaters我不明白如何通过 inte.net 正确创建翻译字段我找不到任何可以向我解释如何拥有可翻译中继器的内容

Seems like jsonRepeaters is the way to go and apparently this dude achieved it.似乎 jsonRepeaters 是通往 go 的方式,显然这个家伙实现了它。 I use jsonRepeaters feature myself and I also had to set the following on my model's repository (note about it in the docs ).我自己使用 jsonRepeaters 功能,我还必须在我的模型存储库中设置以下内容(在文档中注意它)。

protected $jsonRepeaters = ['my_json_field'];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM