可翻译斜纹中继器
我在 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;
}
}
我不明白如何正确创建翻译字段, 通过互联网我找不到任何可以向我解释如何拥有可翻译中继器的内容
I'm using twill for laravel
I need to have a translatable repeater like this
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
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看起来 jsonRepeaters 是可行的方法,显然 这个家伙 实现了它。我自己使用 jsonRepeaters 功能,并且还必须在模型的存储库上设置以下内容(请在 文档)。
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).