editor.admin.inc 4.3 KB

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