Skip to content
Snippets Groups Projects

Fix querying for maxFileSize of uploaded files

Merged Hasan Atak requested to merge issue/graphql_webform-3478477:3478477-fix-querying-for into 2.x
Files
2
@@ -5,6 +5,7 @@ declare(strict_types=1);
namespace Drupal\graphql_webform\Plugin\GraphQL\SchemaExtension;
use Drupal\Component\Utility\Bytes;
use Drupal\Component\Utility\Environment;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\graphql\GraphQL\ResolverBuilder;
@@ -288,19 +289,33 @@ class WebformExtension extends SdlSchemaExtensionPluginBase {
// Resolve the max filesize of managed file elements.
$registry->addFieldResolver('WebformElementManagedFileBase', 'maxFilesize',
$builder->callback(function (array $value): ?int {
$builder->callback(function (array $value): int {
// Check if the webform element has a max filesize set. If set this is
// representing the size in MB.
$size = $value['#max_filesize'] ?? NULL;
if ($size && is_numeric($size)) {
return (int) $size * 1024 * 1024;
}
$size = $this->getWebformSetting('file.default_max_filesize');
if (!empty($size)) {
$size = Bytes::toNumber($size);
if (!empty($size)) {
return (int) $size;
}
$size .= 'MB';
}
return NULL;
// Consider the other possible sources for the max filesize: the PHP
// limit and the default webform setting.
$sizes = array_filter([
$size,
// Ignore the PHP limit if a default max filesize is set on the form.
// This follows the functionality of the Webform module, but in case
// the PHP limit is lower than the default max filesize, the reported
// value will be incorrect.
// @todo Change this behavior when the bug is fixed in the Webform
// module.
// @see https://www.drupal.org/project/webform/issues/3482402
$this->getWebformSetting('file.default_max_filesize') ?: Environment::getUploadMaxSize(),
]);
// Convert to bytes.
$sizes = array_map(fn ($size): int => (int) Bytes::toNumber($size), $sizes);
// Return the smallest value.
return min($sizes);
})
);
Loading