ckeditor5-1.0.x-dev/src/Plugin/CKEditor5Plugin/ImageUpload.php

src/Plugin/CKEditor5Plugin/ImageUpload.php
<?php

namespace Drupal\ckeditor5\Plugin\CKEditor5Plugin;

use Drupal\ckeditor5\Plugin\CKEditor5PluginDefault;
use Drupal\ckeditor5\Plugin\CKEditor5PluginConfigurableInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Drupal\editor\Entity\Editor;

// cspell:ignore imageupload

/**
 * CKEditor5 Image plugin.
 *
 * @CKEditor5Plugin(
 *   id = "ckeditor5.imageUpload",
 *   label = @Translation("Image Upload"),
 *   admin_library = "ckeditor5/admin.imageupload",
 *   plugins = {"image.ImageUpload", "drupalImage.DrupalImageUpload"},
 *   plugin_config = {
 *     "image" = {
 *       "upload" = {
 *         "types" = { "jpeg", "png", "gif" }
 *       }
 *     }
 *   },
 *   toolbar_items = {
 *     "uploadImage" = { "label" = "Image upload" }
 *   },
 *   conditions = {
 *     "toolbarItem" = "uploadImage",
 *     "imageUploadStatus" = true,
 *   }
 * )
 */
class ImageUpload extends CKEditor5PluginDefault implements CKEditor5PluginConfigurableInterface {

  /**
   * {@inheritdoc}
   */
  public function getDynamicPluginConfig(array $static_plugin_config, Editor $editor) {
    return $static_plugin_config + [
      'drupalImageUpload' => [
        'uploadUrl' => URL::fromRoute('ckeditor5.upload_image')
          ->setRouteParameter('editor', $editor->getFilterFormat()->id())
          ->toString(TRUE)
          ->getGeneratedUrl(),
        'withCredentials' => TRUE,
        'headers' => ['Accept' => 'application/json', 'text/javascript'],
      ],
    ];
  }

  /**
   * {@inheritdoc}
   *
   * @see \Drupal\editor\Form\EditorImageDialog
   * @see editor_image_upload_settings_form()
   */
  public function settingsForm(array $form, FormStateInterface $form_state, Editor $editor) {
    $form_state->loadInclude('editor', 'admin.inc');
    $form['image_upload'] = editor_image_upload_settings_form($editor);
    $form['image_upload']['#element_validate'][] = [
      $this, 'validateImageUploadSettings',
    ];
    return $form;
  }

  /**
   * Handler for the "image_upload" element in settingsForm().
   *
   * Moves the text editor's image upload settings into $editor->image_upload.
   *
   * @see \Drupal\editor\Form\EditorImageDialog
   * @see editor_image_upload_settings_form()
   */
  public function validateImageUploadSettings(array $element, FormStateInterface $form_state) {
    $image_upload = [
      'editor',
      'settings',
      'plugins',
      'ckeditor5.imageUpload',
      'image_upload',
    ];
    $settings = $form_state->getValue($image_upload);
    $settings['status'] = (bool) $settings['status'];
    $settings['max_dimensions']['width'] = (int) $settings['max_dimensions']['width'];
    $settings['max_dimensions']['height'] = (int) $settings['max_dimensions']['height'];
    $form_state->get('editor')->setImageUploadSettings($settings);
    $form_state->unsetValue([
      'editor',
      'settings',
      'plugins',
      'ckeditor5.imageUpload',
    ]);
  }

}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc