TinyMceType   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 102
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 75
c 1
b 0
f 0
dl 0
loc 102
rs 10
ccs 0
cts 32
cp 0
wmc 6

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getTinyMceDefaultConfiguration() 0 49 1
A getParent() 0 3 1
A configureOptions() 0 28 3
A buildView() 0 10 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace LAG\AdminBundle\Form\Type\TinyMce;
6
7
use Symfony\Component\Form\AbstractType;
8
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
9
use Symfony\Component\Form\FormInterface;
10
use Symfony\Component\Form\FormView;
11
use Symfony\Component\OptionsResolver\Options;
12
use Symfony\Component\OptionsResolver\OptionsResolver;
13
14
use function Symfony\Component\String\u;
15
16
class TinyMceType extends AbstractType
17
{
18
    public function getParent(): string
19
    {
20
        return TextareaType::class;
21
    }
22
23
    public function configureOptions(OptionsResolver $resolver)
24
    {
25
        $resolver
26
            ->setDefaults([
27
                'attr' => [],
28
                'tinymce_options' => [],
29
                'custom_buttons' => [],
30
            ])
31
            ->setAllowedTypes('tinymce_options', 'array')
32
            ->addNormalizer('tinymce_options', function (Options $options, $value) {
33
                $attr = $options->offsetGet('attr');
34
35
                if (\array_key_exists('id', $attr)) {
36
                    $value['selector'] = $attr['id'];
37
                } else {
38
                    $value['selector'] = 'textarea#'.uniqid('tinymce');
39
                }
40
                // Do not use a nested options resolver to allow the user to define only some options and not the
41
                // whole set
42
                return array_replace_recursive($this->getTinyMceDefaultConfiguration(), $value);
43
            })
44
            ->setAllowedTypes('custom_buttons', 'array')
45
            ->addNormalizer('custom_buttons', function (Options $options, $value) {
46
                foreach ($value as $item => $button) {
47
                    $buttonResolver = new OptionsResolver();
48
                    $buttonResolver->setDefaults([
49
                        'event_name' => 'tinymce-button-'.$item,
50
                        'text' => u($item)->title(false)->toString(),
51
                    ]);
52
                }
53
            })
54
        ;
55
    }
56
57
    public function buildView(FormView $view, FormInterface $form, array $options): void
58
    {
59
        $view->vars['id'] = u($options['tinymce_options']['selector'])
60
            ->replace('textarea#', '')
61
            ->replace('#', '')
62
            ->toString()
63
        ;
64
        $view->vars['attr']['data-controller'] = 'tinymce';
65
        $view->vars['attr']['data-options'] = json_encode($options['tinymce_options']);
66
        $view->vars['attr']['data-custom-buttons'] = json_encode($options['custom_buttons']);
67
    }
68
69
    private function getTinyMceDefaultConfiguration(): array
70
    {
71
        return [
72
            'branding' => false,
73
            'language' => 'fr_FR',
74
            'selector' => uniqid('tinymce'),
75
            'toolbar1' => 'insertfile undo redo | styleselect | bold italic | alignleft aligncenter '
76
                .'alignright alignjustify | bullist numlist outdent indent | link ',
77
            'toolbar2' => ' print preview | forecolor backcolor emoticons code',
78
            'image_advtab' => true,
79
            'relative_urls' => false,
80
            'convert_urls' => false,
81
            'theme' => 'silver',
82
            'skin' => 'oxide',
83
            'imagetools_toolbar' => 'rotateleft rotateright | flipv fliph | editimage imageoptions',
84
            'content_css' => [],
85
            'body_class' => 'mceForceColors container',
86
            'browser_spellcheck' => true,
87
            'plugins' => [
88
                'advlist',
89
                'anchor',
90
                'autolink',
91
                'charmap',
92
                'code',
93
                'emoticons',
94
                'fullscreen',
95
                'directionality',
96
                'hr',
97
                'image',
98
                'insertdatetime',
99
                'imagetools',
100
                'media',
101
                'nonbreaking',
102
                'link',
103
                'lists',
104
                'pagebreak',
105
                'print',
106
                'paste',
107
                'preview',
108
                'save',
109
                'searchreplace',
110
                'table',
111
                'textpattern',
112
                'template',
113
                'wordcount',
114
                'visualblocks',
115
                'visualchars',
116
            ],
117
            'height' => 1000,
118
        ];
119
    }
120
}
121