1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the Sonata Project package. |
7
|
|
|
* |
8
|
|
|
* (c) Thomas Rabaix <[email protected]> |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Sonata\TranslationBundle\Admin\Extension\Phpcr; |
15
|
|
|
|
16
|
|
|
use Doctrine\ODM\PHPCR\DocumentManager; |
17
|
|
|
use Doctrine\ODM\PHPCR\Translation\LocaleChooser\LocaleChooser; |
18
|
|
|
use Sonata\AdminBundle\Admin\AdminInterface; |
19
|
|
|
use Sonata\AdminBundle\Datagrid\ProxyQueryInterface; |
20
|
|
|
use Sonata\TranslationBundle\Admin\Extension\AbstractTranslatableAdminExtension; |
21
|
|
|
use Sonata\TranslationBundle\Checker\TranslatableChecker; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @author Nicolas Bastien <[email protected]> |
25
|
|
|
*/ |
26
|
|
|
class TranslatableAdminExtension extends AbstractTranslatableAdminExtension |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* @var LocaleChooser |
30
|
|
|
*/ |
31
|
|
|
private $localeChooser; |
32
|
|
|
|
33
|
|
|
public function __construct(TranslatableChecker $translatableChecker, LocaleChooser $localeChooser) |
34
|
|
|
{ |
35
|
|
|
parent::__construct($translatableChecker); |
36
|
|
|
$this->localeChooser = $localeChooser; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* {@inheritdoc} |
41
|
|
|
*/ |
42
|
|
|
public function alterObject(AdminInterface $admin, $object): void |
43
|
|
|
{ |
44
|
|
|
$locale = $this->getTranslatableLocale($admin); |
45
|
|
|
$documentManager = $this->getDocumentManager($admin); |
46
|
|
|
$unitOfWork = $documentManager->getUnitOfWork(); |
47
|
|
|
|
48
|
|
|
if ( |
49
|
|
|
$this->getTranslatableChecker()->isTranslatable($object) |
50
|
|
|
&& ($unitOfWork->getCurrentLocale($object) !== $locale) |
51
|
|
|
) { |
52
|
|
|
$object = $this->getDocumentManager($admin)->findTranslation($admin->getClass(), $object->getId(), $locale); |
53
|
|
|
|
54
|
|
|
// if the translation did not yet exists, the locale will be |
55
|
|
|
// the fallback locale. This makes sure the new locale is set. |
56
|
|
|
if ($unitOfWork->getCurrentLocale($object) !== $locale) { |
57
|
|
|
$documentManager->bindTranslation($object, $locale); |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* {@inheritdoc} |
64
|
|
|
*/ |
65
|
|
|
public function configureQuery(AdminInterface $admin, ProxyQueryInterface $query, $context = 'list'): void |
66
|
|
|
{ |
67
|
|
|
$this->localeChooser->setLocale($this->getTranslatableLocale($admin)); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @return DocumentManager |
72
|
|
|
*/ |
73
|
|
|
protected function getDocumentManager(AdminInterface $admin) |
74
|
|
|
{ |
75
|
|
|
return $admin->getModelManager()->getDocumentManager(); |
|
|
|
|
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.