Completed
Push — master ( d78442...d3ce5f )
by Jason
03:09
created

src/Elements/ElementAccordion.php (7 issues)

Severity
1
<?php
2
3
namespace Dynamic\Elements\Accordion\Elements;
4
5
use DNADesign\Elemental\Models\BaseElement;
6
use Dynamic\Elements\Accordion\Model\AccordionPanel;
7
use SilverStripe\Forms\FieldList;
8
use SilverStripe\Forms\GridField\GridField;
9
use SilverStripe\Forms\GridField\GridFieldAddExistingAutocompleter;
10
use SilverStripe\Forms\GridField\GridFieldDeleteAction;
11
use SilverStripe\ORM\FieldType\DBField;
12
use SilverStripe\ORM\FieldType\DBHTMLText;
13
use Symbiote\GridFieldExtensions\GridFieldOrderableRows;
14
15
/**
16
 * Class ElementAccordion
17
 * @package Dynamic\Elements\Accordion\Elements
18
 *
19
 * @property string $Content
20
 *
21
 * @method \SilverStripe\ORM\HasManyList Panels()
22
 */
23
class ElementAccordion extends BaseElement
24
{
25
    /**
26
     * @var string
27
     */
28
    private static $icon = 'accordion-icon';
0 ignored issues
show
The private property $icon is not used, and could be removed.
Loading history...
29
30
    /**
31
     * @var string
32
     */
33
    private static $singular_name = 'Accordion';
0 ignored issues
show
The private property $singular_name is not used, and could be removed.
Loading history...
34
35
    /**
36
     * @var string
37
     */
38
    private static $plural_name = 'Accordions';
0 ignored issues
show
The private property $plural_name is not used, and could be removed.
Loading history...
39
40
    /**
41
     * @var string
42
     */
43
    private static $table_name = 'ElementAccordion';
0 ignored issues
show
The private property $table_name is not used, and could be removed.
Loading history...
44
45
    /**
46
     * @var string
47
     */
48
    private static $description = 'A collapsing list of content';
0 ignored issues
show
The private property $description is not used, and could be removed.
Loading history...
49
50
    /**
51
     * @var array
52
     */
53
    private static $db = [
0 ignored issues
show
The private property $db is not used, and could be removed.
Loading history...
54
        'Content' => 'HTMLText',
55
    ];
56
57
    /**
58
     * @var array
59
     */
60
    private static $has_many = array(
0 ignored issues
show
The private property $has_many is not used, and could be removed.
Loading history...
61
        'Panels' => AccordionPanel::class,
62
    );
63
64
    /**
65
     * @return FieldList
66
     */
67
    public function getCMSFields()
68
    {
69 1
        $this->beforeUpdateCMSFields(function ($fields) {
70
            /* @var FieldList $fields */
71 1
            $fields->removeByName(array(
72 1
                'Sort',
73
            ));
74
75 1
            $fields->dataFieldByName('Content')
76 1
                ->setRows(8);
77
78 1
            if ($this->ID) {
79
                /** @var GridField $panels */
80 1
                $panels = $fields->dataFieldByName('Panels');
81 1
                $panels->setTitle(_t(__CLASS__.'.Panels', 'Panels'));
82
83 1
                $config = $panels->getConfig();
84 1
                $config->addComponent(new GridFieldOrderableRows('Sort'));
85 1
                $config->removeComponentsByType(GridFieldAddExistingAutocompleter::class);
86 1
                $config->removeComponentsByType(GridFieldDeleteAction::class);
87
            }
88 1
        });
89
90 1
        return parent::getCMSFields();
91
    }
92
93
    /**
94
     * @return DBHTMLText
95
     */
96 1
    public function ElementSummary()
97
    {
98 1
        return DBField::create_field('HTMLText', $this->Content)->Summary(20);
99
    }
100
101
    /**
102
     * @return string
103
     */
104 1
    public function getType()
105
    {
106 1
        return _t(__CLASS__.'.BlockType', 'Accordion');
107
    }
108
}
109