Passed
Push — master ( 1ea92e...59050e )
by Robbie
02:21 queued 10s
created

FontPickerField   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A Value() 0 3 2
A getSchemaDataDefaults() 0 17 2
A __construct() 0 5 1
1
<?php
2
3
namespace CWP\AgencyExtensions\Forms;
4
5
use SilverStripe\Forms\SingleSelectField;
6
7
class FontPickerField extends SingleSelectField
8
{
9
    /**
10
     * The default value if none is already configured
11
     *
12
     * @var string
13
     */
14
    const DEFAULT_VALUE = 'nunito-sans';
15
16
    public function __construct($name, $title = null, $source = array(), $value = null)
17
    {
18
        parent::__construct($name, $title, $source, $value);
19
20
        $this->addExtraClass('font-picker-field');
21
    }
22
23
    public function getSchemaDataDefaults()
24
    {
25
        $schemaData = parent::getSchemaDataDefaults();
26
27
        $fonts = [];
28
        foreach ($this->getSource() as $css => $title) {
29
            $fonts[] = [
30
                'CSSClass' => $css,
31
                'Title' => $title,
32
            ];
33
        }
34
35
        $schemaData['source'] = $fonts;
36
        $schemaData['name'] = $this->getName();
37
        $schemaData['value'] = $this->Value();
38
39
        return $schemaData;
40
    }
41
42
    public function Value()
43
    {
44
        return parent::Value() ?: self::DEFAULT_VALUE;
45
    }
46
}
47