GridFieldRowLink::getExtraClass()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
namespace LeKoala\CmsActions;
4
5
use SilverStripe\Control\Controller;
6
use SilverStripe\Forms\GridField\GridField;
7
use SilverStripe\Forms\GridField\GridField_ColumnProvider;
8
use SilverStripe\ORM\DataObject;
9
use SilverStripe\View\ArrayData;
10
use SilverStripe\View\SSViewer;
11
12
/**
13
 * Provides a custom link for a single record
14
 *
15
 * Links do not trigger actions, use GridFieldRowButton instead
16
 */
17
class GridFieldRowLink implements GridField_ColumnProvider
18
{
19
    /**
20
     * HTML classes to be added to GridField buttons
21
     *
22
     * @var array<string,mixed>
23
     */
24
    protected $extraClass = [
25
        'grid-field__icon-action--hidden-on-hover' => true,
26
        'btn--icon-large'                          => true
27
    ];
28
29
    /**
30
     * @var string
31
     */
32
    protected $name;
33
34
    /**
35
     * @var string
36
     */
37
    protected $title;
38
39
    /**
40
     * @var boolean
41
     */
42
    protected $newWindow = true;
43
44
    /**
45
     * @param string $name
46
     * @param string $title
47
     * @param string $icon
48
     */
49
    public function __construct($name, $title, $icon = 'white-question')
50
    {
51
        $this->name = $name;
52
        $this->title = $title;
53
        $this->addExtraClass('font-icon-' . $icon);
54
    }
55
56
    /**
57
     * Add a column 'Actions'
58
     *
59
     * @param GridField $gridField
60
     * @param array<string> $columns
61
     * @return void
62
     */
63
    public function augmentColumns($gridField, &$columns)
64
    {
65
        if (!in_array('Actions', $columns)) {
66
            $columns[] = 'Actions';
67
        }
68
    }
69
70
    /**
71
     * Return any special attributes that will be used for FormField::create_tag()
72
     *
73
     * @param GridField $gridField
74
     * @param DataObject $record
75
     * @param string $columnName
76
     * @return array<string,string>
77
     */
78
    public function getColumnAttributes($gridField, $record, $columnName)
79
    {
80
        return ['class' => 'grid-field__col-compact'];
81
    }
82
83
    /**
84
     * Add the title
85
     *
86
     * @param GridField $gridField
87
     * @param string $columnName
88
     * @return array<string|int,string|null>
89
     */
90
    public function getColumnMetadata($gridField, $columnName)
91
    {
92
        if ($columnName === 'Actions') {
93
            return ['title' => ''];
94
        }
95
96
        return [];
97
    }
98
99
    /**
100
     * Which columns are handled by this component
101
     *
102
     * @param GridField $gridField
103
     * @return array<string>
104
     */
105
    public function getColumnsHandled($gridField)
106
    {
107
        return ['Actions'];
108
    }
109
110
    /**
111
     * @param GridField $gridField
112
     * @param DataObject $record
113
     * @param string $columnName
114
     * @return string The link to the action
115
     */
116
    public function getLink($gridField, $record, $columnName)
117
    {
118
        return Controller::join_links($gridField->Link('item'), $record->ID, $this->name);
119
    }
120
121
    /**
122
     * @param GridField $gridField
123
     * @param DataObject $record
124
     * @param string $columnName
125
     * @return string The HTML for the column
126
     */
127
    public function getColumnContent($gridField, $record, $columnName)
128
    {
129
        if ($this->newWindow) {
130
            $this->addExtraClass('no-ajax');
131
        }
132
133
        $data = new ArrayData(
0 ignored issues
show
Deprecated Code introduced by
The class SilverStripe\View\ArrayData has been deprecated: 5.4.0 Will be renamed to SilverStripe\Model\ArrayData ( Ignorable by Annotation )

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

133
        $data = /** @scrutinizer ignore-deprecated */ new ArrayData(
Loading history...
134
            [
135
                'Link' => $this->getLink($gridField, $record, $columnName),
136
                'ExtraClass' => $this->getExtraClass(),
137
                'Title' => $this->title,
138
                'NewWindow' => $this->newWindow
139
            ]
140
        );
141
142
        $template = SSViewer::get_templates_by_class($this, '', __CLASS__);
143
144
        return $data->renderWith($template);
145
    }
146
147
    /**
148
     * Get the extra HTML classes to add for edit buttons
149
     *
150
     * @return string
151
     */
152
    public function getExtraClass()
153
    {
154
        return implode(' ', array_keys($this->extraClass));
155
    }
156
157
    /**
158
     * Add an extra HTML class
159
     *
160
     * @param string $class
161
     * @return $this
162
     */
163
    public function addExtraClass($class)
164
    {
165
        $this->extraClass[$class] = true;
166
167
        return $this;
168
    }
169
170
    /**
171
     * Remove an HTML class
172
     *
173
     * @param string $class
174
     * @return $this
175
     */
176
    public function removeExtraClass($class)
177
    {
178
        unset($this->extraClass[$class]);
179
180
        return $this;
181
    }
182
183
184
    /**
185
     * Get the value of name
186
     *
187
     * @return string
188
     */
189
    public function getName()
190
    {
191
        return $this->name;
192
    }
193
194
    /**
195
     * Set the value of name
196
     *
197
     * @param string $name
198
     *
199
     * @return $this
200
     */
201
    public function setName(string $name)
202
    {
203
        $this->name = $name;
204
205
        return $this;
206
    }
207
208
    /**
209
     * Get the value of title
210
     *
211
     * @return string
212
     */
213
    public function getTitle()
214
    {
215
        return $this->title;
216
    }
217
218
    /**
219
     * Set the value of title
220
     *
221
     * @param string $title
222
     *
223
     * @return $this
224
     */
225
    public function setTitle(string $title)
226
    {
227
        $this->title = $title;
228
229
        return $this;
230
    }
231
232
    /**
233
     * Get the value of newWindow
234
     * @return bool
235
     */
236
    public function getNewWindow()
237
    {
238
        return $this->newWindow;
239
    }
240
241
    /**
242
     * Set the value of newWindow
243
     *
244
     * @param bool $newWindow
245
     * @return $this
246
     */
247
    public function setNewWindow($newWindow)
248
    {
249
        $this->newWindow = $newWindow;
250
        return $this;
251
    }
252
}
253