Skip to content

Commit d811b78

Browse files
Set default upload destination (#1527)
1 parent 3a11996 commit d811b78

3 files changed

Lines changed: 19 additions & 1 deletion

File tree

docs/usage.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ vich_uploader:
4141
This is the minimal amount of configuration needed in order to describe a
4242
working mapping.
4343
44+
> [!NOTE]
45+
> If the `upload_destination` parameter is missing, it is set automatically
46+
> with the `%kernel.project_dir%/public` and the `uri_prefix` value.
47+
4448
## Step 2: link the upload mapping to an entity
4549

4650
The final step is to create a link between the filesystem and the entity you

src/DependencyInjection/Configuration.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ private function addMappingsSection(ArrayNodeDefinition $node): void
103103
->prototype('array')
104104
->children()
105105
->scalarNode('uri_prefix')->defaultValue('/uploads')->end()
106-
->scalarNode('upload_destination')->isRequired()->end()
106+
->scalarNode('upload_destination')->defaultNull()->end()
107107
->arrayNode('namer')
108108
->addDefaultsIfNotSet()
109109
->beforeNormalization()

src/DependencyInjection/VichUploaderExtension.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ public function load(array $configs, ContainerBuilder $container): void
3939
$config = $this->processConfiguration($configuration, $configs);
4040

4141
$config = $this->fixDbDriverConfig($config);
42+
$config = $this->fixUploadDestinationConfig($container, $config);
4243
$config = $this->createNamerServices($container, $config);
4344

4445
// define a few parameters
@@ -200,6 +201,19 @@ protected function fixDbDriverConfig(array $config): array
200201
return $config;
201202
}
202203

204+
protected function fixUploadDestinationConfig(ContainerBuilder $container, array $config): array
205+
{
206+
// mapping with no declared upload_destination uses the uri_prefix
207+
$prefix = $container->getParameter('kernel.project_dir') ?? '';
208+
foreach ($config['mappings'] as &$mapping) {
209+
if (!isset($mapping['upload_destination'])) {
210+
$mapping['upload_destination'] = $prefix.'/public/'.$mapping['uri_prefix'];
211+
}
212+
}
213+
214+
return $config;
215+
}
216+
203217
protected function registerListeners(ContainerBuilder $container, array $config): void
204218
{
205219
$servicesMap = [

0 commit comments

Comments
 (0)