Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions docs/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ vich_uploader:
This is the minimal amount of configuration needed in order to describe a
working mapping.

> [!NOTE]
> If the `upload_destination` parameter is missing, it is set automatically
> with the `%kernel.project_dir%/public` and the `uri_prefix` value.

## Step 2: link the upload mapping to an entity

The final step is to create a link between the filesystem and the entity you
Expand Down
2 changes: 1 addition & 1 deletion src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ private function addMappingsSection(ArrayNodeDefinition $node): void
->prototype('array')
->children()
->scalarNode('uri_prefix')->defaultValue('/uploads')->end()
->scalarNode('upload_destination')->isRequired()->end()
->scalarNode('upload_destination')->defaultNull()->end()
->arrayNode('namer')
->addDefaultsIfNotSet()
->beforeNormalization()
Expand Down
14 changes: 14 additions & 0 deletions src/DependencyInjection/VichUploaderExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public function load(array $configs, ContainerBuilder $container): void
$config = $this->processConfiguration($configuration, $configs);

$config = $this->fixDbDriverConfig($config);
$config = $this->fixUploadDestinationConfig($container, $config);
$config = $this->createNamerServices($container, $config);

// define a few parameters
Expand Down Expand Up @@ -200,6 +201,19 @@ protected function fixDbDriverConfig(array $config): array
return $config;
}

protected function fixUploadDestinationConfig(ContainerBuilder $container, array $config): array
{
// mapping with no declared upload_destination uses the uri_prefix
$prefix = $container->getParameter('kernel.project_dir') ?? '';
foreach ($config['mappings'] as &$mapping) {
if (!isset($mapping['upload_destination'])) {
$mapping['upload_destination'] = $prefix.'/public/'.$mapping['uri_prefix'];
}
}

return $config;
}

protected function registerListeners(ContainerBuilder $container, array $config): void
{
$servicesMap = [
Expand Down