Skip to content

Commit 4b88b23

Browse files
authored
VichFileType : Fix translation for 'delete_label' when 'download_label' is a callable #1573 (#1574)
1 parent 0f1b3ee commit 4b88b23

File tree

9 files changed

+227
-98
lines changed

9 files changed

+227
-98
lines changed

phpstan-baseline.neon

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,8 @@ parameters:
6666
count: 4
6767
path: tests/TestCase.php
6868

69+
-
70+
message: '#^Return type of call to method PHPUnit\\Framework\\TestCase\:\:createMock\(\) contains unresolvable type\.$#'
71+
identifier: method.unresolvableReturnType
72+
count: 4
73+
path: tests/TestCaseTrait.php

src/Form/Type/VichFileType.php

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public function __construct(
3131
protected readonly StorageInterface $storage,
3232
protected readonly UploadHandler $handler,
3333
protected readonly PropertyMappingFactory $factory,
34-
?PropertyAccessorInterface $propertyAccessor = null
34+
?PropertyAccessorInterface $propertyAccessor = null,
3535
) {
3636
$this->propertyAccessor = $propertyAccessor ?: PropertyAccess::createPropertyAccessor();
3737
}
@@ -44,6 +44,9 @@ public function configureOptions(OptionsResolver $resolver): void
4444
'download_uri' => true,
4545
'download_label' => 'vich_uploader.link.download',
4646
'delete_label' => 'vich_uploader.form_label.delete_confirm',
47+
'download_label_translation_domain' => null,
48+
'delete_label_translation_domain' => null,
49+
'translation_domain' => null,
4750
'error_bubbling' => false,
4851
]);
4952

@@ -90,7 +93,7 @@ protected function buildDeleteField(FormBuilderInterface $builder, array $option
9093
$form->add('delete', Type\CheckboxType::class, [
9194
'label' => $options['delete_label'],
9295
'mapped' => false,
93-
'translation_domain' => $options['translation_domain'],
96+
'translation_domain' => $options['delete_label_translation_domain'] ?? $options['translation_domain'],
9497
'required' => false,
9598
]);
9699
});
@@ -117,9 +120,10 @@ public function buildView(FormView $view, FormInterface $form, array $options):
117120
$view->vars['download_uri'] = null;
118121
if ($options['download_uri'] && $object) {
119122
$view->vars['download_uri'] = $this->resolveUriOption($options['download_uri'], $object, $form);
123+
120124
$view->vars = \array_replace(
121125
$view->vars,
122-
$this->resolveDownloadLabel($options['download_label'], $object, $form)
126+
$this->resolveDownloadLabel($options['download_label'], $object, $form, $options)
123127
);
124128
}
125129

@@ -149,7 +153,7 @@ protected function resolveUriOption(mixed $uriOption, object $object, FormInterf
149153
return $uriOption;
150154
}
151155

152-
protected function resolveDownloadLabel(mixed $downloadLabel, object $object, FormInterface $form): array
156+
protected function resolveDownloadLabel(mixed $downloadLabel, object $object, FormInterface $form, array $options): array
153157
{
154158
if (true === $downloadLabel) {
155159
$fieldName = $this->getFieldName($form);
@@ -158,25 +162,32 @@ protected function resolveDownloadLabel(mixed $downloadLabel, object $object, Fo
158162
throw new \UnexpectedValueException(\sprintf('Cannot find mapping for "%s" field', $fieldName));
159163
}
160164

161-
return ['download_label' => $mapping->readProperty($object, 'originalName'), 'translation_domain' => false];
165+
return ['download_label' => $mapping->readProperty($object, 'originalName'), 'download_label_translation_domain' => false];
162166
}
163167

164168
if (\is_callable($downloadLabel)) {
165169
$result = $downloadLabel($object);
166170

167171
return [
168172
'download_label' => $result['download_label'] ?? $result,
169-
'translation_domain' => $result['translation_domain'] ?? false,
173+
'download_label_translation_domain' => $result['download_label_translation_domain']
174+
?? $result['translation_domain']
175+
?? false,
170176
];
171177
}
172178

173179
if ($downloadLabel instanceof PropertyPath) {
174180
return [
175181
'download_label' => $this->propertyAccessor->getValue($object, $downloadLabel),
176-
'translation_domain' => false,
182+
'download_label_translation_domain' => false,
177183
];
178184
}
179185

180-
return ['download_label' => $downloadLabel];
186+
return [
187+
'download_label' => $downloadLabel,
188+
'download_label_translation_domain' => $options['download_label_translation_domain']
189+
?? $options['translation_domain']
190+
?? null,
191+
];
181192
}
182193
}

src/Form/Type/VichImageType.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ public function buildView(FormView $view, FormInterface $form, array $options):
8686

8787
$view->vars = \array_replace(
8888
$view->vars,
89-
$this->resolveDownloadLabel($options['download_label'], $object, $form)
89+
$this->resolveDownloadLabel($options['download_label'], $object, $form, $options)
9090
);
9191

9292
$view->vars['download_uri'] = $this->resolveUriOption($options['download_uri'], $object, $form);

templates/Form/fields.html.twig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
{%- if download_uri -%}
1717
<a href="{{ asset_helper is same as(true) ? asset(download_uri) : download_uri }}" download>
18-
{{ translation_domain is same as(false) ? download_label : download_label|trans({}, translation_domain) }}
18+
{{ download_label_translation_domain is same as(false) ? download_label : download_label|trans({}, download_label_translation_domain) }}
1919
</a>
2020
{%- endif -%}
2121
</div>
@@ -43,7 +43,7 @@
4343
{%- endif -%}
4444
{%- if download_uri -%}
4545
<a href="{{ asset_helper is same as(true) ? asset(download_uri) : download_uri }}" download>
46-
{{ translation_domain is same as(false) ? download_label : download_label|trans({}, translation_domain) }}
46+
{{ download_label_translation_domain is same as(false) ? download_label : download_label|trans({}, download_label_translation_domain) }}
4747
</a>
4848
{%- endif -%}
4949
</div>

tests/Fixtures/TestBundle/src/Entity/Product.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class Product
1818

1919
private ?string $title = null;
2020

21-
public function getImage(): File
21+
public function getImage(): ?File
2222
{
2323
return $this->image;
2424
}

0 commit comments

Comments
 (0)