View source
<?php
namespace Drupal\s3fs_cors\Plugin\Field\FieldWidget;
use Drupal\Component\Utility\Bytes;
use Drupal\Core\Form\FormStateInterface;
use Drupal\file\Plugin\Field\FieldWidget\FileWidget;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\s3fs_cors\Element\S3fsCorsFile;
use Drupal\Core\StringTranslation\StringTranslationTrait;
class S3fsCorsFileWidget extends FileWidget {
use StringTranslationTrait;
public static function defaultSettings() {
$settings = parent::defaultSettings();
$settings['max_filesize'] = '';
return $settings;
}
public function settingsForm(array $form, FormStateInterface $form_state) {
$elements = parent::settingsForm($form, $form_state);
$elements['max_filesize'] = [
'#type' => 'textfield',
'#title' => $this
->t('Maximum upload size'),
'#description' => $this
->t('Enter a value like "512" (bytes), "80 KB" (kilobytes), "50 MB" (megabytes) or "2 GB" (gigabytes) in order to restrict the allowed file size. This value will override any value specified in the field configuration settings.'),
'#default_value' => $this
->getSetting('max_filesize'),
'#size' => 10,
'#weight' => 5,
'#element_validate' => [
[
$this,
'settingsMaxFilesizeValidate',
],
],
];
return $elements;
}
public function settingsMaxFilesizeValidate($element, FormStateInterface $form_state) {
$submitted_value = $form_state
->getValue($element['#parents']);
if ($submitted_value) {
$matches = [];
if (preg_match('/(\\d+)(.*)/', $submitted_value, $matches)) {
$suffix = strtoupper(trim($matches[2]));
if ($suffix && !in_array($suffix, [
'B',
'KB',
'MB',
'GB',
])) {
$form_state
->setError($element, $this
->t('Invalid numeric value or size suffix. Specify an integer followed by "KB", "MB" or "GB".'));
return;
}
}
$max_filesize = Bytes::toInt($submitted_value);
if (!$max_filesize) {
$form_state
->setError($element, $this
->t('Invalid max filesize. Enter a value like "512" (bytes), "80 KB" (kilobytes), "50 MB" (megabytes) or "2 GB" (gigabytes).'));
}
if ($max_filesize > Bytes::toInt('5 GB')) {
$form_state
->setError($element, $this
->t('Invalid max filesize. 5 GB is largest file size current supported.'));
}
}
}
public function settingsSummary() {
$summary = parent::settingsSummary();
if ($this
->getSetting('max_filesize')) {
$summary[] = $this
->t('Max upload filesize: @max_filesize (override field configuration value).', [
'@max_filesize' => $this
->getSetting('max_filesize'),
]);
}
else {
$summary[] = $this
->t('Max upload filesize not specified (use field configuration value).');
}
return $summary;
}
public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
$element = parent::formElement($items, $delta, $element, $form, $form_state);
$element_info = $this->elementInfo
->getInfo('s3fs_cors_file');
$max_filesize = $this
->getSetting('max_filesize') ?: $element['#upload_validators']['file_validate_size'][0];
$element['#element_validate'] = [
[
'\\Drupal\\s3fs_cors\\Element\\S3fsCorsFile',
'validateManagedFile',
],
];
$element['#type'] = 's3fs_cors_file';
$element['#process'][] = $element_info['#process'][0];
$element['#process'][] = $element['#process'][1];
$element['#max_filesize'] = $max_filesize;
$element['#upload_validators']['file_validate_size'] = [
$max_filesize,
];
$element['#attributes'] = [
'class' => [
's3fs-cors-file',
],
];
return $element;
}
public static function value($element, $input = FALSE, FormStateInterface $form_state = NULL) {
if ($input) {
if (empty($input['display'])) {
$input['display'] = $element['#display_field'] ? 0 : 1;
}
if (in_array('upload', array_keys($input)) && is_null($input['upload'])) {
$input = FALSE;
}
}
$return = S3fsCorsFile::valueCallback($element, $input, $form_state);
$return += [
'fids' => [],
'display' => 1,
'description' => '',
];
return $return;
}
}