N1edFileField.php in N1ED - Visual editor as CKEditor plugin with Bootstrap support 8.2
File
src/N1edFileField.php
View source
<?php
namespace Drupal\n1ed;
use Drupal\Core\Security\TrustedCallbackInterface;
use Drupal\Core\Field\WidgetInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
class N1edFileField implements TrustedCallbackInterface {
public static function trustedCallbacks() {
return [
'preRenderWidget',
];
}
public static function processWidget($element, FormStateInterface $form_state, $form) {
$settings = \Drupal::config('n1ed.settings');
$apiKey = $settings
->get('apikey') ?: 'N1D8DFLT';
$version = $settings
->get('version') ?: '';
if ($version === '') {
$version = 'latest';
}
$urlCache = $settings
->get('urlCache') ?: '';
if ($urlCache === '') {
$standaloneSrc = '//' . (isset($_COOKIE['N1ED_PREFIX']) ? $_COOKIE['N1ED_PREFIX'] . '.' : '') . 'cloud.n1ed.com/cdn/' . $apiKey . '/' . 'n1flmngr.js';
$standaloneImgPen = '//' . (isset($_COOKIE['N1ED_PREFIX']) ? $_COOKIE['N1ED_PREFIX'] . '.' : '') . 'cloud.n1ed.com/cdn/' . $apiKey . '/' . 'n1imgpen.js';
}
else {
if (strpos($urlCache, 'http:') === 0) {
$urlCache = substr($urlCache, 5);
}
elseif (strpos($urlCache, 'https:') === 0) {
$urlCache = substr($urlCache, 6);
}
$standaloneSrc = $urlCache . $apiKey . '/' . 'n1flmngr.js';
$standaloneSrc = $urlCache . $apiKey . '/' . 'n1imgpen.js';
}
$standaloneUrl = Url::fromRoute('n1ed.flmngr')
->toString();
$standaloneCSRF = 'drupal8';
$standaloneUrlFiles = parse_url(file_create_url('public://flmngr'))['path'];
$standaloneDirUploads = '/';
if ($settings
->get('selfHosted')) {
$standaloneSrc = selfHostedPath . 'Flmngr/flmngr.js';
$standaloneImgPen = selfHostedPath . 'ImgPen/imgpen.js';
}
$standaloneData = [
'standaloneSrc' => $standaloneSrc,
'standaloneUrl' => $standaloneUrl,
'standaloneCSRF' => $standaloneCSRF,
'standaloneUrlFiles' => $standaloneUrlFiles,
'standaloneDirUploads' => $standaloneDirUploads,
'standaloneimgpen' => $standaloneImgPen,
];
if ($settings
->get('selfHosted')) {
$standaloneData['apikey'] = $apiKey;
}
$element['n1ed_paths'] = [
'#type' => 'hidden',
'#attributes' => [
'class' => [
'flmngrWidget-filefield-paths',
],
'data-extensions' => $element['#upload_validators']['file_validate_extensions'][0],
'data-standaloneData' => json_encode($standaloneData),
'data-multiple' => $element['#multiple'] ? 1 : 0,
],
'#value' => '',
];
$element['#attached']['library'][] = 'n1ed/drupal.n1ed.filefield';
$element['#pre_render'][] = [
get_called_class(),
'preRenderWidget',
];
return $element;
}
public static function preRenderWidget($element) {
if (!empty($element['#value']['fids'])) {
$element['n1ed_paths']['#access'] = false;
}
return $element;
}
}