Completed
Push — master ( 72b93c...30f901 )
by Nicolaas
09:37 queued 09:33
created

ElementToc::getCMSFields()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 6
nc 1
nop 0
dl 0
loc 13
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace Sunnysideup\ElementalToc;
4
5
use DNADesign\Elemental\Models\ElementContent;
6
use SilverStripe\Forms\FieldList;
7
use SilverStripe\Forms\HTMLEditor\HTMLEditorField;
8
use SilverStripe\Forms\LiteralField;
9
10
/**
11
 * Class \Sunnysideup\ElementalToc\ElementToc
12
 *
13
 * @property bool $WithNumbers
14
 */
15
class ElementToc extends ElementContent
16
{
17
    private static $icon = 'font-icon-list';
18
19
20
    private static $table_name = 'ElementToc';
21
22
    private static $singular_name = 'table of contents';
23
24
    private static $plural_name = 'tables of contents';
25
26
    private static $description = 'Table of Contents for Blocks';
27
28
    private static $db = [
29
        'WithNumbers' => 'Boolean',
30
    ];
31
32
    /**
33
     * Re-title the HTML field to Content
34
     *
35
     * {@inheritDoc}
36
     */
37
    public function getCMSFields()
38
    {
39
        $this->beforeUpdateCMSFields(function (FieldList $fields) {
40
            /** @var HTMLEditorField $editorField */
41
            $fields->removeByName('HTML');
42
            $fields->addFieldsToTab(
43
                'Root.Main',
44
                [
45
                    LiteralField::create('TOC', $this->getHTML())
46
                ]
47
            );
48
        });
49
        return parent::getCMSFields();
50
    }
51
52
    protected function getToc()
53
    {
54
        return $this->Parent()
55
            ->Elements()
56
            ->exclude(['ID' => $this->ID])
57
            ->filter(['Sort:GreaterThanOrEqual' => $this->Sort]);
58
    }
59
60
    public function getHTML()
61
    {
62
        return $this->renderWith("SunnySideUp\\ElementalToC\\ElementToC");
63
    }
64
65
    public function getType()
66
    {
67
        return 'Table of Contents';
68
    }
69
}
70