Klief
09/10/2021, 1:28 PMKlief
09/10/2021, 1:31 PMKlief
09/10/2021, 1:31 PMifox
09/10/2021, 1:36 PMpatrick
09/10/2021, 1:43 PMHữu Hiếu
09/10/2021, 2:55 PMifox
09/10/2021, 2:57 PMifox
09/10/2021, 2:58 PMSami
09/10/2021, 3:21 PMDefenestrația
09/10/2021, 3:51 PMifox
09/10/2021, 3:54 PMDefenestrația
09/10/2021, 5:36 PMuser
09/10/2021, 8:29 PMpublic function getContentsAttribute(){
$blocks = [];
foreach ($this->blocks()->orderBy('position')->get() as $block){
$current =
['type' => $block->type]
;
if($block->type == 'image'){
$current['content'] = ['image' => $block->imagesWithCrops('image')];
}elseif($block->type == 'galeriaImgTexto'){
$current['content'] = ['itens' => []];
foreach ($block->children as $child){
$currentChild = $child->content;
$currentChild['images'] = $child->imagesWithCrops('image');
$current['content']['itens'][] = $currentChild;
}
}
else{
$current['content'] = $block->content;
}
$blocks[]=$current;
}
return $blocks;
}
pboivin
09/10/2021, 8:48 PMuser
09/10/2021, 8:50 PMpboivin
09/10/2021, 9:01 PM$blocks->media
to query all Media
items attached to your blocks, but this may not be exactly what you need. It's a bit low level...pboivin
09/10/2021, 9:06 PM$blockSerializers = [
'image' => ImageSerializer::class,
'galeriaImgTexto' => GaleriaImgTextoSerializer::class,
...
];
if ($serializer = $blockSerializers[$block->type] ?? null) {
return (new $serializer)->serialize($block);
}
return null;
I don't know... this is just a quick "sketch" 🙂ifox
09/10/2021, 9:09 PMphp
public function getContentsAttribute()
{
return $this->blocks->where('parent_id', null)->map(function ($block) {
$adapterMethodName = Str::camel($block->type) . 'ToApi';
return $this->$adapterMethodName($block);
});
}
// let's say you have a block named "quote"
private function quoteToApi($block)
{
return [
'text' => $block->translatedInput('quote'),
'author' => $block->input('author'),
];
}
// and so on for your other blocks
ifox
09/10/2021, 9:11 PMuser
09/10/2021, 9:13 PMuser
09/10/2021, 9:13 PMuser
09/10/2021, 9:14 PMuser
09/10/2021, 9:15 PMuser
09/10/2021, 9:15 PMagnonym
09/11/2021, 8:54 AMpboivin
09/11/2021, 3:22 PMHandleFieldsGroups
is not quite optimized for translations.pboivin
09/11/2021, 3:23 PMphp
protected $fillable = [
// ...
'header',
];
protected $casts = [
'header' => 'array',
];
// ...
public function headerField($name)
{
if ($value = $this->header[$name] ?? null) {
$value = $value[app()->getLocale()];
}
return $value;
}
In your views:
{{ $item->headerField('header_title') }}
... something like that. This could be improved 🙂agnonym
09/11/2021, 3:39 PMHandleTranslations
Trait needs to have this structure:
"meta_title" => [
"fr" => null
"en" => null
]
to map it into this structure:
"fr" => [
"meta_title" => null
]
"en" => [
"meta_title" => null
]
I'll have to make more tests and see how to handle this!
But does it work for you to define the attribute cast to array and having no error in admin edition with the HandleFieldsGroups
Trait?pboivin
09/11/2021, 3:42 PMpboivin
09/11/2021, 3:43 PMgetFormFieldsHandleFieldsGroups()
, as you noted.