Skip to content

Commit

Permalink
Merge pull request #34 from neos/improve-file-upload
Browse files Browse the repository at this point in the history
FEATURE: Use accept attribute to select the correct file types on uploads
  • Loading branch information
markusguenther authored Jan 8, 2025
2 parents d695e0a + c9265d8 commit c160c99
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 2 deletions.
29 changes: 28 additions & 1 deletion Classes/Eel/Helper/FormHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Neos\Form\Core\Model\Renderable\RootRenderableInterface;
use Neos\Form\Core\Runtime\FormRuntime;
use Neos\Utility\ObjectAccess;
use Symfony\Component\Mime\MimeTypes;

/**
* Eel Helper with some convenience methods for Fusion based Form rendering
Expand Down Expand Up @@ -199,6 +200,32 @@ public function escape(string $string): string
return htmlspecialchars($string, ENT_QUOTES);
}

/**
* Get accept string for input attribute accept based on the allowed extensions array
*
* @param array $allowedExtensions
* @param bool $asFileExtension
* @param bool $asMimeType
* @return string
*/
public function getAcceptFromAllowedExtensions(array $allowedExtensions, bool $asFileExtension = true, bool $asMimeType = true): string {
$accept = [];

foreach ($allowedExtensions as $key => $extension) {
if ($asFileExtension) {
$accept[] = '.' . $extension;
}

if ($asMimeType) {
$mimeTypes = new MimeTypes();
$mimeType = $mimeTypes->getMimeTypes($extension);
$accept[] = implode(',', $mimeType);
}
}

return implode(',', $accept);
}

/**
* @param string $methodName
* @return boolean
Expand All @@ -207,4 +234,4 @@ public function allowsCallOfMethod($methodName): bool
{
return true;
}
}
}
1 change: 1 addition & 0 deletions Resources/Private/Fusion/Elements/FileUpload.fusion
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ prototype(Neos.Form:FileUpload) < prototype(Neos.Form.FusionRenderer:FormElement
tagName = 'input'
attributes {
type = 'file'
accept = ${Neos.Form.FusionRenderer.getAcceptFromAllowedExtensions(element.properties.allowedExtensions)}
name = ${elementName}
}
}
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"license": "MIT",
"require": {
"neos/form": "^5.0",
"neos/fusion": "^7.3 || ^8.0 || ^9.0"
"neos/fusion": "^7.3 || ^8.0 || ^9.0",
"symfony/mime": "^7.0"
},
"autoload": {
"psr-4": {
Expand Down

0 comments on commit c160c99

Please sign in to comment.