16
16
use Doctrine \Common \Persistence \Event \LifecycleEventArgs ;
17
17
use Doctrine \Common \Persistence \Event \LoadClassMetadataEventArgs ;
18
18
use Doctrine \ODM \PHPCR \Mapping \ClassMetadata ;
19
- use Symfony \Cmf \Bundle \CoreBundle \Translatable \TranslatableInterface ;
20
19
21
20
/**
22
- * Metadata listener for when translations are disabled in PHPCR-ODM to remove
23
- * mapping information that makes fields being translated.
21
+ * Metadata listener for when the translations is globally defined
24
22
*
25
- * @author David Buchmann <[email protected] >
23
+ * @author Lukas Kahwe Smith <[email protected] >
26
24
*/
27
25
class TranslatableMetadataListener implements EventSubscriber
28
26
{
27
+ /**
28
+ * @var string
29
+ */
30
+ private $ translationStrategy ;
31
+
32
+ /**
33
+ * @param string $translationStrategy
34
+ */
35
+ public function __construct ($ translationStrategy )
36
+ {
37
+ $ this ->translationStrategy = $ translationStrategy ;
38
+ }
39
+
29
40
/**
30
41
* @return array
31
42
*/
32
43
public function getSubscribedEvents ()
33
44
{
34
45
return array (
35
46
'loadClassMetadata ' ,
36
- 'postLoad ' ,
37
47
);
38
48
}
39
49
40
50
/**
41
- * Handle the load class metadata event: remove translated attribute from
42
- * fields and remove the locale mapping if present.
51
+ * Handle the load class metadata event: set the translation strategy
43
52
*
44
53
* @param LoadClassMetadataEventArgs $eventArgs
45
54
*/
@@ -49,28 +58,7 @@ public function loadClassMetadata(LoadClassMetadataEventArgs $eventArgs)
49
58
$ meta = $ eventArgs ->getClassMetadata ();
50
59
51
60
if ($ meta ->getReflectionClass ()->implementsInterface ('Symfony\Cmf\Bundle\CoreBundle\Translatable\TranslatableInterface ' )) {
52
- foreach ($ meta ->translatableFields as $ field ) {
53
- unset($ meta ->mappings [$ field ]['translated ' ]);
54
- }
55
- $ meta ->translatableFields = array ();
56
- if (null !== $ meta ->localeMapping ) {
57
- unset($ meta ->mappings [$ meta ->localeMapping ]);
58
- $ meta ->localeMapping = null ;
59
- }
60
- }
61
- }
62
-
63
- /**
64
- * We set the locale field to false so that other code can use the
65
- * information that translations are deactivated.
66
- *
67
- * @param LifecycleEventArgs $eventArgs
68
- */
69
- public function postLoad (LifecycleEventArgs $ eventArgs )
70
- {
71
- $ object = $ eventArgs ->getObject ();
72
- if ($ object instanceof TranslatableInterface) {
73
- $ object ->setLocale (false );
61
+ $ meta ->setTranslator ($ this ->translationStrategy );
74
62
}
75
63
}
76
64
}
0 commit comments