/** * Gets entity available fields * * @param $entity * * @return array * @throws NoTranslationsDefinedException */ public function getEntityTranslatableFields($entity) { $translatable_config = $this->gedmoTranslatableListener->getConfiguration($this->em, get_class($entity)); $fields = @$translatable_config['fields']; if (!is_array($fields) || count($fields) == 0) { throw new NoTranslationsDefinedException('No translations defined'); } return $translatable_config['fields']; }
public function testTranslatableMetadata() { $meta = $this->em->getClassMetadata('Mapping\\Fixture\\Xml\\Translatable'); $config = $this->translatable->getConfiguration($this->em, $meta->name); $this->assertArrayHasKey('translationClass', $config); $this->assertEquals('Gedmo\\Translatable\\Entity\\Translation', $config['translationClass']); $this->assertArrayHasKey('locale', $config); $this->assertEquals('locale', $config['locale']); $this->assertArrayHasKey('fields', $config); $this->assertCount(2, $config['fields']); $this->assertTrue(in_array('title', $config['fields'])); $this->assertTrue(in_array('content', $config['fields'])); }
public function testTranslatableMetadataWithEmbedded() { $meta = $this->em->getClassMetadata('Mapping\\Fixture\\Xml\\TranslatableWithEmbedded'); $config = $this->translatable->getConfiguration($this->em, $meta->name); $this->assertContains('embedded.subtitle', $config['fields']); }
/** * Makes additional translation of $entity $field into $locale using $value * * @param object $entity * @param string $field * @param string $locale * @param mixed $value * * @return TranslationRepository * @access public * @author Etienne de Longeaux <*****@*****.**> */ public function translate($entity, $field, $locale, $value) { $meta = $this->_em->getClassMetadata(get_class($entity)); $listener = new \Gedmo\Translatable\TranslatableListener(); //$this->getTranslatableListener(); $config = $listener->getConfiguration($this->_em, $meta->name); if (!isset($config['fields']) || !in_array($field, $config['fields'])) { throw new \Gedmo\Exception\InvalidArgumentException("Entity: {$meta->name} does not translate field - {$field}"); } if (in_array($locale, array($listener->getDefaultLocale(), $listener->getTranslatableLocale($entity, $meta)))) { $meta->getReflectionProperty($field)->setValue($entity, $value); $this->_em->persist($entity); } else { $ea = new TranslatableAdapterORM(); $foreignKey = $meta->getReflectionProperty($meta->getSingleIdentifierFieldName())->getValue($entity); $objectClass = $meta->name; $class = $listener->getTranslationClass($ea, $meta->name); $transMeta = $this->_em->getClassMetadata($class); $trans = $this->findOneBy(compact('locale', 'field', 'object')); if (!$trans) { $trans = new $class(); $transMeta->getReflectionProperty('object')->setValue($trans, $entity->getId()); $transMeta->getReflectionProperty('field')->setValue($trans, $field); $transMeta->getReflectionProperty('locale')->setValue($trans, $locale); } $type = Type::getType($meta->getTypeOfField($field)); $transformed = $type->convertToDatabaseValue($value, $this->_em->getConnection()->getDatabasePlatform()); $transMeta->getReflectionProperty('content')->setValue($trans, $transformed); if ($this->_em->getUnitOfWork()->isInIdentityMap($entity)) { $this->_em->persist($trans); } else { $oid = spl_object_hash($entity); $listener->addPendingTranslationInsert($oid, $trans); } } return $this; // $meta = $this->_em->getClassMetadata(get_class($entity)); // $listener = $this->getTranslatableListener(); // $config = $listener->getConfiguration($this->_em, $meta->name); // if (!isset($config['fields']) || !in_array($field, $config['fields'])) { // throw new \Gedmo\Exception\InvalidArgumentException("Entity: {$meta->name} does not translate field - {$field}"); // } // $ea = new TranslatableAdapterORM(); // $class = $listener->getTranslationClass($ea, $meta->name); // $trans = $this->findOneBy(compact('locale', 'field', 'object_id')); // if (!$trans) { // $entity->setTranslatableLocale('fr'); // $entity->addTranslation(new $class($locale, $field, $value)); // } // $this->_em->persist($entity); // $this->_em->flush(); }