Issues (81)

src/BlockFieldList.php (1 issue)

Labels
Severity
1
<?php
2
3
namespace LeKoala\Blocks;
4
5
use SilverStripe\Forms\FormField;
6
use LeKoala\Blocks\Fields\BlockButtonField;
7
use SilverStripe\AssetAdmin\Forms\UploadField;
8
use LeKoala\Blocks\Fields\BlockHTMLEditorField;
9
10
/**
11
 * Easily add fields to your blocks
12
 *
13
 * TODO: extract BuildableFieldList from module to separate form module
14
 */
15
class BlockFieldList extends BuildableFieldList
16
{
17
    /**
18
     * @var string
19
     */
20
    protected $defaultTab = 'Main';
21
22
    /**
23
     * The key to store data to. Typically BlockData or Settings
24
     *
25
     * @var string
26
     */
27
    protected $defaultKey = 'BlockData';
28
29
    /**
30
     * Automatically expand the given name to match the default key
31
     * and items if necessary
32
     *
33
     * @param string|array $name The key or a composite key like [2, "FieldName"].
34
     * @param string $baseKey Will use defaultKey if not provided. Use '' for no key.
35
     * @return string
36
     */
37
    protected function normalizeName($name, $baseKey = null)
38
    {
39
        if ($baseKey === null) {
40
            $baseKey = $this->defaultKey;
41
        }
42
        if (is_array($name)) {
43
            $itemsKey = Block::ITEMS_KEY;
44
            $idx = $name[0];
45
            $key = $name[1];
46
            $name = $baseKey . '[' . $itemsKey . '][' . $idx . '][' . $key . ']';
47
        } else {
48
            if ($baseKey) {
49
                $name = $baseKey . '[' . $name . ']';
50
            }
51
        }
52
        return $name;
53
    }
54
55
    protected function normalizeTitle($name, $title = "")
56
    {
57
        $name = str_replace([$this->defaultKey . '[', ']'], '', $name);
58
        return parent::normalizeTitle($name, $title);
59
    }
60
61
    /**
62
     * Get our default uploader
63
     *
64
     * @return string
65
     */
66
    public static function getUploaderClass()
67
    {
68
        if (class_exists(\LeKoala\Base\Forms\SmartSortableUploadField::class)) {
0 ignored issues
show
The type LeKoala\Base\Forms\SmartSortableUploadField was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
69
            return \LeKoala\Base\Forms\SmartSortableUploadField::class;
70
        } elseif (class_exists(\Bummzack\SortableFile\Forms\SortableUploadField::class)) {
71
            return \Bummzack\SortableFile\Forms\SortableUploadField::class;
72
        }
73
        return UploadField::class;
74
    }
75
76
    /**
77
     * Add a sortable Files uploader
78
     *
79
     * @param array $attributes
80
     * @param string $title
81
     * @return UploadField
82
     */
83
    public function addFiles($attributes = [], $title = null)
84
    {
85
        $class = self::getUploaderClass();
86
        $name = "Files";
87
        return parent::addField($class, $name, $title, $attributes);
88
    }
89
90
    /**
91
     * Add a sortable Images uploader
92
     *
93
     * @param array $attributes
94
     * @param string $title
95
     * @return UploadField
96
     */
97
    public function addImages($attributes = [], $title = null)
98
    {
99
        $class = self::getUploaderClass();
100
        $name = "Images";
101
        return parent::addField($class, $name, $title, $attributes);
102
    }
103
104
    /**
105
     * Convenience methods to add settings
106
     *
107
     * @param callable $cb
108
     * @return void
109
     */
110
    public function addSettings($cb)
111
    {
112
        $tab = $this->getDefaultTab();
113
        $key = $this->getDefaultKey();
114
        $this->setCurrentTab('Settings');
115
        $this->setDefaultKey('Settings');
116
117
        $cb($this);
118
119
        $this->setCurrentTab($tab);
120
        $this->setDefaultKey($key);
121
    }
122
123
    /**
124
     * Add a field to the list
125
     *
126
     * Supports adding items from lists which will be available
127
     * under the "Items" list
128
     *
129
     * see : $data[self::ITEMS_KEY] = self::normalizeIndexedList($data[self::ITEMS_KEY]);
130
     *
131
     * @param string $class
132
     * @param string|array $name Pass an array as [$idx, $name] to specify an item from a list
133
     * @param string $title
134
     * @param array $attributes
135
     * @return FormField
136
     */
137
    public function addField($class, $name, $title = "", $attributes = [])
138
    {
139
        $name = $this->normalizeName($name);
140
        return parent::addField($class, $name, $title, $attributes);
141
    }
142
143
    /**
144
     * @param string $name
145
     * @param string $title
146
     * @return BlockButtonField
147
     */
148
    public function addButton($name = "Button", $title = null)
149
    {
150
        return $this->addField(BlockButtonField::class, $name, $title);
151
    }
152
153
    /**
154
     * @param string $name
155
     * @param string $title
156
     * @param array $attributes
157
     * @return BlockHTMLEditorField
158
     */
159
    public function addEditor($name = "Description", $title = null, $attributes = [])
160
    {
161
        return $this->addField(BlockHTMLEditorField::class, $name, $title, $attributes);
162
    }
163
164
165
    /**
166
     * Get the value of defaultKey
167
     */
168
    public function getDefaultKey()
169
    {
170
        return $this->defaultKey;
171
    }
172
173
    /**
174
     * Set the value of defaultKey
175
     *
176
     * @return $this
177
     */
178
    public function setDefaultKey($defaultKey)
179
    {
180
        $this->defaultKey = $defaultKey;
181
        return $this;
182
    }
183
184
    /**
185
     * @param string $key
186
     * @param callable $callable
187
     * @return $this
188
     */
189
    public function withKey($key, $callable)
190
    {
191
        $defaultKey = $this->getDefaultKey();
192
        $this->setDefaultKey($key);
193
        $callable($this);
194
        $this->setDefaultKey($defaultKey);
195
        return $this;
196
    }
197
}
198