1 | <?php |
||
2 | |||
3 | |||
4 | namespace Riclep\Storyblok\Fields; |
||
5 | |||
6 | |||
7 | use Riclep\Storyblok\Field; |
||
8 | use Riclep\Storyblok\Traits\HasChildClasses; |
||
9 | use Storyblok\RichtextRender\Resolver; |
||
10 | |||
11 | class RichText extends Field |
||
12 | { |
||
13 | use HasChildClasses; |
||
14 | |||
15 | public function init(): void |
||
16 | { |
||
17 | $richtextResolver = new Resolver(); |
||
18 | |||
19 | $content = []; |
||
20 | |||
21 | // Loop through all the nodes looking for a ‘blok’ nodes and convery them to |
||
22 | // the correct Block Class. All other nodes are converted to HTML |
||
23 | foreach ($this->content['content'] as $node) { |
||
24 | if ($node['type'] === 'blok' && isset($node['attrs']['body']) && is_array($node['attrs']['body'])) { |
||
25 | foreach ($node['attrs']['body'] as $blockContent) { |
||
26 | $class = $this->getChildClassName('Block', $blockContent['component']); |
||
27 | $block = new $class($blockContent, $this->block()); |
||
28 | |||
29 | $content[] = $block; |
||
30 | } |
||
31 | } else { |
||
32 | $content[] = $richtextResolver->render(["content" => [$node]]); |
||
33 | } |
||
34 | } |
||
35 | |||
36 | $this->content = collect($content); |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
37 | } |
||
38 | |||
39 | /** |
||
40 | * Converts the data to HTML when printed. If there is an inline Component |
||
41 | * it will use it’s render method. |
||
42 | * |
||
43 | * @return string |
||
44 | */ |
||
45 | public function __toString(): string |
||
46 | { |
||
47 | $html = ""; |
||
48 | |||
49 | foreach ($this->content as $content) { |
||
50 | if (is_string($content)) { |
||
51 | $html .= $content; |
||
52 | } else { |
||
53 | $html .= $content->render(); |
||
54 | } |
||
55 | } |
||
56 | |||
57 | return $html; |
||
58 | } |
||
59 | } |