Skip to content

Commit

Permalink
Merge pull request #615 from doctrine/pre_bind_translation
Browse files Browse the repository at this point in the history
Added preBind translation
  • Loading branch information
dbu committed Mar 24, 2015
2 parents 69cfbdc + b8e3519 commit d3c7808
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/Doctrine/ODM/PHPCR/Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ final class Event

const postLoadTranslation = 'postLoadTranslation';
const preCreateTranslation = 'preCreateTranslation';
const preUpdateTranslation = 'preUpdateTranslation';
const preRemoveTranslation = 'preRemoveTranslation';
const postRemoveTranslation = 'postRemoveTranslation';

Expand All @@ -55,6 +56,7 @@ final class Event
self::postLoad => self::postLoad,
self::postLoadTranslation => self::postLoadTranslation,
self::preCreateTranslation => self::preCreateTranslation,
self::preUpdateTranslation => self::preUpdateTranslation,
self::preRemoveTranslation => self::preRemoveTranslation,
self::postRemoveTranslation => self::postRemoveTranslation,
);
Expand Down
8 changes: 8 additions & 0 deletions lib/Doctrine/ODM/PHPCR/UnitOfWork.php
Original file line number Diff line number Diff line change
Expand Up @@ -753,6 +753,14 @@ private function doBindTranslation($document, $locale, ClassMetadata $class)
new LifecycleEventArgs($document, $this->dm),
$invoke
);
} elseif ($invoke = $this->eventListenersInvoker->getSubscribedSystems($class, Event::preUpdateTranslation)) {
$this->eventListenersInvoker->invoke(
$class,
Event::preUpdateTranslation,
$document,
new LifecycleEventArgs($document, $this->dm),
$invoke
);
}

$this->setLocale($document, $class, $locale);
Expand Down
12 changes: 12 additions & 0 deletions tests/Doctrine/Tests/ODM/PHPCR/Functional/EventComputingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ public function testComputingBetweenEventsWithTranslation()
->addEventListener(
array(
Event::preCreateTranslation,
Event::preUpdateTranslation,
Event::postLoadTranslation,
Event::preRemoveTranslation,
Event::postRemoveTranslation,
Expand All @@ -142,8 +143,13 @@ public function testComputingBetweenEventsWithTranslation()
// name had been changed pre binding translation
$this->assertEquals('preCreateTranslation', $user->name);

$this->dm->bindTranslation($user, 'en');
// name has been changed when translation was updated
$this->assertEquals('preUpdateTranslation', $user->name);

$this->dm->name = 'neuer Name';
$this->dm->bindTranslation($user, 'de');

$this->dm->flush();
$this->dm->clear();

Expand Down Expand Up @@ -215,6 +221,12 @@ public function preCreateTranslation(LifecycleEventArgs $e)
$document->name = 'preCreateTranslation';
}

public function preUpdateTranslation(LifecycleEventArgs $e)
{
$document = $e->getObject();
$document->name = 'preUpdateTranslation';
}

public function postLoadTranslation(LifecycleEventArgs $e)
{
$document = $e->getObject();
Expand Down

0 comments on commit d3c7808

Please sign in to comment.