Skip to content

Instantly share code, notes, and snippets.

@lorenzulrich
Created January 18, 2022 16:34
Show Gist options
  • Save lorenzulrich/b1110349d19d458d3c4beec4839de45a to your computer and use it in GitHub Desktop.
Save lorenzulrich/b1110349d19d458d3c4beec4839de45a to your computer and use it in GitHub Desktop.

Assumed folder/file structure:

DistributionPackages/My.FoobarCom/Configuration/Settings.Form.yaml
DistributionPackages/My.FoobarCom/Classes/Form/FormElements/FileUpload.php
DistributionPackages/My.FoobarCom/Classes/Form/FormElements/ImageUpload.php
DistributionPackages/My.FoobarCom/Classes/Form/Validation/FileTypeValidator.php
DistributionPackages/My.FoobarCom/Classes/Form/Validation/ImageTypeValidator.php

The code is based on Neos 4.3 LTS. I assume some changes (regarding type hints) need to be made for Neos 7.

<?php
namespace My\FoobarCom\Form\Validation;
/* *
* This script belongs to the TYPO3 Flow package "Neos.Form". *
* *
* It is free software; you can redistribute it and/or modify it under *
* the terms of the GNU Lesser General Public License, either version 3 *
* of the License, or (at your option) any later version. *
* *
* The TYPO3 project - inspiring people to share! *
* */
use Neos\Flow\Annotations as Flow;
use Neos\Flow\ResourceManagement\ResourceManager;
/**
* The given $value is valid if it is an \Neos\Flow\ResourceManagement\PersistentResource of the configured resolution
* Note: a value of NULL or empty string ('') is considered valid
*/
class FileTypeValidator extends \Neos\Flow\Validation\Validator\AbstractValidator
{
/**
* @var ResourceManager
* @Flow\Inject
*/
protected $resourceManager;
/**
* @var array
*/
protected $supportedOptions = [
'allowedExtensions' => [[], 'Array of allowed file extensions', 'array', true]
];
/**
* The given $value is valid if it is an \Neos\Flow\ResourceManagement\PersistentResource of the configured resolution
* Note: a value of NULL or empty string ('') is considered valid
*
* @param \Neos\Flow\ResourceManagement\PersistentResource $resource The resource object that should be validated
* @return void
* @api
*/
protected function isValid($resource)
{
if (!$resource instanceof \Neos\Flow\ResourceManagement\PersistentResource) {
$this->addError('The given value was not a Resource instance.', 1327865587);
return;
}
$fileExtension = $resource->getFileExtension();
if ($fileExtension === null || $fileExtension === '') {
$this->addError(
'Die Datei <strong>"' . $resource->getFilename() . '"</strong> hat keine gültige Endung.',
1327865808
);
// Remove the PersistentResource to clean up
$this->resourceManager->deleteResource($resource);
return;
}
if (!in_array($fileExtension, $this->options['allowedExtensions'])) {
$this->addError(
'Wir können <strong>"' . $resource->getFilename(
) . '"</strong> leider nicht verarbeiten.<br />Möglich sind Dateien des Typs: <strong>' . implode(
', ',
$this->options['allowedExtensions']
) . '</strong>',
1327865764,
[$resource->getFileExtension()]
);
// Remove the PersistentResource to clean up
$this->resourceManager->deleteResource($resource);
return;
}
}
}
<?php
namespace My\FoobarCom\Form\FormElements;
/* *
* This script belongs to the TYPO3 Flow package "Neos.Form". *
* *
* It is free software; you can redistribute it and/or modify it under *
* the terms of the GNU Lesser General Public License, either version 3 *
* of the License, or (at your option) any later version. *
* *
* The TYPO3 project - inspiring people to share! *
* */
use Neos\Flow\Annotations as Flow;
/**
* A generic file upload form element with custom fileTypeValidator
*/
class FileUpload extends \Neos\Form\Core\Model\AbstractFormElement
{
/**
* @return void
*/
public function initializeFormElement()
{
$this->setDataType('Neos\Flow\ResourceManagement\PersistentResource');
$fileTypeValidator = new \My\FoobarCom\Form\Validation\FileTypeValidator(array('allowedExtensions' => $this->properties['allowedExtensions']));
$this->addValidator($fileTypeValidator);
}
}
<?php
namespace My\FoobarCom\Form\Validation;
/*
* This file is part of the Neos.Media package.
*
* (c) Contributors of the Neos Project - www.neos.io
*
* This package is Open Source Software. For the full copyright and license
* information, please view the LICENSE file which was distributed with this
* source code.
*/
use Neos\Flow\Annotations as Flow;
use Neos\Flow\Persistence\PersistenceManagerInterface;
use Neos\Flow\ResourceManagement\ResourceManager;
use Neos\Media\Domain\Model\Image;
use Neos\Media\Domain\Model\ImageInterface;
use Neos\Media\Domain\Repository\ImageRepository;
/**
* Validator that checks the type of a given image
*
* Example:
* [at]Flow\Validate("$image", type="\Neos\Media\Validator\ImageTypeValidator", options={ "allowedTypes"={"jpeg", "png"} })
*/
class ImageTypeValidator extends \Neos\Media\Validator\ImageTypeValidator
{
/**
* @var ImageRepository
* @Flow\Inject
*/
protected $imageRepository;
/**
* @var ResourceManager
* @Flow\Inject
*/
protected $resourceManager;
/**
* @var PersistenceManagerInterface
* @Flow\Inject
*/
protected $persistenceManager;
/**
* The given $value is valid if it is an \Neos\Media\Domain\Model\ImageInterface of the
* configured type (one of the image/* IANA media subtypes)
*
* Note: a value of NULL or empty string ('') is considered valid
*
* @param ImageInterface $image The image that should be validated
* @return void
* @api
*/
protected function isValid($image)
{
$this->validateOptions();
if (!$image instanceof Image) {
$this->addError('The given value was not an Image instance.', 1327947256);
return;
}
$allowedImageTypes = $this->options['allowedTypes'];
array_walk(
$allowedImageTypes,
function (&$value) {
$value = 'image/' . $value;
}
);
if (!in_array($image->getMediaType(), $allowedImageTypes)) {
$this->addError('The media type "%s" is not allowed for this image.', 1327947647, [$image->getMediaType()]);
// Remove the PersistentResource to clean up
$this->imageRepository->remove($image);
$this->resourceManager->deleteResource($image->getResource());
}
}
}
<?php
namespace My\FoobarCom\Form\FormElements;
/* *
* This script belongs to the TYPO3 Flow package "Neos.Form". *
* *
* It is free software; you can redistribute it and/or modify it under *
* the terms of the GNU Lesser General Public License, either version 3 *
* of the License, or (at your option) any later version. *
* *
* The TYPO3 project - inspiring people to share! *
* */
use Neos\Form\Core\Runtime\FormRuntime;
use My\FoobarCom\Form\Validation\ImageTypeValidator;
/**
* A generic file upload form element
*/
class ImageUpload extends \Neos\Form\FormElements\ImageUpload
{
/**
* Add ImageTypeValidator just before submitting so that the "allowedTypes" can be changed at runtime
*
* @param FormRuntime $formRuntime
* @param mixed $elementValue
* @return void
*/
public function onSubmit(FormRuntime $formRuntime, &$elementValue)
{
$imageTypeValidator = new ImageTypeValidator(['allowedTypes' => $this->properties['allowedTypes']]);
$this->addValidator($imageTypeValidator);
}
}
Neos:
Form:
presets:
default:
formElementTypes:
'Neos.Form:FileUpload':
implementationClassName: My\FoobarCom\Form\FormElements\FileUpload
'Neos.Form:ImageUpload':
implementationClassName: My\FoobarCom\Form\FormElements\ImageUpload
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment