SimpleHtmlEditorField::FieldHolder()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 10
nc 4
nop 1
dl 0
loc 16
ccs 11
cts 11
cp 1
crap 3
rs 9.9332
c 1
b 0
f 0
1
<?php
2
3
namespace Dynamic\Jobs\Forms;
4
5
use SilverStripe\Forms\TextareaField;
6
use SilverStripe\ORM\FieldType\DBHTMLText;
7
use SilverStripe\View\Requirements;
8
9
/**
10
 * Class SimpleHtmlEditorField
11
 *
12
 * Originally from https://github.com/unclecheese/silverstripe-bootstrap-forms
13
 * Should be replaced when/if bootstrap forms updates
14
 */
15
class SimpleHtmlEditorField extends TextareaField
16
{
17
    /**
18
     * @var string The default buttons to show on the editor
19
     */
20
    public static $default_buttons = "bold,italic,bullist,link,formatselect";
21
22
    /**
23
     * @var string The default formats to show in the format dropdown
24
     */
25
    public static $default_blockformats = "p,h3,h4";
26
27
    /**
28
     * Sets the buttons for this HTML editor
29
     *
30
     * @param string $buttons The buttons to show
31
     *
32
     * @return SimpleHtmlEditorField
33
     */
34 2
    public function setButtons($buttons)
35
    {
36 2
        return $this->setAttribute('data-buttons', $buttons);
37
    }
38
39
    /**
40
     * Sets the available block formats for this HTML Editor
41
     *
42
     * @param string $formats The formats to show
43
     *
44
     * @return SimpleHtmlEditorField
45
     */
46 2
    public function setBlockFormats($formats)
47
    {
48 2
        return $this->setAttribute('data-blockformats', $formats);
49
    }
50
51
    /**
52
     * Sets the default CSS for the editor, i.e. for typography
53
     *
54
     * @param string $css The path to the CSS document
55
     *
56
     * @return SimpleHtmlEditorField
57
     */
58
    public function setCSS($css)
59
    {
60
        return $this->setAttribute('data-css', $css);
61
    }
62
63
    /**
64
     * Builds the form field, includes JavaScript, and sets defaults
65
     *
66
     * @param array $attributes The attributes to include on the form field
67
     *
68
     * @return DBHTMLText
69
     */
70 2
    public function FieldHolder($attributes = [])
71
    {
72 2
        Requirements::javascript("silverstripe/admin: thirdparty/jquery/jquery.min.js");
73 2
        Requirements::javascript("silverstripe/admin: thirdparty/tinymce/jquery.tinymce.min.js");
74 2
        Requirements::javascript("silverstripe/admin: thirdparty/tinymce/tinymce.min.js");
75 2
        Requirements::javascript("dynamic/silverstripe-jobs: javascript/simple_HTML_editor.js");
76
77 2
        if (!$this->getAttribute('data-buttons')) {
78 2
            $this->setButtons(self::$default_buttons);
79
        }
80 2
        if (!$this->getAttribute('data-blockformats')) {
81 2
            $this->setBlockFormats(self::$default_blockformats);
82
        }
83 2
        $this->addExtraClass('wysiwyg');
84
85 2
        return parent::FieldHolder($attributes);
86
    }
87
}
88