qbank_dam-8.x-1.4/src/Form/CKEditorDialog.php

src/Form/CKEditorDialog.php
<?php

namespace Drupal\qbank_dam\Form;

use Drupal\Core\Ajax\AjaxResponse;
use Drupal\Core\Ajax\CloseModalDialogCommand;
use Drupal\Core\Ajax\OpenDialogCommand;
use Drupal\Core\Ajax\HtmlCommand;
use Drupal\Core\Extension\ExtensionPathResolver;
use Drupal\Core\File\FileUrlGeneratorInterface;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\editor\Ajax\EditorDialogSave;
use Drupal\qbank_dam\QBankDAMService;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Class CKEditorDialog.
 *
 * @package Drupal\qbank_dam\Form
 */
class CKEditorDialog extends FormBase {

  protected $QAPI;
  protected $extensionPathResolver;
  protected $fileUrlGenerator;

  public function __construct(QBankDAMService $qbank_api, ExtensionPathResolver $extension_path_resolver, FileUrlGeneratorInterface $file_url_generator) {
    $this->QAPI = $qbank_api;
    $this->extensionPathResolver = $extension_path_resolver;
    $this->fileUrlGenerator = $file_url_generator;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static(
            $container->get('qbank_dam.service'),
            $container->get('extension.path.resolver'),
            $container->get('file_url_generator')
          );
  }

  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'qbank_ckeditor_dialog';
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    // Ensure relevant dialog libraries are attached.
    $form['#attached']['library'][] = 'editor/drupal.editor.dialog';
    $form['#attached']['library'][] = 'qbank_dam/ckeditor';

    $wrapper_id = 'qbank-ckeditor-wrapper' . rand();
    $form['#attached']['drupalSettings']['qbank_dam']['protocol'] = $this->QAPI->getProtocol();
    $form['#attached']['drupalSettings']['qbank_dam']['deployment_site'] = $this->QAPI->getDeploymentSite();
    $form['#attached']['drupalSettings']['qbank_dam']['url'] = $this->QAPI->getApiUrl();
    $form['#attached']['drupalSettings']['qbank_dam']['token'] = $this->QAPI->getToken();
    $form['#attached']['drupalSettings']['qbank_dam']['modulePath'] = $this->extensionPathResolver->getPath('module', 'qbank_dam');
    $form['#attached']['drupalSettings']['qbank_dam']['html_id'] = $wrapper_id;

    $form['#prefix'] = '<div id="' . $wrapper_id . '">';
    $form['#suffix'] = '</div>';

    $form['qbank_url'] = [
      '#type' => 'hidden',
      '#title' => $this->t('Url'),
      '#maxlength' => 256,
      '#size' => 64,
    ];

    $form['qbank_extension'] = [
      '#type' => 'hidden',
      '#title' => $this->t('Extension'),
      '#maxlength' => 256,
      '#size' => 64,
    ];

    $form['qbank_title'] = [
      '#type' => 'hidden',
      '#title' => $this->t('Title'),
      '#maxlength' => 64,
      '#size' => 64,
    ];

    $form['qbank_media_id'] = [
      '#type' => 'hidden',
      '#title' => $this->t('Media ID'),
      '#maxlength' => 64,
      '#size' => 64,
    ];

    $form['actions'] = [
      '#type' => 'actions',
    ];
    $form['actions']['save_modal'] = [
      '#type' => 'submit',
      '#value' => $this->t('Save'),
      '#submit' => [],
      '#ajax' => [
        'callback' => '::submitForm',
        'event' => 'click',
        'wrapper' => 'qbank-dam-dialog-form',
      ],
    ];

    return $form;
  }

  /**
    * {@inheritdoc}
    */
  public function validateForm(array &$form, FormStateInterface $form_state) {
    parent::validateForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
	
    $response = new AjaxResponse();

    $media = $this->QAPI->getMedia($form_state->getValue('qbank_media_id'));


    $templates = $this->QAPI->findSuitableTemplateId(
      $media->getMimetype()->getClassification(), 
      $media->getDeployedFiles()
    );

    $thumb_url = $form_state->getValue('qbank_url');
	
	$resized = false;
	if(substr_count($thumb_url, '[crop:')){
		$resized=true;
	}

	if($resized OR $media->getMimetype()->getClassification() == 'image'){
		$media_url = $thumb_url;
	}else{
		$media_url = str_replace('template', 'selection', $form_state->getValue('qbank_url')).'&templateType='.$media->getMimetype()->getClassification().'&template='.$templates[0];
	}

    $filename = $media->getFilename();

    try{
      $thumbnail = $this->QAPI->download(
        $thumb_url,
        $form_state->getValue('qbank_media_id')
	  );
	  $file = $this->QAPI->download(
		$media_url,
		$form_state->getValue('qbank_media_id')
	  );
    }catch(\Exception $e){
      $thumbnail = FALSE;
    }




  

    if (!empty($file)) {      
      $thumbnail_url = $thumbnail ? $this->fileUrlGenerator->generateAbsoluteString($thumbnail->getFileUri()):''; 
      $file_url = $this->fileUrlGenerator->generateAbsoluteString($file->getFileUri());      
      $form_state->setValue(array('attributes', 'filename'), $filename);
      $form_state->setValue(array('attributes', 'thumbnail'), $thumbnail_url);
      $form_state->setValue(array('attributes', 'src'), $file_url);
      $form_state->setValue(array('attributes', 'type'), $resized?'image':$media->getMimetype()->getClassification());
      $form_state->setValue(array('attributes', 'mimetype'), $resized?'image/jpeg':$media->getMimetype()->getMimetype());
      $form_state->setValue(array('attributes', 'data-entity-uuid'), $file->uuid());
      $form_state->setValue(array('attributes', 'data-entity-type'), 'file');
    }

    if ($form_state->getErrors()) {
      unset($form['#prefix'], $form['#suffix']);
      $form['status_messages'] = [
        '#type' => 'status_messages',
        '#weight' => -10,
      ];
      $response->addCommand(new HtmlCommand('#editor-file-dialog-form', $form));
    }
    else {
      $response->addCommand(new EditorDialogSave($form_state->getValues()));
      $response->addCommand(new CloseModalDialogCommand());
    }

    return $response;
  }
  
}

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

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