Created
April 30, 2016 11:39
-
-
Save istvankis/0010cf0e318258146d4f203007aaf1e2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
AppBundle\Entity\Lang: | |
lang_0: | |
lang: | |
en: French | |
fr: Français | |
lang_1: | |
lang: | |
en: English | |
fr: Anglais |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
alice.processor.translatable: | |
class: AppBundle\DataFixtures\Processor\TranslatableProcessor | |
arguments: | |
- '@doctrine.orm.entity_manager' | |
- '@gedmo.listener.translatable' | |
tags: [ { name: hautelook_alice.alice.processor } ] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace AppBundle\DataFixtures\Processor; | |
use Doctrine\ORM\EntityManager; | |
use Gedmo\Tool\Wrapper\AbstractWrapper; | |
use Gedmo\Translatable\Translatable; | |
use Gedmo\Translatable\TranslatableListener; | |
use Nelmio\Alice\ProcessorInterface; | |
class TranslatableProcessor implements ProcessorInterface | |
{ | |
/** | |
* @var EntityManager | |
*/ | |
private $entityManager; | |
/** | |
* @var TranslatableListener | |
*/ | |
private $translatableListener; | |
/** | |
* @var Translatable | |
*/ | |
private $translateRepository; | |
/** | |
* TranslatableProcessor constructor. | |
* @param EntityManager $entityManager | |
* @param TranslatableListener $translatableListener | |
*/ | |
public function __construct(EntityManager $entityManager, TranslatableListener $translatableListener) | |
{ | |
$this->entityManager = $entityManager; | |
$this->translatableListener = $translatableListener; | |
$this->translateRepository = $entityManager->getRepository("Gedmo\\Translatable\\Entity\\Translation"); | |
} | |
public function postProcess($object) | |
{ | |
} | |
public function preProcess($object) | |
{ | |
$translationConfig = $this->translatableListener | |
->getConfiguration($this->entityManager, get_class($object)); | |
if ($translationConfig && isset($translationConfig['fields'])) { | |
foreach ($translationConfig['fields'] as $field) { | |
$wrapped = AbstractWrapper::wrap($object, $this->entityManager); | |
$propertyValue = $wrapped->getPropertyValue($field); | |
if (is_array($propertyValue)) { | |
$wrapped->setPropertyValue($field, $propertyValue[$translationConfig['fallback'][$field]]); | |
foreach ($propertyValue as $local => $content) { | |
$this->translateRepository->translate($object, $field, $local, $content); | |
} | |
} | |
} | |
} | |
} | |
} |
Hi,
Have a look on this https://gist.github.com/pitpit/ce6df260fea08e61a6e5c5e7cb668d59 for a way that does not require any code
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi,
I am using API Platform with Symfony 4 and try to make fixtures. I use DoctrineExtensionsBundle to use Translatable.
When I do
bin/console hautelook:fixtures:load
, my fixtures are created but the translations are not inserted in theext_translations
table.I tried to use the TranslatableProcessor proposed here, but it doesn't work. Here's my entity:
And my fixtures file (if I remove the
fr
keys and put the values forname
, the insertions work but my tableext_translations
remains empty):I see
Notice: Array to string conversion
when I dobin/console hautelook:fixtures:load
with the file. In the previous answer, you say to remove the type hinting, but I don't understand where I must remove it.Thanks for your answer.