Can you please tell me how to work with the getData method in the block?

Good evening guys! Can you please tell me how to work with the getData method in the block? I have a translatable field where I need to get the last word and insert it separately into a block template. public function getData(array $data, \A17\Twill\Models\Block $block): array { $data = parent::getData($data, $block); $words = explode(' ', $block->translatedinput('title ')); **$data['last_word']** = array_pop($words); return $data; } And here I need to get this element: <h1 class="main-section_title"> {!! $block->translatedinput('title') !!}<br/><span class="text-accent-01">**{!! $block->translatedinput(last_word)** !!}</span></h1>
7 Replies
savchuk.ivan
savchuk.ivan•2y ago
I need to substitute $data['last_word'] (I wanted to make it bold, but it didn't work😃 )
Hro
Hro•2y ago
Hey @savchuk.ivan , what error are you receiving?
savchuk.ivan
savchuk.ivan•2y ago
@Harings Rob Good evening. There are no errors. It just doesn't display the element. I have a suspicion that I incorrectly form the data.
ifox
ifox•2y ago
<h1 class="main-section_title"> {!! $data['all_words_except_last'] !!}<br/><span class="text-accent-01">{!! $data['last_word'] !!}</span></h1>
<h1 class="main-section_title"> {!! $data['all_words_except_last'] !!}<br/><span class="text-accent-01">{!! $data['last_word'] !!}</span></h1>
$block->translatedinput('title') will always return the whole thing, so you need to return 2 different variables to achieve what you want, I think
savchuk.ivan
savchuk.ivan•2y ago
@ifox message: "Undefined variable $data Now it throws an error.
ifox
ifox•2y ago
my bad, you should directly use the variable eg, $last_word
savchuk.ivan
savchuk.ivan•2y ago
@ifox Thank you very much. Now everything works correctly.