-
Notifications
You must be signed in to change notification settings - Fork 4
/
insert_svg.admin.inc
102 lines (93 loc) · 3.48 KB
/
insert_svg.admin.inc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
<?php
/**
* @file
* Administration functions for insert_svg.module.
*/
use Drupal\Core\StreamWrapper\StreamWrapperInterface;
use Drupal\editor\Entity\Editor;
/**
* Subform constructor to configure the text editor's file upload settings.
*
* @param \Drupal\editor\Entity\Editor $editor
* The text editor entity that is being edited.
*
* @return array
* The file upload settings form.
*
* @see \Drupal\insert_svg\Plugin\CKEditorPlugin\SvgFile
*/
function insert_svg_upload_settings_form(Editor $editor) {
// Defaults.
$file_upload = $editor->getThirdPartySettings('insert_svg');
$file_upload += [
'status' => TRUE,
'scheme' => file_default_scheme(),
'directory' => 'inline-illustrations',
'extensions' => 'svg',
'max_size' => '',
];
$form['status'] = [
'#type' => 'checkbox',
'#title' => t('Enable file uploads'),
'#default_value' => $file_upload['status'],
'#attributes' => [
'data-editor-file-upload' => 'status',
],
];
$show_if_file_uploads_enabled = [
'visible' => [
':input[data-editor-file-upload="status"]' => ['checked' => TRUE],
],
];
// Any visible, writable wrapper can potentially be used for uploads,
// including a remote file system that integrates with a CDN.
$options = \Drupal::service('stream_wrapper_manager')->getDescriptions(StreamWrapperInterface::WRITE_VISIBLE);
if (!empty($options)) {
$form['scheme'] = [
'#type' => 'radios',
'#title' => t('File storage'),
'#default_value' => $file_upload['scheme'],
'#options' => $options,
'#states' => $show_if_file_uploads_enabled,
'#access' => count($options) > 1,
];
}
// Set data- attributes with human-readable names for all possible stream
// wrappers, so that drupal.ckeditor.svgfile.admin's summary rendering
// can use that.
foreach (\Drupal::service('stream_wrapper_manager')->getNames(StreamWrapperInterface::WRITE_VISIBLE) as $scheme => $name) {
$form['scheme'][$scheme]['#attributes']['data-label'] = t('Storage: @name', ['@name' => $name]);
}
$form['directory'] = [
'#type' => 'textfield',
'#default_value' => $file_upload['directory'],
'#title' => t('Upload directory'),
'#description' => t("A directory relative to Drupal's files directory where uploaded files will be stored."),
'#states' => $show_if_file_uploads_enabled,
];
$extensions = str_replace(' ', ', ', $file_upload['extensions']);
$form['extensions'] = [
'#type' => 'textfield',
'#title' => t('Allowed file extensions'),
'#default_value' => $extensions,
'#description' => t('Separate extensions with a space or comma and do not include the leading dot.'),
'#element_validate' => [['\Drupal\file\Plugin\Field\FieldType\FileItem', 'validateExtensions']],
'#maxlength' => 256,
// By making this field required, we prevent a potential security issue
// that would allow files of any type to be uploaded.
'#required' => TRUE,
'#states' => $show_if_file_uploads_enabled,
];
$default_max_size = format_size(file_upload_max_size());
$form['max_size'] = [
'#type' => 'textfield',
'#default_value' => $file_upload['max_size'],
'#title' => t('Maximum file size'),
'#description' => t('If this is left empty, then the file size will be limited by the PHP maximum upload size of @size.', ['@size' => $default_max_size]),
'#maxlength' => 20,
'#size' => 10,
'#placeholder' => $default_max_size,
'#states' => $show_if_file_uploads_enabled,
];
return $form;
}