Skip to content

Commit 0a23b80

Browse files
committed
Merge pull request #99 from symfony-cmf/translation_strategy
added support for setting a global PHPCR ODM translation strategy
2 parents e9c1a97 + 1d365f5 commit 0a23b80

File tree

7 files changed

+114
-32
lines changed

7 files changed

+114
-32
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
Changelog
22
=========
33

4+
1.0.0-RC6
5+
---------
6+
7+
* **2013-10-03**: added support for setting a global PHPCR ODM translation strategy
48

59
1.0.0-RC5
610
---------

DependencyInjection/CmfCoreExtension.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,9 @@ public function prepend(ContainerBuilder $container)
150150
)
151151
)
152152
);
153+
if (!empty($persistenceConfig['translation_strategy'])) {
154+
$prependConfig['persistence']['phpcr']['translation_strategy'] = $persistenceConfig['translation_strategy'];
155+
}
153156
break;
154157
case 'cmf_simple_cms':
155158
$prependConfig = array(
@@ -208,6 +211,11 @@ public function load(array $configs, ContainerBuilder $container)
208211
if (isset($config['multilang'])) {
209212
$container->setParameter($this->getAlias() . '.multilang.locales', $config['multilang']['locales']);
210213
$loader->load('translatable.xml');
214+
if (!empty($config['persistence']['phpcr']['translation_strategy'])) {
215+
$container->setParameter($this->getAlias() . '.persistence.phpcr.translation_strategy', $config['persistence']['phpcr']['translation_strategy']);
216+
} else {
217+
$container->removeDefinition('cmf_core.phpcr.translatable_metadata_listener');
218+
}
211219
} else {
212220
$loader->load('translatable-disabled.xml');
213221
}

DependencyInjection/Configuration.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ public function getConfigTreeBuilder()
4141
->values(array(true, false, 'auto'))
4242
->defaultValue('auto')
4343
->end()
44+
->scalarNode('translation_strategy')->defaultNull()->end()
4445
->end()
4546
->end()
4647
->end()
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony CMF package.
5+
*
6+
* (c) 2011-2013 Symfony CMF
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
13+
namespace Symfony\Cmf\Bundle\CoreBundle\Doctrine\Phpcr;
14+
15+
use Doctrine\Common\EventSubscriber;
16+
use Doctrine\Common\Persistence\Event\LifecycleEventArgs;
17+
use Doctrine\Common\Persistence\Event\LoadClassMetadataEventArgs;
18+
use Doctrine\ODM\PHPCR\Mapping\ClassMetadata;
19+
use Symfony\Cmf\Bundle\CoreBundle\Translatable\TranslatableInterface;
20+
21+
/**
22+
* Metadata listener for when translations are disabled in PHPCR-ODM to remove
23+
* mapping information that makes fields being translated.
24+
*
25+
* @author David Buchmann <[email protected]>
26+
*/
27+
class NonTranslatableMetadataListener implements EventSubscriber
28+
{
29+
/**
30+
* @return array
31+
*/
32+
public function getSubscribedEvents()
33+
{
34+
return array(
35+
'loadClassMetadata',
36+
'postLoad',
37+
);
38+
}
39+
40+
/**
41+
* Handle the load class metadata event: remove translated attribute from
42+
* fields and remove the locale mapping if present.
43+
*
44+
* @param LoadClassMetadataEventArgs $eventArgs
45+
*/
46+
public function loadClassMetadata(LoadClassMetadataEventArgs $eventArgs)
47+
{
48+
/** @var $meta ClassMetadata */
49+
$meta = $eventArgs->getClassMetadata();
50+
51+
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);
74+
}
75+
}
76+
}

Doctrine/Phpcr/TranslatableMetadataListener.php

Lines changed: 17 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -16,30 +16,39 @@
1616
use Doctrine\Common\Persistence\Event\LifecycleEventArgs;
1717
use Doctrine\Common\Persistence\Event\LoadClassMetadataEventArgs;
1818
use Doctrine\ODM\PHPCR\Mapping\ClassMetadata;
19-
use Symfony\Cmf\Bundle\CoreBundle\Translatable\TranslatableInterface;
2019

2120
/**
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
2422
*
25-
* @author David Buchmann <[email protected]>
23+
* @author Lukas Kahwe Smith <[email protected]>
2624
*/
2725
class TranslatableMetadataListener implements EventSubscriber
2826
{
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+
2940
/**
3041
* @return array
3142
*/
3243
public function getSubscribedEvents()
3344
{
3445
return array(
3546
'loadClassMetadata',
36-
'postLoad',
3747
);
3848
}
3949

4050
/**
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
4352
*
4453
* @param LoadClassMetadataEventArgs $eventArgs
4554
*/
@@ -49,28 +58,7 @@ public function loadClassMetadata(LoadClassMetadataEventArgs $eventArgs)
4958
$meta = $eventArgs->getClassMetadata();
5059

5160
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);
7462
}
7563
}
7664
}

Resources/config/translatable-disabled.xml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,13 @@
55
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
66

77
<parameters>
8-
<parameter key="cmf_core.phpcr.translatable_metadata_listener.class">Symfony\Cmf\Bundle\CoreBundle\Doctrine\Phpcr\TranslatableMetadataListener</parameter>
8+
<parameter key="cmf_core.persistence.phpcr.non_translatable_metadata_listener.class">Symfony\Cmf\Bundle\CoreBundle\Doctrine\Phpcr\NonTranslatableMetadataListener</parameter>
99
<parameter key="cmf_core.phpcr.multilang.locales" type="collection" />
1010
</parameters>
1111

1212
<services>
1313

14-
<service id="cmf_core.phpcr.translatable_metadata_listener" class="%cmf_core.phpcr.translatable_metadata_listener.class%">
15-
<argument>false</argument>
14+
<service id="cmf_core.persistence.phpcr.non_translatable_metadata_listener" class="%cmf_core.persistence.phpcr.non_translatable_metadata_listener.class%">
1615
<tag name="doctrine_phpcr.event_subscriber"/>
1716
</service>
1817

Resources/config/translatable.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,16 @@
66

77
<parameters>
88
<parameter key="cmf_core.admin_extension.translatable.class">Symfony\Cmf\Bundle\CoreBundle\Admin\Extension\TranslatableExtension</parameter>
9+
<parameter key="cmf_core.persistence.phpcr.translatable_metadata_listener.class">Symfony\Cmf\Bundle\CoreBundle\Doctrine\Phpcr\TranslatableMetadataListener</parameter>
910
</parameters>
1011

1112
<services>
1213

14+
<service id="cmf_core.persistence.phpcr.translatable_metadata_listener" class="%cmf_core.persistence.phpcr.translatable_metadata_listener.class%">
15+
<argument>%cmf_core.persistence.phpcr.translation_strategy%</argument>
16+
<tag name="doctrine_phpcr.event_subscriber"/>
17+
</service>
18+
1319
<service id="cmf_core.admin_extension.translatable" class="%cmf_core.admin_extension.translatable.class%">
1420
<argument>%cmf_core.multilang.locales%</argument>
1521
<tag name="sonata.admin.extension"/>

0 commit comments

Comments
 (0)