phpdoctrine-ormzend-framework2zend-formdoctrine-extensions

ZF2 and Doctrine 2: Translation entity form


I need to create ZF2 form for a Doctrine translatable Entity (I use https://github.com/Atlantic18/DoctrineExtensions Translatable Extension), which should provide fields for all translatable properties(columns) of the entity in each available language. So far I have the following:

1) Article Entity

namespace TestModule\Entity;

use Doctrine\Common\Collections\ArrayCollection;

/**
 * @Doctrine\ORM\Mapping\Entity(repositoryClass="TestModule\Entity\ArticleRepository")
 * @Doctrine\ORM\Mapping\Table(name="test_module_articles")
 * @Gedmo\Mapping\Annotation\TranslationEntity(class="TestModule\Entity\ArticleTranslation")
 */
class Article
{
    /**
     * @var int Auto-Incremented Primary Key
     *
     * @Doctrine\ORM\Mapping\Id
     * @Doctrine\ORM\Mapping\Column(type="integer")
     * @Doctrine\ORM\Mapping\GeneratedValue
     */
    protected $id;

    /**
     * @Doctrine\ORM\Mapping\Column(type="string")
     * @Gedmo\Mapping\Annotation\Translatable
     */
    protected $name;

    /**
     * @Doctrine\ORM\Mapping\Column(type="text", length=65535)
     * @Gedmo\Mapping\Annotation\Translatable
     */
    protected $description;

    /**
     * @Gedmo\Mapping\Annotation\Locale
     * Used locale to override Translation listener`s locale
     * this is not a mapped field of entity metadata, just a simple property
     * and it is not necessary because globally locale can be set in listener
     */
    private $locale;

    /**
     * @Doctrine\ORM\Mapping\OneToMany(
     *   targetEntity="TestModule\Entity\ArticleTranslation",
     *   mappedBy="object",
     *   cascade={"persist", "remove"}
     * )
     */
    private $translations;


    public function __construct() 
    {
        $this->translations = new ArrayCollection();
    }

    public function getTranslations()
    {
        return $this->translations;
    }

    public function addTranslation(\TestModule\Entity\ArticleTranslation $t)
    {
        if (!$this->translations->contains($t)) {
            $this->translations[] = $t;
            $t->setObject($this);
        }
    }


    public function addTranslations($translations)
    {
        foreach ($translations as $translation) {
            $this->addTranslation($translation);
        }
    }

    public function removeTranslations($translations)
    {
        foreach ($translations as $translation) {
            $this->translations->removeElement($translation);
            $translation->setObject(null);
        }
    }


    public function setTranslatableLocale($locale)
    {
        $this->locale = $locale;
    }

}

2) ArticleTranslation Entity

namespace TestModule\Entity;

use Gedmo\Translatable\Entity\MappedSuperclass\AbstractPersonalTranslation;
/**
 * @Doctrine\ORM\Mapping\Entity
 * @Doctrine\ORM\Mapping\Table(name="test_module_articles_translations",
 *     uniqueConstraints={@Doctrine\ORM\Mapping\UniqueConstraint(name="lookup_unique_idx", columns={
 *         "locale", "object_id", "field"
 *     })}
 * )
 */
class ArticleTranslation extends AbstractPersonalTranslation
{
    /**
     * Convinient constructor
     *
     * @param string $locale
     * @param string $field
     * @param string $value
     */
    public function __construct($locale, $field, $value)
    {
        $this->setLocale($locale);
        $this->setField($field);
        $this->setContent($value);
    }
    /**
     * @Doctrine\ORM\Mapping\ManyToOne(targetEntity="TestModule\Entity\Article", inversedBy="translations")
     * @Doctrine\ORM\Mapping\JoinColumn(name="object_id", referencedColumnName="id", onDelete="CASCADE")
     */
    protected $object;
} 

3) The Form

namespace TestModule\Form;

use Zend\Form\Form;
use Doctrine\ORM\EntityManager;
use DoctrineModule\Stdlib\Hydrator\DoctrineObject as DoctrineHydrator;
use TestModule\Form\ArticleTranslationsFieldset;
use TestModule\Entity\ArticleTranslation;

class ArticleForm extends Form
{
    protected $entityManager;

    public function __construct(EntityManager $entityManager,$name = null)
    {
        parent::__construct($name);

        $this->entityManager = $entityManager;

        $hydrator = new DoctrineHydrator($this->entityManager, 'TestModule\Entity\Article');


        $this->setAttribute('method', 'post')
            ->setHydrator($hydrator)
            //->setInputFilter($inputFilter)
        ;

        $this->add(array(
            'name'  => 'id',
            'type'  => 'Hidden',
        ));

        $articleFieldset = new ArticleTranslationsFieldset($entityManager);
        $fieldsetHydrator = new DoctrineHydrator($entityManager, 'TestModule\Entity\ArticleTranslation');
        $articleFieldset->setHydrator($fieldsetHydrator)->setObject(new ArticleTranslation('en','name',''));

        $this->add(array(
            'type' => 'Zend\Form\Element\Collection',
            'name' => 'translations',
            'allow_empty' => true,
             'options' => array(
                 'label' => '',
                 'count' => 0,
                 'allow_add' => true,
                 'allow_remove' => true,
                 'target_element' => $articleFieldset,

             ),
         ));


        $this->add(array(
            'name' => 'submit',
            'attributes' => array(
                'type' => 'submit',
                'value' => 'Submit'
            ),
        ));


    }
}

4) And the Translations Fieldset:

namespace TestModule\Form;

use Zend\Form\Fieldset;

class ArticleTranslationsFieldset extends Fieldset
{
    public function __construct()
    {

        parent::__construct('translations');

        $this->add(array(
            'name'  => 'locale',
            'type'  => 'Hidden',
        ));

        $this->add(array(
            'name'  => 'field',
            'type'  => 'Hidden',
        ));

        $this->add(array(
            'name' => 'content',
            'type' => 'Zend\Form\Element\Text',
            'options' => array(
                'label' => _(''),
            ),
            'attributes' => array(
                'type' => 'text',
            ),
        ));

    }

}

With this set-up I can save both the name and the description properties for each language, but I cannot manage the content field type - it is Text element for either the name and the description and cannot set the proper field label. I also cannot group the elements by language so that the form presented to the user is well organized.

Do you have any other suggestions how to solve this problem?
What I want to achieve is something like this:

enter image description here


Solution

  • I couldn't find a solution with the Translatable Doctrine Extension that I used in the question. So I search for another one and finally I end up using the Prezent Extension(https://github.com/Prezent/doctrine-translatable).

    With this extension the translation entity contains the translatable fields, which makes it easy to map the translation entity with the translations fieldset. Each translation entity has a locale property which I map to a hidden field in the fieldset and use it to present the form in the desired way.