View source
<?php
namespace Drupal\lightning_media\Plugin\EntityBrowser\Widget;
use Drupal\Core\Form\FormStateInterface;
use Drupal\file\Plugin\Field\FieldType\FileItem;
use Drupal\image\Plugin\Field\FieldType\ImageItem;
use Drupal\lightning_media\Element\AjaxUpload;
use Drupal\lightning_media\MediaHelper;
use Drupal\media\MediaInterface;
use Drupal\media\MediaTypeInterface;
class FileUpload extends EntityFormProxy {
protected function getCurrentValue(FormStateInterface $form_state) {
$value = parent::getCurrentValue($form_state);
return $value['fid'] ?? NULL;
}
protected function prepareEntities(array $form, FormStateInterface $form_state) {
$entities = parent::prepareEntities($form, $form_state);
$get_file = function (MediaInterface $entity) {
return MediaHelper::getSourceField($entity)->entity;
};
if ($this->configuration['return_file']) {
return array_map($get_file, $entities);
}
else {
return $entities;
}
}
public function getForm(array &$original_form, FormStateInterface $form_state, array $additional_widget_parameters) {
$form = parent::getForm($original_form, $form_state, $additional_widget_parameters);
$form['input'] = [
'#type' => 'ajax_upload',
'#title' => $this
->t('File'),
'#required' => TRUE,
'#process' => [
[
$this,
'processUploadElement',
],
],
'#upload_validators' => $this
->getUploadValidators(),
'#weight' => 70,
];
return $form;
}
protected function getUploadValidators() {
$validators = $this->configuration['upload_validators'];
if (empty($validators['file_validate_extensions'])) {
return array_merge([
'file_validate_extensions' => [
implode(' ', $this
->getAllowedFileExtensions()),
],
], $validators);
}
return $validators;
}
protected function getAllowedFileExtensions() {
$extensions = '';
foreach ($this
->getAllowedTypes() as $media_type) {
$extensions .= $media_type
->getSource()
->getSourceFieldDefinition($media_type)
->getSetting('file_extensions') . ' ';
}
$extensions = preg_split('/,?\\s+/', rtrim($extensions));
return array_unique($extensions);
}
public function validate(array &$form, FormStateInterface $form_state) {
$fid = $this
->getCurrentValue($form_state);
if ($fid) {
parent::validate($form, $form_state);
$media = $this
->getCurrentEntity($form_state);
if ($media) {
foreach ($this
->validateFile($media) as $error) {
$form_state
->setError($form['widget']['input'], $error);
}
}
}
}
protected function validateFile(MediaInterface $media) {
$field = $media
->getSource()
->getSourceFieldDefinition($media->bundle->entity)
->getName();
$item = $media
->get($field)
->first();
$validators = [
'file_validate_name_length' => [],
];
$validators = array_merge($validators, $item
->getUploadValidators());
unset($validators['file_validate_extensions']);
if ($item instanceof ImageItem) {
$validators['file_validate_is_image'] = [];
$settings = $item
->getFieldDefinition()
->getSettings();
if ($settings['max_resolution'] || $settings['min_resolution']) {
$validators['file_validate_image_resolution'] = [
$settings['max_resolution'],
$settings['min_resolution'],
];
}
}
return file_validate($item->entity, $validators);
}
public function submit(array &$element, array &$form, FormStateInterface $form_state) {
$entity = $element['entity']['#entity'];
$file = MediaHelper::useFile($entity, MediaHelper::getSourceField($entity)->entity);
$file
->setPermanent();
$file
->save();
$entity
->save();
$selection = [
$this->configuration['return_file'] ? $file : $entity,
];
$this
->selectEntities($selection, $form_state);
}
public function processUploadElement(array $element, FormStateInterface $form_state) {
$element = AjaxUpload::process($element, $form_state);
$element['upload_button']['#ajax']['callback'] = $element['remove']['#ajax']['callback'] = [
static::class,
'ajax',
];
$element['remove']['#value'] = $this
->t('Cancel');
return $element;
}
public function defaultConfiguration() {
$configuration = parent::defaultConfiguration();
$configuration['return_file'] = FALSE;
$configuration['upload_validators'] = [];
return $configuration;
}
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$form = parent::buildConfigurationForm($form, $form_state);
$form['return_file'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Return source file entity'),
'#default_value' => $this->configuration['return_file'],
'#description' => $this
->t('If checked, the source file(s) of the media entity will be returned from this widget.'),
];
return $form;
}
protected function isAllowedType(MediaTypeInterface $media_type) {
$is_allowed = parent::isAllowedType($media_type);
if ($is_allowed) {
$item_class = $media_type
->getSource()
->getSourceFieldDefinition($media_type)
->getItemDefinition()
->getClass();
$is_allowed = is_a($item_class, FileItem::class, TRUE);
}
return $is_allowed;
}
}