Upgraded drupal core with security updates
[yaffs-website] / web / core / modules / editor / editor.admin.inc
1 <?php
2
3 /**
4  * @file
5  * Administration functions for editor.module.
6  */
7
8 use Drupal\Core\StreamWrapper\StreamWrapperInterface;
9 use Drupal\editor\Entity\Editor;
10
11 /**
12  * Subform constructor to configure the text editor's image upload settings.
13  *
14  * Each text editor plugin that is configured to offer the ability to insert
15  * images and uses EditorImageDialog for that, should use this form to update
16  * the text editor's configuration so that EditorImageDialog knows whether it
17  * should allow the user to upload images.
18  *
19  * @param \Drupal\editor\Entity\Editor $editor
20  *   The text editor entity that is being edited.
21  *
22  * @return array
23  *   The image upload settings form.
24  *
25  * @see \Drupal\editor\Form\EditorImageDialog
26  */
27 function editor_image_upload_settings_form(Editor $editor) {
28   // Defaults.
29   $image_upload = $editor->getImageUploadSettings();
30   $image_upload += [
31     'status' => FALSE,
32     'scheme' => file_default_scheme(),
33     'directory' => 'inline-images',
34     'max_size' => '',
35     'max_dimensions' => ['width' => '', 'height' => ''],
36   ];
37
38   $form['status'] = [
39     '#type' => 'checkbox',
40     '#title' => t('Enable image uploads'),
41     '#default_value' => $image_upload['status'],
42     '#attributes' => [
43       'data-editor-image-upload' => 'status',
44     ],
45   ];
46   $show_if_image_uploads_enabled = [
47     'visible' => [
48       ':input[data-editor-image-upload="status"]' => ['checked' => TRUE],
49     ],
50   ];
51
52   // Any visible, writable wrapper can potentially be used for uploads,
53   // including a remote file system that integrates with a CDN.
54   $options = \Drupal::service('stream_wrapper_manager')->getDescriptions(StreamWrapperInterface::WRITE_VISIBLE);
55   if (!empty($options)) {
56     $form['scheme'] = [
57       '#type' => 'radios',
58       '#title' => t('File storage'),
59       '#default_value' => $image_upload['scheme'],
60       '#options' => $options,
61       '#states' => $show_if_image_uploads_enabled,
62       '#access' => count($options) > 1,
63     ];
64   }
65   // Set data- attributes with human-readable names for all possible stream
66   // wrappers, so that drupal.ckeditor.drupalimage.admin's summary rendering
67   // can use that.
68   foreach (\Drupal::service('stream_wrapper_manager')->getNames(StreamWrapperInterface::WRITE_VISIBLE) as $scheme => $name) {
69     $form['scheme'][$scheme]['#attributes']['data-label'] = t('Storage: @name', ['@name' => $name]);
70   }
71
72   $form['directory'] = [
73     '#type' => 'textfield',
74     '#default_value' => $image_upload['directory'],
75     '#title' => t('Upload directory'),
76     '#description' => t("A directory relative to Drupal's files directory where uploaded images will be stored."),
77     '#states' => $show_if_image_uploads_enabled,
78   ];
79
80   $default_max_size = format_size(file_upload_max_size());
81   $form['max_size'] = [
82     '#type' => 'textfield',
83     '#default_value' => $image_upload['max_size'],
84     '#title' => t('Maximum file size'),
85     '#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]),
86     '#maxlength' => 20,
87     '#size' => 10,
88     '#placeholder' => $default_max_size,
89     '#states' => $show_if_image_uploads_enabled,
90   ];
91
92   $form['max_dimensions'] = [
93     '#type' => 'item',
94     '#title' => t('Maximum dimensions'),
95     '#field_prefix' => '<div class="container-inline clearfix">',
96     '#field_suffix' => '</div>',
97     '#description' => t('Images larger than these dimensions will be scaled down.'),
98     '#states' => $show_if_image_uploads_enabled,
99   ];
100   $form['max_dimensions']['width'] = [
101     '#title' => t('Width'),
102     '#title_display' => 'invisible',
103     '#type' => 'number',
104     '#default_value' => (empty($image_upload['max_dimensions']['width'])) ? '' : $image_upload['max_dimensions']['width'],
105     '#size' => 8,
106     '#maxlength' => 8,
107     '#min' => 1,
108     '#max' => 99999,
109     '#placeholder' => t('width'),
110     '#field_suffix' => ' x ',
111     '#states' => $show_if_image_uploads_enabled,
112   ];
113   $form['max_dimensions']['height'] = [
114     '#title' => t('Height'),
115     '#title_display' => 'invisible',
116     '#type' => 'number',
117     '#default_value' => (empty($image_upload['max_dimensions']['height'])) ? '' : $image_upload['max_dimensions']['height'],
118     '#size' => 8,
119     '#maxlength' => 8,
120     '#min' => 1,
121     '#max' => 99999,
122     '#placeholder' => t('height'),
123     '#field_suffix' => t('pixels'),
124     '#states' => $show_if_image_uploads_enabled,
125   ];
126
127   return $form;
128 }