Test Setup Failed
Pull Request — master (#197)
by Gorrie
01:07
created

src/ElementalEditor.php (2 issues)

1
<?php
2
3
namespace DNADesign\Elemental;
4
5
use DNADesign\Elemental\Models\ElementalArea;
6
use SilverStripe\Core\Extensible;
7
use SilverStripe\Core\Injector\Injectable;
8
use SilverStripe\Core\Injector\Injector;
9
use SilverStripe\Forms\GridField\GridField;
10
use SilverStripe\Forms\GridField\GridFieldAddExistingAutocompleter;
11
use SilverStripe\Forms\GridField\GridFieldAddNewButton;
12
use SilverStripe\Forms\GridField\GridFieldConfig_RelationEditor;
13
use SilverStripe\Forms\GridField\GridFieldDeleteAction;
14
use SilverStripe\Forms\GridField\GridFieldPageCount;
15
use SilverStripe\Forms\GridField\GridFieldPaginator;
16
use SilverStripe\Forms\GridField\GridFieldSortableHeader;
17
use SilverStripe\Forms\GridField\GridFieldVersionedState;
18
use SilverStripe\Versioned\VersionedGridFieldState\VersionedGridFieldState;
19
use Symbiote\GridFieldExtensions\GridFieldAddNewMultiClass;
20
use Symbiote\GridFieldExtensions\GridFieldOrderableRows;
21
22
class ElementalEditor
23
{
24
    use Extensible;
25
    use Injectable;
26
27
    /**
28
     * @var ElementalArea $area
29
     */
30
    protected $area;
31
32
    /**
33
     * @var string $name
34
     */
35
    protected $name;
36
37
    /**
38
     * By default, no need for a title on the editor. If there is more than one
39
     * area then use `setTitle` to describe.
40
     *
41
     * @var string $title
42
     */
43
    protected $title = '';
44
45
    /**
46
     * @var array $type
47
     */
48
    protected $types = [];
49
50
    /**
51
     * @param string $name
52
     * @param ElementalArea $area
53
     */
54
    public function __construct($name, ElementalArea $area)
55
    {
56
        $this->name = $name;
57
        $this->area = $area;
58
    }
59
60
    /**
61
     * @param array $types
62
     *
63
     * @return $this
64
     */
65
    public function setTypes($types)
66
    {
67
        $this->types = $types;
68
69
        return $this;
70
    }
71
72
    /**
73
     * @return array
74
     */
75
    public function getTypes()
76
    {
77
        $types = $this->types;
78
79
        $this->extend('updateGetTypes', $types);
80
81
        return $types;
82
    }
83
84
    /**
85
     * @return ElementalArea
86
     */
87
    public function getArea()
88
    {
89
        return $this->area;
90
    }
91
92
    /**
93
     * @param string $title
94
     *
95
     * @return $this
96
     */
97
    public function setTitle($title)
98
    {
99
        $this->title = $title;
100
101
        return $this;
102
    }
103
104
    /**
105
     * @return GridField
106
     */
107
    public function getField()
108
    {
109
        $gridField = GridField::create(
110
            $this->name,
0 ignored issues
show
$this->name of type string is incompatible with the type array expected by parameter $args of SilverStripe\View\ViewableData::create(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

110
            /** @scrutinizer ignore-type */ $this->name,
Loading history...
111
            $this->title,
112
            $this->getArea()->Elements(),
113
            $config = GridFieldConfig_RelationEditor::create()
114
                ->removeComponentsByType(array(
115
                    GridFieldAddNewButton::class,
116
                    GridFieldSortableHeader::class,
117
                    GridFieldDeleteAction::class,
118
                    GridFieldPaginator::class,
119
                    GridFieldPageCount::class,
120
                    GridFieldVersionedState::class,
121
                    VersionedGridFieldState::class,
122
                    GridFieldAddExistingAutocompleter::class,
123
                ))
124
                ->addComponent(new GridFieldOrderableRows('Sort'))
125
                // delete elements rather than unlinking them
126
                ->addComponent(new GridFieldDeleteAction(false))
127
        );
128
129
        $gridField->addExtraClass('elemental-editor');
130
131
        if ($this->types) {
0 ignored issues
show
Bug Best Practice introduced by Will Rossiter
The expression $this->types of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
132
            $adder = Injector::inst()->create(GridFieldAddNewMultiClass::class, 'toolbar-header-left');
133
            $adder->setClasses($this->getTypes());
134
135
            $config->addComponent($adder);
136
        }
137
138
        $this->extend('updateField', $gridField);
139
140
        return $gridField;
141
    }
142
}
143