1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Drupal\entity_browser\Plugin\Field\FieldWidget; |
4
|
|
|
|
5
|
|
|
use Drupal\Component\Utility\Bytes; |
6
|
|
|
use Drupal\Component\Utility\SortArray; |
7
|
|
|
use Drupal\Core\Config\ConfigFactoryInterface; |
8
|
|
|
use Drupal\Core\Entity\EntityDisplayRepositoryInterface; |
9
|
|
|
use Drupal\Core\Entity\EntityTypeManagerInterface; |
10
|
|
|
use Drupal\Core\Field\FieldDefinitionInterface; |
11
|
|
|
use Drupal\Core\Field\FieldItemListInterface; |
12
|
|
|
use Drupal\Core\Form\FormStateInterface; |
13
|
|
|
use Drupal\Core\Image\ImageFactory; |
14
|
|
|
use Drupal\Core\Url; |
15
|
|
|
use Drupal\entity_browser\FieldWidgetDisplayManager; |
16
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
17
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Entity browser file widget. |
21
|
|
|
* |
22
|
|
|
* @FieldWidget( |
23
|
|
|
* id = "entity_browser_file", |
24
|
|
|
* label = @Translation("Entity browser"), |
25
|
|
|
* provider = "entity_browser", |
26
|
|
|
* multiple_values = TRUE, |
27
|
|
|
* field_types = { |
28
|
|
|
* "file", |
29
|
|
|
* "image" |
30
|
|
|
* } |
31
|
|
|
* ) |
32
|
|
|
*/ |
33
|
|
|
class FileBrowserWidget extends EntityReferenceBrowserWidget { |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Due to the table structure, this widget has a different depth. |
37
|
|
|
* |
38
|
|
|
* @var int |
39
|
|
|
*/ |
40
|
|
|
protected static $deleteDepth = 3; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* A list of currently edited items. Used to determine alt/title values. |
44
|
|
|
* |
45
|
|
|
* @var \Drupal\Core\Field\FieldItemListInterface |
46
|
|
|
*/ |
47
|
|
|
protected $items; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* The config factory service. |
51
|
|
|
* |
52
|
|
|
* @var \Drupal\Core\Config\ConfigFactoryInterface |
53
|
|
|
*/ |
54
|
|
|
protected $configFactory; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* The image factory service. |
58
|
|
|
* |
59
|
|
|
* @var \Drupal\Core\Image\ImageFactory |
60
|
|
|
*/ |
61
|
|
|
protected $imageFactory; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* The display repository service. |
65
|
|
|
* |
66
|
|
|
* @var \Drupal\Core\Entity\EntityDisplayRepositoryInterface |
67
|
|
|
*/ |
68
|
|
|
protected $displayRepository; |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Constructs widget plugin. |
72
|
|
|
* |
73
|
|
|
* @param string $plugin_id |
74
|
|
|
* The plugin_id for the plugin instance. |
75
|
|
|
* @param mixed $plugin_definition |
76
|
|
|
* The plugin implementation definition. |
77
|
|
|
* @param \Drupal\Core\Field\FieldDefinitionInterface $field_definition |
78
|
|
|
* The definition of the field to which the widget is associated. |
79
|
|
|
* @param array $settings |
80
|
|
|
* The widget settings. |
81
|
|
|
* @param array $third_party_settings |
82
|
|
|
* Any third party settings. |
83
|
|
|
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager |
84
|
|
|
* Entity type manager service. |
85
|
|
|
* @param \Symfony\Component\EventDispatcher\EventDispatcherInterface $event_dispatcher |
86
|
|
|
* Event dispatcher. |
87
|
|
|
* @param \Drupal\entity_browser\FieldWidgetDisplayManager $field_display_manager |
88
|
|
|
* Field widget display plugin manager. |
89
|
|
|
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory |
90
|
|
|
* The config factory. |
91
|
|
|
* @param \Drupal\Core\Image\ImageFactory $image_factory |
92
|
|
|
* The image factory. |
93
|
|
|
* @param \Drupal\Core\Entity\EntityDisplayRepositoryInterface $display_repository |
94
|
|
|
* The entity display repository service. |
95
|
|
|
*/ |
96
|
|
|
public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, array $third_party_settings, EntityTypeManagerInterface $entity_type_manager, EventDispatcherInterface $event_dispatcher, FieldWidgetDisplayManager $field_display_manager, ConfigFactoryInterface $config_factory, ImageFactory $image_factory, EntityDisplayRepositoryInterface $display_repository) { |
97
|
|
|
parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $third_party_settings, $entity_type_manager, $event_dispatcher, $field_display_manager); |
98
|
|
|
$this->entityTypeManager = $entity_type_manager; |
99
|
|
|
$this->fieldDisplayManager = $field_display_manager; |
100
|
|
|
$this->configFactory = $config_factory; |
101
|
|
|
$this->imageFactory = $image_factory; |
102
|
|
|
$this->displayRepository = $display_repository; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* {@inheritdoc} |
107
|
|
|
*/ |
108
|
|
|
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { |
109
|
|
|
return new static( |
110
|
|
|
$plugin_id, |
111
|
|
|
$plugin_definition, |
112
|
|
|
$configuration['field_definition'], |
113
|
|
|
$configuration['settings'], |
114
|
|
|
$configuration['third_party_settings'], |
115
|
|
|
$container->get('entity_type.manager'), |
116
|
|
|
$container->get('event_dispatcher'), |
117
|
|
|
$container->get('plugin.manager.entity_browser.field_widget_display'), |
118
|
|
|
$container->get('config.factory'), |
119
|
|
|
$container->get('image.factory'), |
120
|
|
|
$container->get('entity_display.repository') |
121
|
|
|
); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* {@inheritdoc} |
126
|
|
|
*/ |
127
|
|
|
public static function defaultSettings() { |
128
|
|
|
$settings = parent::defaultSettings(); |
129
|
|
|
|
130
|
|
|
// These settings are hidden. |
131
|
|
|
unset($settings['field_widget_display']); |
132
|
|
|
unset($settings['field_widget_display_settings']); |
133
|
|
|
|
134
|
|
|
$settings['view_mode'] = 'default'; |
135
|
|
|
$settings['preview_image_style'] = 'thumbnail'; |
136
|
|
|
|
137
|
|
|
return $settings; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* {@inheritdoc} |
142
|
|
|
*/ |
143
|
|
|
public function settingsForm(array $form, FormStateInterface $form_state) { |
144
|
|
|
$element = parent::settingsForm($form, $form_state); |
145
|
|
|
|
146
|
|
|
$element['field_widget_display']['#access'] = FALSE; |
147
|
|
|
$element['field_widget_display_settings']['#access'] = FALSE; |
148
|
|
|
|
149
|
|
|
$element['view_mode'] = [ |
150
|
|
|
'#title' => $this->t('File view mode'), |
151
|
|
|
'#type' => 'select', |
152
|
|
|
'#default_value' => $this->getSetting('view_mode'), |
153
|
|
|
'#options' => $this->displayRepository->getViewModeOptions('file'), |
154
|
|
|
]; |
155
|
|
|
|
156
|
|
|
$element['preview_image_style'] = [ |
157
|
|
|
'#title' => $this->t('Preview image style'), |
158
|
|
|
'#type' => 'select', |
159
|
|
|
'#options' => image_style_options(FALSE), |
160
|
|
|
'#default_value' => $this->getSetting('preview_image_style'), |
161
|
|
|
'#description' => $this->t('The preview image will be shown while editing the content. Only relevant if using the default file view mode.'), |
162
|
|
|
'#weight' => 15, |
163
|
|
|
'#access' => $this->fieldDefinition->getType() == 'image', |
164
|
|
|
]; |
165
|
|
|
|
166
|
|
|
return $element; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* {@inheritdoc} |
171
|
|
|
*/ |
172
|
|
|
public function settingsSummary() { |
173
|
|
|
$summary = $this->summaryBase(); |
174
|
|
|
$view_mode = $this->getSetting('view_mode'); |
175
|
|
|
$image_style_setting = $this->getSetting('preview_image_style'); |
176
|
|
|
|
177
|
|
|
if ($this->fieldDefinition->getType() == 'image' && $view_mode == 'default') { |
178
|
|
|
$image_styles = image_style_options(FALSE); |
179
|
|
|
// Styles could be lost because of enabled/disabled modules that defines |
180
|
|
|
// their styles in code. |
181
|
|
|
if (isset($image_styles[$image_style_setting])) { |
182
|
|
|
$preview_image_style = $this->t('Preview image style: @style', ['@style' => $image_styles[$image_style_setting]]); |
183
|
|
|
} |
184
|
|
|
else { |
185
|
|
|
$preview_image_style = $this->t('No preview image'); |
186
|
|
|
} |
187
|
|
|
} |
188
|
|
|
array_unshift($summary, $preview_image_style); |
|
|
|
|
189
|
|
|
|
190
|
|
|
return $summary; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* {@inheritdoc} |
195
|
|
|
*/ |
196
|
|
|
public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) { |
197
|
|
|
$this->items = $items; |
198
|
|
|
return parent::formElement($items, $delta, $element, $form, $form_state); |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* {@inheritdoc} |
203
|
|
|
*/ |
204
|
|
|
protected function displayCurrentSelection($details_id, $field_parents, $entities) { |
205
|
|
|
$field_type = $this->fieldDefinition->getType(); |
206
|
|
|
$field_settings = $this->fieldDefinition->getSettings(); |
207
|
|
|
$field_machine_name = $this->fieldDefinition->getName(); |
208
|
|
|
$file_settings = $this->configFactory->get('file.settings'); |
209
|
|
|
$widget_settings = $this->getSettings(); |
210
|
|
|
$view_mode = $widget_settings['view_mode']; |
211
|
|
|
$can_edit = (bool) $widget_settings['field_widget_edit']; |
212
|
|
|
|
213
|
|
|
$delta = 0; |
214
|
|
|
|
215
|
|
|
$order_class = $field_machine_name . '-delta-order'; |
216
|
|
|
|
217
|
|
|
$current = [ |
218
|
|
|
'#type' => 'table', |
219
|
|
|
'#header' => [ |
220
|
|
|
$this->t('Preview'), |
221
|
|
|
$this->t('Filename'), |
222
|
|
|
$this->t('Metadata'), |
223
|
|
|
['data' => $this->t('Operations'), 'colspan' => 2], |
224
|
|
|
$this->t('Order', [], ['context' => 'Sort order']), |
225
|
|
|
], |
226
|
|
|
'#empty' => $this->t('No files yet'), |
227
|
|
|
'#attributes' => ['class' => ['entities-list']], |
228
|
|
|
'#tabledrag' => [ |
229
|
|
|
[ |
230
|
|
|
'action' => 'order', |
231
|
|
|
'relationship' => 'sibling', |
232
|
|
|
'group' => $order_class, |
233
|
|
|
], |
234
|
|
|
], |
235
|
|
|
]; |
236
|
|
|
/** @var \Drupal\file\FileInterface[] $entities */ |
237
|
|
|
foreach ($entities as $entity) { |
238
|
|
|
// Check to see if this entity has an edit form. If not, the edit button |
239
|
|
|
// will only throw an exception. |
240
|
|
|
if (!$entity->getEntityType()->getFormClass('edit')) { |
241
|
|
|
$can_edit = FALSE; |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
$entity_id = $entity->id(); |
245
|
|
|
$uri = $entity->getFileUri(); |
246
|
|
|
if ($field_type == 'image' && $view_mode == 'default') { |
247
|
|
|
$image = $this->imageFactory->get($uri); |
248
|
|
|
if ($image->isValid()) { |
249
|
|
|
$width = $image->getWidth(); |
250
|
|
|
$height = $image->getHeight(); |
251
|
|
|
} |
252
|
|
|
else { |
253
|
|
|
$width = $height = NULL; |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
$display = [ |
257
|
|
|
'#weight' => -10, |
258
|
|
|
'#theme' => 'image_style', |
259
|
|
|
'#width' => $width, |
260
|
|
|
'#height' => $height, |
261
|
|
|
'#style_name' => $widget_settings['preview_image_style'], |
262
|
|
|
'#uri' => $uri, |
263
|
|
|
]; |
264
|
|
|
} |
265
|
|
|
else { |
266
|
|
|
$display = $this->entityTypeManager->getViewBuilder('file')->view($entity, $view_mode); |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
// Find the default description. |
270
|
|
|
$description = ''; |
271
|
|
|
$display_field = $field_settings['display_default']; |
272
|
|
|
$alt = ''; |
273
|
|
|
$title = ''; |
274
|
|
|
$weight = $delta; |
275
|
|
|
foreach ($this->items as $item) { |
276
|
|
|
if ($item->target_id == $entity_id) { |
277
|
|
|
if ($field_type == 'file') { |
278
|
|
|
$description = $item->description; |
279
|
|
|
$display_field = $item->display; |
280
|
|
|
} |
281
|
|
|
elseif ($field_type == 'image') { |
282
|
|
|
$alt = $item->alt; |
283
|
|
|
$title = $item->title; |
284
|
|
|
} |
285
|
|
|
$weight = $item->_weight ?: $delta; |
286
|
|
|
} |
287
|
|
|
} |
288
|
|
|
|
289
|
|
|
$current[$entity_id] = [ |
290
|
|
|
'#attributes' => [ |
291
|
|
|
'class' => ['draggable'], |
292
|
|
|
'data-entity-id' => $entity->getEntityTypeId() . ':' . $entity_id, |
293
|
|
|
], |
294
|
|
|
'display' => $display, |
295
|
|
|
'filename' => ['#markup' => $entity->label()], |
296
|
|
|
'meta' => [ |
297
|
|
|
'display_field' => [ |
298
|
|
|
'#type' => 'checkbox', |
299
|
|
|
'#title' => $this->t('Include file in display'), |
300
|
|
|
'#default_value' => (bool) $display_field, |
301
|
|
|
'#access' => $field_type == 'file' && $field_settings['display_field'], |
302
|
|
|
], |
303
|
|
|
'description' => [ |
304
|
|
|
'#type' => $file_settings->get('description.type'), |
305
|
|
|
'#title' => $this->t('Description'), |
306
|
|
|
'#default_value' => $description, |
307
|
|
|
'#size' => 45, |
308
|
|
|
'#maxlength' => $file_settings->get('description.length'), |
309
|
|
|
'#description' => $this->t('The description may be used as the label of the link to the file.'), |
310
|
|
|
'#access' => $field_type == 'file' && $field_settings['description_field'], |
311
|
|
|
], |
312
|
|
|
'alt' => [ |
313
|
|
|
'#type' => 'textfield', |
314
|
|
|
'#title' => $this->t('Alternative text'), |
315
|
|
|
'#default_value' => $alt, |
316
|
|
|
'#size' => 45, |
317
|
|
|
'#maxlength' => 512, |
318
|
|
|
'#description' => $this->t('This text will be used by screen readers, search engines, or when the image cannot be loaded.'), |
319
|
|
|
'#access' => $field_type == 'image' && $field_settings['alt_field'], |
320
|
|
|
'#required' => $field_type == 'image' && $field_settings['alt_field_required'], |
321
|
|
|
], |
322
|
|
|
'title' => [ |
323
|
|
|
'#type' => 'textfield', |
324
|
|
|
'#title' => $this->t('Title'), |
325
|
|
|
'#default_value' => $title, |
326
|
|
|
'#size' => 45, |
327
|
|
|
'#maxlength' => 1024, |
328
|
|
|
'#description' => $this->t('The title is used as a tool tip when the user hovers the mouse over the image.'), |
329
|
|
|
'#access' => $field_type == 'image' && $field_settings['title_field'], |
330
|
|
|
'#required' => $field_type == 'image' && $field_settings['title_field_required'], |
331
|
|
|
], |
332
|
|
|
], |
333
|
|
|
'edit_button' => [ |
334
|
|
|
'#type' => 'submit', |
335
|
|
|
'#value' => $this->t('Edit'), |
336
|
|
|
'#ajax' => [ |
337
|
|
|
'url' => Url::fromRoute('entity_browser.edit_form', ['entity_type' => $entity->getEntityTypeId(), 'entity' => $entity_id]), |
338
|
|
|
], |
339
|
|
|
'#access' => $can_edit, |
340
|
|
|
], |
341
|
|
|
'remove_button' => [ |
342
|
|
|
'#type' => 'submit', |
343
|
|
|
'#value' => $this->t('Remove'), |
344
|
|
|
'#ajax' => [ |
345
|
|
|
'callback' => [get_class($this), 'updateWidgetCallback'], |
346
|
|
|
'wrapper' => $details_id, |
347
|
|
|
], |
348
|
|
|
'#submit' => [[get_class($this), 'removeItemSubmit']], |
349
|
|
|
'#name' => $field_machine_name . '_remove_' . $entity_id, |
350
|
|
|
'#limit_validation_errors' => [array_merge($field_parents, [$field_machine_name, 'target_id'])], |
351
|
|
|
'#attributes' => ['data-entity-id' => $entity->getEntityTypeId() . ':' . $entity_id], |
352
|
|
|
'#access' => (bool) $widget_settings['field_widget_remove'], |
353
|
|
|
], |
354
|
|
|
'_weight' => [ |
355
|
|
|
'#type' => 'weight', |
356
|
|
|
'#title' => $this->t('Weight for row @number', ['@number' => $delta + 1]), |
357
|
|
|
'#title_display' => 'invisible', |
358
|
|
|
// Note: this 'delta' is the FAPI #type 'weight' element's property. |
359
|
|
|
'#delta' => count($entities), |
360
|
|
|
'#default_value' => $weight, |
361
|
|
|
'#attributes' => ['class' => [$order_class]], |
362
|
|
|
], |
363
|
|
|
]; |
364
|
|
|
|
365
|
|
|
$delta++; |
366
|
|
|
} |
367
|
|
|
|
368
|
|
|
return $current; |
369
|
|
|
} |
370
|
|
|
|
371
|
|
|
/** |
372
|
|
|
* {@inheritdoc} |
373
|
|
|
*/ |
374
|
|
|
public function massageFormValues(array $values, array $form, FormStateInterface $form_state) { |
375
|
|
|
$ids = empty($values['target_id']) ? [] : explode(' ', trim($values['target_id'])); |
376
|
|
|
$return = []; |
377
|
|
|
foreach ($ids as $id) { |
378
|
|
|
$id = explode(':', $id)[1]; |
379
|
|
|
if (is_array($values['current']) && isset($values['current'][$id])) { |
380
|
|
|
$item_values = [ |
381
|
|
|
'target_id' => $id, |
382
|
|
|
'_weight' => $values['current'][$id]['_weight'], |
383
|
|
|
]; |
384
|
|
View Code Duplication |
if ($this->fieldDefinition->getType() == 'file') { |
|
|
|
|
385
|
|
|
if (isset($values['current'][$id]['meta']['description'])) { |
386
|
|
|
$item_values['description'] = $values['current'][$id]['meta']['description']; |
387
|
|
|
} |
388
|
|
|
if ($this->fieldDefinition->getSetting('display_field') && isset($values['current'][$id]['meta']['display_field'])) { |
389
|
|
|
$item_values['display'] = $values['current'][$id]['meta']['display_field']; |
390
|
|
|
} |
391
|
|
|
} |
392
|
|
View Code Duplication |
if ($this->fieldDefinition->getType() == 'image') { |
|
|
|
|
393
|
|
|
if (isset($values['current'][$id]['meta']['alt'])) { |
394
|
|
|
$item_values['alt'] = $values['current'][$id]['meta']['alt']; |
395
|
|
|
} |
396
|
|
|
if (isset($values['current'][$id]['meta']['title'])) { |
397
|
|
|
$item_values['title'] = $values['current'][$id]['meta']['title']; |
398
|
|
|
} |
399
|
|
|
} |
400
|
|
|
$return[] = $item_values; |
401
|
|
|
} |
402
|
|
|
} |
403
|
|
|
|
404
|
|
|
// Return ourself as the structure doesn't match the default. |
405
|
|
|
usort($return, function ($a, $b) { |
406
|
|
|
return SortArray::sortByKeyInt($a, $b, '_weight'); |
407
|
|
|
}); |
408
|
|
|
|
409
|
|
|
return array_values($return); |
410
|
|
|
} |
411
|
|
|
|
412
|
|
|
/** |
413
|
|
|
* Retrieves the upload validators for a file field. |
414
|
|
|
* |
415
|
|
|
* This is a combination of logic shared between the File and Image widgets. |
416
|
|
|
* |
417
|
|
|
* @return array |
418
|
|
|
* An array suitable for passing to file_save_upload() or the file field |
419
|
|
|
* element's '#upload_validators' property. |
420
|
|
|
*/ |
421
|
|
|
public function getFileValidators() { |
422
|
|
|
$validators = []; |
423
|
|
|
$settings = $this->fieldDefinition->getSettings(); |
424
|
|
|
|
425
|
|
|
// Cap the upload size according to the PHP limit. |
426
|
|
|
$max_filesize = Bytes::toInt(file_upload_max_size()); |
427
|
|
|
if (!empty($settings['max_filesize'])) { |
428
|
|
|
$max_filesize = min($max_filesize, Bytes::toInt($settings['max_filesize'])); |
429
|
|
|
} |
430
|
|
|
|
431
|
|
|
// There is always a file size limit due to the PHP server limit. |
432
|
|
|
$validators['file_validate_size'] = [$max_filesize]; |
433
|
|
|
|
434
|
|
|
// Images have expected defaults for file extensions. |
435
|
|
|
// See \Drupal\image\Plugin\Field\FieldWidget::formElement() for details. |
436
|
|
|
if ($this->fieldDefinition->getType() == 'image') { |
437
|
|
|
// If not using custom extension validation, ensure this is an image. |
438
|
|
|
$supported_extensions = ['png', 'gif', 'jpg', 'jpeg']; |
439
|
|
|
$extensions = isset($settings['file_extensions']) ? $settings['file_extensions'] : implode(' ', $supported_extensions); |
440
|
|
|
$extensions = array_intersect(explode(' ', $extensions), $supported_extensions); |
441
|
|
|
$validators['file_validate_extensions'] = [implode(' ', $extensions)]; |
442
|
|
|
} |
443
|
|
|
elseif (!empty($settings['file_extensions'])) { |
444
|
|
|
$validators['file_validate_extensions'] = [$settings['file_extensions']]; |
445
|
|
|
} |
446
|
|
|
|
447
|
|
|
// Add upload resolution validation. |
448
|
|
|
if ($settings['max_resolution'] || $settings['min_resolution']) { |
449
|
|
|
$validators['entity_browser_file_validate_image_resolution'] = [$settings['max_resolution'], $settings['min_resolution']]; |
450
|
|
|
} |
451
|
|
|
|
452
|
|
|
return $validators; |
453
|
|
|
} |
454
|
|
|
|
455
|
|
|
/** |
456
|
|
|
* {@inheritdoc} |
457
|
|
|
*/ |
458
|
|
|
protected function getPersistentData() { |
459
|
|
|
$data = parent::getPersistentData(); |
460
|
|
|
$settings = $this->fieldDefinition->getSettings(); |
461
|
|
|
// Add validators based on our current settings. |
462
|
|
|
$data['validators']['file'] = ['validators' => $this->getFileValidators()]; |
463
|
|
|
// Provide context for widgets to enhance their configuration. Currently |
464
|
|
|
// we only know that "upload_location" is used. |
465
|
|
|
$data['widget_context']['upload_location'] = $settings['uri_scheme'] . '://' . $settings['file_directory']; |
466
|
|
|
return $data; |
467
|
|
|
} |
468
|
|
|
|
469
|
|
|
} |
470
|
|
|
|
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: