Skip to content

Commit 5af98de

Browse files
author
Hrvoje Knežević
committed
Merged in ezforms_fieldtype_handler (pull request #3)
Implemented FieldTypeHandler for use with EzFormsBundle
2 parents 84c6195 + 7064015 commit 5af98de

File tree

5 files changed

+125
-5
lines changed

5 files changed

+125
-5
lines changed

DependencyInjection/NetgenEnhancedBinaryFileExtension.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ public function load( array $configs, ContainerBuilder $container )
1919

2020
$loader = new Loader\YamlFileLoader( $container, new FileLocator( __DIR__ . '/../Resources/config' ) );
2121
$loader->load( 'fieldtypes.yml' );
22+
$loader->load( 'field_type_handlers.yml' );
2223
$loader->load( 'storage_engines.yml' );
2324
$loader->load( 'mime.yml' );
2425

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
<?php
2+
3+
namespace Netgen\Bundle\EnhancedBinaryFileBundle\Form\FieldTypeHandler;
4+
5+
use eZ\Publish\Core\MVC\ConfigResolverInterface;
6+
use Netgen\Bundle\EzFormsBundle\Form\FieldTypeHandler;
7+
use Symfony\Component\Form\FormBuilderInterface;
8+
use eZ\Publish\API\Repository\Values\Content\Content;
9+
use eZ\Publish\API\Repository\Values\ContentType\FieldDefinition;
10+
use eZ\Publish\SPI\FieldType\Value;
11+
use Symfony\Component\Validator\Constraints;
12+
use Symfony\Component\HttpFoundation\File\UploadedFile;
13+
14+
/**
15+
* Class EnhancedFile
16+
*/
17+
class EnhancedFile extends FieldTypeHandler
18+
{
19+
20+
/**
21+
* @param \eZ\Publish\Core\MVC\ConfigResolverInterface $configResolver
22+
*/
23+
public function __construct( ConfigResolverInterface $configResolver )
24+
{
25+
$this->configResolver = $configResolver;
26+
}
27+
28+
/**
29+
* {@inheritdoc}
30+
*
31+
* @param \eZ\Publish\Core\FieldType\Image\Value $value
32+
*/
33+
public function convertFieldValueToForm( Value $value )
34+
{
35+
return null;
36+
}
37+
38+
/**
39+
* {@inheritdoc}
40+
*
41+
* @param UploadedFile $data
42+
*/
43+
public function convertFieldValueFromForm( $data )
44+
{
45+
if ( $data === null )
46+
{
47+
return null;
48+
}
49+
50+
return array(
51+
"inputUri" => $data->getFileInfo()->getRealPath(),
52+
"fileName" => $data->getClientOriginalName(),
53+
"fileSize" => $data->getSize(),
54+
"mimeType" => $data->getClientMimeType(),
55+
);
56+
}
57+
58+
/**
59+
* {@inheritdoc}
60+
*/
61+
protected function buildFieldForm(
62+
FormBuilderInterface $formBuilder,
63+
FieldDefinition $fieldDefinition,
64+
$languageCode,
65+
Content $content = null
66+
)
67+
{
68+
$options = $this->getDefaultFieldOptions( $fieldDefinition, $languageCode, $content );
69+
70+
$maxFileSize = $fieldDefinition->validatorConfiguration["FileSizeValidator"]["maxFileSize"];
71+
$allowedExtensions = $fieldDefinition->fieldSettings['allowedTypes'];
72+
73+
if ( $maxFileSize !== false || !empty( $allowedExtensions ) )
74+
{
75+
$constraints = array();
76+
77+
if ( $maxFileSize !== false )
78+
{
79+
$constraints['maxSize'] = $maxFileSize;
80+
}
81+
82+
if ( !empty( $allowedExtensions ) )
83+
{
84+
$allowedExtensions = explode( '|', $allowedExtensions );
85+
86+
$allowedMimeTypes = array();
87+
88+
foreach( $allowedExtensions as $allowedExtension )
89+
{
90+
if ( $this->configResolver->hasParameter( "{$allowedExtension}.Types", 'mime' ) )
91+
{
92+
$allowedMimeTypes = array_merge( $allowedMimeTypes, $this->configResolver->getParameter( "{$allowedExtension}.Types", 'mime' ) );
93+
}
94+
}
95+
$constraints['mimeTypes'] = $allowedMimeTypes;
96+
}
97+
98+
$options["constraints"][] = new Constraints\File( $constraints );
99+
}
100+
101+
// EnhancedBinaryFile should not be erased (updated as empty) if nothing is selected in file input
102+
$this->skipEmptyUpdate( $formBuilder, $fieldDefinition->identifier );
103+
104+
$formBuilder->add( $fieldDefinition->identifier, "file", $options );
105+
}
106+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
parameters:
2+
netgen.ezforms.form.fieldtype_handler.enhancedezbinaryfile.class: Netgen\Bundle\EnhancedBinaryFileBundle\Form\FieldTypeHandler\EnhancedFile
3+
4+
services:
5+
netgen.ezforms.form.fieldtype_handler.enhancedezbinaryfile:
6+
class: %netgen.ezforms.form.fieldtype_handler.enhancedezbinaryfile.class%
7+
arguments:
8+
- "@ezpublish.config.resolver"
9+
tags:
10+
- {name: netgen.ezforms.form.fieldtype_handler, alias: enhancedezbinaryfile}

Resources/config/fieldtypes.yml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ services:
77
class: %ezpublish.fieldType.enhancedezbinaryfile.class%
88
parent: ezpublish.fieldType
99
arguments:
10-
- @ezpublish.core.io.mimetypedetector
11-
- @ezpublish.config.resolver
10+
- "@ezpublish.core.io.mimetypedetector"
11+
- "@ezpublish.config.resolver"
1212
tags:
1313
- {name: ezpublish.fieldType, alias: enhancedezbinaryfile}
1414
ezpublish.fieldType.enhancedezbinaryfile.storage_gateway:
@@ -20,8 +20,8 @@ services:
2020
class: eZ\Publish\Core\FieldType\BinaryBase\BinaryBaseStorage
2121
arguments:
2222
- []
23-
- @ezpublish.fieldType.ezbinaryfile.io_service
24-
- @ezpublish.fieldType.ezbinaryfile.pathGenerator
25-
- @ezpublish.core.io.mimeTypeDetector
23+
- "@ezpublish.fieldType.ezbinaryfile.io_service"
24+
- "@ezpublish.fieldType.ezbinaryfile.pathGenerator"
25+
- "@ezpublish.core.io.mimeTypeDetector"
2626
tags:
2727
- {name: ezpublish.fieldType.externalStorageHandler, alias: enhancedezbinaryfile }

composer.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
"ezsystems/ezpublish-kernel": "*",
1313
"netgen/enhancedezbinaryfile": "~4.5"
1414
},
15+
"suggest": {
16+
"netgen/ez-forms-bundle": "Allows use of Enhanced Binary File with Symfony forms"
17+
},
1518
"autoload": {
1619
"psr-4": { "Netgen\\Bundle\\EnhancedBinaryFileBundle\\": "" }
1720
}

0 commit comments

Comments
 (0)