Completed
Pull Request — master (#433)
by
unknown
02:18
created

ImageFormFactory::getForm()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 17
rs 9.2
cc 4
eloc 11
nc 2
nop 3
1
<?php
2
3
namespace SilverStripe\AssetAdmin\Forms;
4
5
use SilverStripe\Forms\DropdownField;
6
use SilverStripe\Forms\FieldGroup;
7
use SilverStripe\Forms\Tab;
8
use SilverStripe\Forms\TextField;
9
use SilverStripe\Control\Controller;
10
11
class ImageFormFactory extends FileFormFactory
12
{
13
    protected function getSpecsMarkup($record)
14
    {
15
        if (!$record || !$record->exists()) {
16
            return null;
17
        }
18
        // Add dimensions to specs
19
        $dimensions = $record->getDimensions() ? $record->getDimensions() . 'px,' : '';
20
        return sprintf(
21
            '<div class="editor__specs">%s %s %s</div>',
22
            $dimensions,
23
            $record->getSize(),
24
            $this->getStatusFlagMarkup($record)
25
        );
26
    }
27
28
    protected function getFormFieldAttributesTab($record, $context = [])
29
    {
30
        /** @var Tab $tab */
31
        $tab = parent::getFormFieldAttributesTab($record, $context);
32
33
        $alignments = array(
34
            'leftAlone' => _t('AssetAdmin.AlignmentLeftAlone', 'On the left, on its own.'),
35
            'center' => _t('AssetAdmin.AlignmentCenter', 'Centered, on its own.'),
36
            'left' => _t('AssetAdmin.AlignmentLeft', 'On the left, with text wrapping around.'),
37
            'right' => _t('AssetAdmin.AlignmentRight', 'On the right, with text wrapping around.'),
38
        );
39
40
        $tab->push(
41
            DropdownField::create('Alignment', _t('AssetAdmin.Alignment', 'Alignment'), $alignments)
42
        );
43
        $tab->push(
44
            FieldGroup::create(
45
                _t('AssetAdmin.ImageSpecs', 'Dimensions'),
46
                TextField::create(
47
                    'InsertWidth',
48
                    _t('AssetAdmin.ImageWidth', 'Width')
49
                )
50
                    ->setMaxLength(5)
51
                    ->addExtraClass('flexbox-area-grow'),
52
                TextField::create(
53
                    'InsertHeight',
54
                    _t('AssetAdmin.ImageHeight', 'Height')
55
                )
56
                    ->setMaxLength(5)
57
                    ->addExtraClass('flexbox-area-grow')
58
            )
59
            ->addExtraClass('fieldgroup--fill-width')
60
            ->setName('Dimensions')
61
        );
62
63
        $tab->insertBefore(
64
            'Caption',
65
            TextField::create('AltText', _t('AssetAdmin.AltText', 'Alternative text (alt)'))
66
                ->setDescription(_t(
67
                    'AssetAdmin.AltTextDescription',
68
                    'Shown to screen readers or if image can\'t be displayed'
69
                ))
70
        );
71
        $tab->insertAfter(
72
            'AltText',
73
            TextField::create('TitleTooltip', _t('AssetAdmin.TitleTooltip', 'Title text (tooltip)'))
74
                ->setDescription(_t('AssetAdmin.TitleTooltipDescription', 'For additional information about the image'))
75
                ->setValue($record->Title)
76
        );
77
78
        return $tab;
79
    }
80
81
    /**
82
     * @param Controller $controller
83
     * @param string $name
84
     * @param array $context
85
     * @return Form
86
     */
87
    public function getForm(Controller $controller, $name = FormFactory::DEFAULT_NAME, $context = [])
88
    {
89
        $form = parent::getForm($controller, $name, $context);
90
        $dimensions = $form->Fields()->fieldByName('Editor.Placement.Dimensions');
91
        $widthField = $form->Fields()->dataFieldByName('InsertWidth');
92
        $heightField = $form->Fields()->dataFieldByName('InsertHeight');
93
        if ($dimensions && $widthField && $heightField) {
94
            $dimensions->setSchemaComponent('ProportionConstraintField');
95
            $dimensions->setSchemaState([
96
                'data' => [
97
                    'ratio' => $widthField->dataValue() / $heightField->dataValue()
98
                ]
99
            ]);
100
        }
101
102
        return $form;
103
    }
104
}
105