1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SilverStripe\ThemeFontpicker\Extensions; |
4
|
|
|
|
5
|
|
|
use SilverStripe\Core\Config\Config; |
6
|
|
|
use SilverStripe\Fontpicker\Forms\FontPickerField; |
7
|
|
|
use SilverStripe\Forms\FieldList; |
8
|
|
|
use SilverStripe\ORM\DataExtension; |
9
|
|
|
use SilverStripe\View\Requirements; |
10
|
|
|
|
11
|
|
|
class FontpickerSiteConfigExtension extends DataExtension |
12
|
|
|
{ |
13
|
|
|
private static $db = array( |
|
|
|
|
14
|
|
|
'MainFontFamily' => 'Varchar(50)', |
15
|
|
|
); |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Defines the theme fonts that can be selected via the CMS |
19
|
|
|
* |
20
|
|
|
* @config |
21
|
|
|
* @var array |
22
|
|
|
*/ |
23
|
|
|
protected static $theme_fonts = [ |
24
|
|
|
'nunito-sans' => 'Nunito Sans', |
25
|
|
|
'fira-sans' => 'Fira Sans', |
26
|
|
|
'merriweather' => 'Merriweather', |
27
|
|
|
]; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @param FieldList $fields |
31
|
|
|
*/ |
32
|
|
|
public function updateCMSFields(FieldList $fields) |
33
|
|
|
{ |
34
|
|
|
$this->addFontPicker($fields); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
protected function addFontPicker(FieldList $fields) |
38
|
|
|
{ |
39
|
|
|
$fonts = $this->owner->config()->get('theme_fonts'); |
40
|
|
|
|
41
|
|
|
// Import each font via the google fonts api to render font preview |
42
|
|
|
foreach ($fonts as $fontTitle) { |
43
|
|
|
$fontFamilyName = str_replace(' ', '+', $fontTitle); |
44
|
|
|
Requirements::css("//fonts.googleapis.com/css?family=$fontFamilyName"); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
$fields->addFieldsToTab( |
48
|
|
|
'Root.ThemeOptions', |
49
|
|
|
[ |
50
|
|
|
FontPickerField::create( |
51
|
|
|
'MainFontFamily', |
52
|
|
|
_t( |
53
|
|
|
__CLASS__ . '.MainFontFamily', |
54
|
|
|
'Main font family' |
55
|
|
|
), |
56
|
|
|
$fonts |
57
|
|
|
) |
58
|
|
|
] |
59
|
|
|
); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* If HeaderBackground is not set, assume no theme colours exist and populate some defaults if the colour |
64
|
|
|
* picker is enabled. We don't use populateDefaults() because we don't want SiteConfig to re-populate its own |
65
|
|
|
* defaults. |
66
|
|
|
*/ |
67
|
|
|
public function onBeforeWrite() |
68
|
|
|
{ |
69
|
|
|
if (!$this->owner->MainFontFamily) { |
70
|
|
|
$this->owner->update([ |
71
|
|
|
'MainFontFamily' => Config::inst()->get(FontPickerField::class, 'default_font'), |
72
|
|
|
]); |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|