-
Notifications
You must be signed in to change notification settings - Fork 262
/
TransUnitFormHandler.php
89 lines (75 loc) · 2.7 KB
/
TransUnitFormHandler.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
<?php
namespace Lexik\Bundle\TranslationBundle\Form\Handler;
use Lexik\Bundle\TranslationBundle\Manager\LocaleManagerInterface;
use Lexik\Bundle\TranslationBundle\Manager\TransUnitManagerInterface;
use Lexik\Bundle\TranslationBundle\Manager\FileInterface;
use Lexik\Bundle\TranslationBundle\Manager\FileManagerInterface;
use Lexik\Bundle\TranslationBundle\Storage\StorageInterface;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\HttpFoundation\Request;
/**
* @author Cédric Girard <[email protected]>
*/
class TransUnitFormHandler implements FormHandlerInterface
{
/**
* @param string $rootDir
*/
public function __construct(
protected TransUnitManagerInterface $transUnitManager,
protected FileManagerInterface $fileManager,
protected StorageInterface $storage,
protected LocaleManagerInterface $localeManager,
protected string $rootDir,
) {
}
/**
* {@inheritdoc}
*/
public function createFormData()
{
return $this->transUnitManager->newInstance($this->localeManager->getLocales());
}
/**
* {@inheritdoc}
*/
public function getFormOptions()
{
return [
'domains' => $this->storage->getTransUnitDomains(),
'data_class' => $this->storage->getModelClass('trans_unit'),
'translation_class' => $this->storage->getModelClass('translation'),
];
}
/**
* {@inheritdoc}
*/
public function process(FormInterface $form, Request $request)
{
$valid = false;
if ($request->isMethod('POST')) {
$form->handleRequest($request);
if ($form->isValid()) {
$transUnit = $form->getData();
$translations = $transUnit->filterNotBlankTranslations(); // only keep translations with a content
// link new translations to a file to be able to export them.
foreach ($translations as $translation) {
if (!$translation->getFile()) {
$file = $this->fileManager->getFor(
sprintf('%s.%s.yml', $transUnit->getDomain(), $translation->getLocale()),
$this->rootDir . '/Resources/translations'
);
if ($file instanceof FileInterface) {
$translation->setFile($file);
}
}
}
$transUnit->setTranslations($translations);
$this->storage->persist($transUnit);
$this->storage->flush();
$valid = true;
}
}
return $valid;
}
}