-
-
Save istvankis/0010cf0e318258146d4f203007aaf1e2 to your computer and use it in GitHub Desktop.
AppBundle\Entity\Lang: | |
lang_0: | |
lang: | |
en: French | |
fr: Français | |
lang_1: | |
lang: | |
en: English | |
fr: Anglais |
alice.processor.translatable: | |
class: AppBundle\DataFixtures\Processor\TranslatableProcessor | |
arguments: | |
- '@doctrine.orm.entity_manager' | |
- '@gedmo.listener.translatable' | |
tags: [ { name: hautelook_alice.alice.processor } ] |
<?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); | |
} | |
} | |
} | |
} | |
} | |
} |
have done all above. My fixtures.yml is:
AppBundle\Entity\TaskDefinition:
section_1_task_definition_1:
name:
en: "Task number one"
nl: "Opdracht nummer een"
When running doctrine:fixtures:load I get :
[Symfony\Component\Debug\Exception\ContextErrorException]
Notice: Array to string conversion
What can i do now?
Hi,
I think that I solved your problems.
First, I changed the tag in the service definition :
tags: [ { name: fidry_alice_data_fixtures.processor } ]
After, in translatableProcessor, I changed the fallback manually ( It's not very clean... but it's just for fixtures... )
$wrapped->setPropertyValue($field, $propertyValue['en']);
It works successfully for me... Hope it can help you ;)
Yoann
its not compatible with Alice v3
using fidry_alice_data_fixtures.processor do not works, because Array to string conversion are generated in Nelmio/Alice core parser BEFORE fidry execute processors.
Did anybody have working snippet for Alice v3?
Would love a snippet for Alice V3 !
@zhil
You have to remove the type hinting on your entities for the translatable fields. By this way your fields are hydrated with array and the processor will reset to scalar.
@Yoann-TYT
Or you can also use $propertyValue[$this->translatableListener->getDefaultLocale()]
Thank you @romain-pierre, removing the type hinting did the trick !
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 the ext_translations
table.
I tried to use the TranslatableProcessor proposed here, but it doesn't work. Here's my entity:
<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
use ApiPlatform\Core\Annotation\ApiResource;
use Symfony\Component\Serializer\Annotation\Groups;
use Gedmo\Mapping\Annotation as Gedmo;
use Gedmo\Translatable\Translatable;
/**
* @ApiResource(
* denormalizationContext={"groups"={"cloth"}},
* collectionOperations={
* "get"={"path"="/clothes"},
* "post"={"path"="/clothes"}
* },
* itemOperations={
* "get"={"path"="/clothes/{id}", "requirements"={"id"="\d+"}},
* "put"={"path"="/clothes/{id}", "requirements"={"id"="\d+"}},
* "delete"={"path"="/clothes/{id}", "requirements"={"id"="\d+"}}
* }
* )
* @ORM\Entity
* @ORM\Table(name="clothes")
*/
class Cloth implements Translatable
{
/**
* @ORM\Id
* @ORM\Column(type="integer", options={"check":"CONSTRAINT positive_cloth_id CHECK (id > 0)"})
* @ORM\GeneratedValue
*/
private $id;
/**
* @ORM\ManyToOne(targetEntity="Brand", inversedBy="clothes", cascade={"persist"})
* @ORM\JoinColumn(nullable=false)
* @Groups({"cloth"})
*/
public $brand;
/**
* @ORM\Column(type="string", length=40)
* @Groups({"cloth", "read_brand"})
* @Gedmo\Translatable
*/
public $name;
public function getId(): ?int
{
return $this->id;
}
}
And my fixtures file (if I remove the fr
keys and put the values for name
, the insertions work but my table ext_translations
remains empty):
App\Entity\Cloth:
coat:
name:
fr: Manteau croisé bleu
brand: '@suitsupply'
trousers:
name:
fr: Pantalon taille haute à pinces
brand: '@scavini'
knit:
name:
fr: Pull torsadé gris
brand: '@hircus'
I see Notice: Array to string conversion
when I do bin/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.
Hi,
Have a look on this https://gist.github.com/pitpit/ce6df260fea08e61a6e5c5e7cb668d59 for a way that does not require any code
Does Someone have the same problem ?