可翻译斜纹中继器

发布于 2025-01-13 02:34:42 字数 4460 浏览 0 评论 0原文

我在 Laravel 中使用斜纹布

我需要一个像这样的可翻译中继器 可翻译中继器

因此,我创建了中继器模型,

php artisan twill:make:module TechnologyBenefits -P

如果我没有启用翻译模块并从表单中禁用 translate 键,则一切正常。

我不明白如何通过翻译来实现这一点。

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

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

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;
        }
    }

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

I'm using twill for laravel

I need to have a translatable repeater like this
translatable repeater

So i created the repeater 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
emptydb
But the editor still empty

Instead if I try to update my model without a row this error appear

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

I save add in my model a column named "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

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

Now I need to populate field with 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 internet I can't find anything that can explain to me how to have translatable repeaters

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夜巴黎 2025-01-20 02:34:42

看起来 jsonRepeaters 是可行的方法,显然 这个家伙 实现了它。我自己使用 jsonRepeaters 功能,并且还必须在模型的存储库上设置以下内容(请在 文档)。

protected $jsonRepeaters = ['my_json_field'];

Seems like jsonRepeaters is the way to go and apparently 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'];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文