ShowImageType   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 5
eloc 10
dl 0
loc 32
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getParent() 0 2 1
A configureOptions() 0 4 1
A buildView() 0 19 3
1
<?php
2
3
namespace App\Form\Type;
4
5
use Symfony\Component\Form\AbstractType;
6
use Symfony\Component\Form\Extension\Core\Type\TextType;
7
use Symfony\Component\Form\FormInterface;
8
use Symfony\Component\Form\FormView;
9
use Symfony\Component\OptionsResolver\OptionsResolver;
10
use Symfony\Component\PropertyAccess\PropertyAccess;
11
12
class ShowImageType extends AbstractType{
13
14
    public function configureOptions(OptionsResolver $resolver)
15
    {
16
        // makes it legal for our field to have an image_property option
17
        $resolver->setDefined(['image_property']);
18
    }
19
20
    public function buildView(FormView $view, FormInterface $form, array $options)
21
    {
22
23
        if(isset($options['image_property'])){
24
25
                // this will be whatever class/entity is bound to your form (e.g. Media)
26
                $parentData = $form->getParent()->getData();
27
28
                $imageUrl = null;
29
30
                if (null !== $parentData) {
31
                    $accessor = PropertyAccess::createPropertyAccessor();
32
33
                    $imageUrl = $accessor->getValue($parentData, $options['image_property']);
34
35
                }
36
37
            // sets an "image_url" variable that will be available when rendering this field
38
            $view->vars['image_url'] = $imageUrl;
39
        }
40
    }
41
42
    public function getParent(){
43
        return TextType::class;
44
    }
45
}