How to insert & render media with block editor to frontend?
Hi! i am new to twill CMS and i wanted to ask some help here, any answer would help me alot!
in this case, i am using some block that named carousel and must insert at least 1 media (photo) inside it and i set it up to 5 Images per block that i made.
@twillBlockTitle('Carousel')
@twillBlockIcon('b-carousel')
@twillBlockGroup('app')
<x-twill::medias
name="cover"
label="Cover Image"
:max="5"
/>
but when i used dd to the blade directive {{ $block->image('cover', 'default') }} in my preview file, instead of giving img url, it shows me that it returns some base64 encoded string : "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7"
i've been set my model like this :
use hasMedias;
...
public $mediasParams = [
'cover' => [
'default' => [
[
'name' => 'default',
'ratio' => 16 / 9,
],
],
],
];
and also, when i am trying to put some media inside carousel block and update it through the cms, the medias isn't updated when it refreshed and showing me that "content is saved, All Good" but the actual thing happened isn't saved.
i wonder why this kinda feels wrong here... but i can't tell where.. if this explanation is not enough, i will explain the details! and sorry for my broken english accent!
Peace π
5 Replies
Hello, no problem, we all started somewhere with the Twill.
Your issue is missing block media definitions in the twill config.
You have defined crops for the module medias, but that is not applicable to the blocks inside that module. Block medias have own definitions.
Long story short, use Model definitions for the normal media fields, and for the blocks use the config
block_editor
definitions.
oh, so what i did was the one that used for media form? dayumn ma bad πΏ
and what about render it to the frontend sir? am i doing the right thing? and what if it has 5 medias inside the block? am i have to use foreach($block->image('cover', 'default' as $medias) and then in the src value, fill the value with {{ $medias }} ? π
Yeah, something like that.
For multiple medias with one crop, you have
$block->images('cover', 'default)
You can always use @dd($block->images('cover', 'default))
to see what you get and how to render stuff.the medias inside the block, its now saved, i am inserting up to 5 medias and keep saved when i refreshed.
TYSM sir!
but when i dump & die the suggestion like the sir kalle told before, it shows me an empty array
[] // DD response from resources\views/site/blocks/hero.blade.php
is there any missing configuration at rendering the block?
Do you have matching block names (form & render blade file)?
Btw I dropped using blade forms a long time ago, I use class blocks which only have render blade and in which you can define blade render file name.