Developed with love by KnpLabs Hire us for your project!
20

DoctrineTranslatableFormBundle

by Simettric

Translatable forms using StofDoctrineExtensionsBundle and Doctrine Extensions

DoctrineTranslatableFormBundle

The goal of this Symfony Bundle is simplify the creation of translatable forms using Gedmo Doctrine Extensions and StofDoctrineExtensionsBundle.

Installation

Step 1: Download the Bundle

Open a command console, enter your project directory and execute the
following command to download the latest stable version of this bundle:

$ composer require simettric/doctrine-translatable-form-bundle 

This command requires you to have Composer installed globally, as explained
in the installation chapter
of the Composer documentation.

Step 2: Enable the Bundle

Then, enable the bundle by adding it to the list of registered bundles
in the app/AppKernel.php file of your project:

<?php
// app/AppKernel.php

// ...
class AppKernel extends Kernel
{
    public function registerBundles()
    {
        $bundles = array(
            // ...
            new Stof\DoctrineExtensionsBundle\StofDoctrineExtensionsBundle(),
            new Simettric\DoctrineTranslatableFormBundle\SimettricDoctrineTranslatableFormBundle(),
        );

        // ...
    }

    // ...
}

Configuration

You must to activate the persist_default_translation key in your stof_doctrine_extensions configuration options

#app/config/config.yml
stof_doctrine_extensions:
    default_locale: %locale%
    translation_fallback: true
    persist_default_translation: true
    orm:
        default:
            translatable: true

Configure the view

This bundle implements the Bootstrap Tabs component in order to show the different locale inputs for each field.

If you want to use it, just add the template in the form_themes section in your Twig configuration.
Obviously, the bootstrap assets must be loaded in your layout.

# app/config/config.yml
twig:
    ...
    form_themes:
        - 'SimettricDoctrineTranslatableFormBundle:Form:fields.html.twig'

Creating your forms

This is a simple example showing how you can code your translatable forms

<?php

namespace AppBundle\Form;


use Simettric\DoctrineTranslatableFormBundle\Form\AbstractTranslatableType;
use Simettric\DoctrineTranslatableFormBundle\Form\TranslatableTextType;

class CategoryType extends AbstractTranslatableType
{
    /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {

        // you can add the translatable fields
        $this->createTranslatableMapper($builder, $options)
             ->add("name", TranslatableTextType::class)
             ->add("description", TranslatableTextareaType::class)
        ;

        // and then you can add the rest of the fields using the standard way
        $builder->add('enabled')
        ;

    }

    /**
     * @param OptionsResolver $resolver
     */
    public function configureOptions(OptionsResolver $resolver)
    {

        $resolver->setDefaults(array(
            'data_class'   => 'AppBundle\Entity\Category'
        ));

        // this is required
        $this->configureTranslationOptions($resolver);

    }
}

Then you need to declare your form type as a service

#app/config/services.yml

parameters:
    locale: es
    locales: [es, eu, en, fr]

services:
    app.form.category_type:
        class: AppBundle\Form\CategoryType
        arguments: ["@sim_trans_form.mapper"]
        calls:
            - [ setRequiredLocale, [%locale%] ]
            - [ setLocales, [%locales%] ]
        tags:
            - { name: form.type }

And now you can work in your controller as if you worked with normal entities

$category = new Category();

$form = $this->createForm(CategoryType::class, $category);

if($request->getMethod() == "POST"){

    $form->handleRequest($request);

    if($form->isValid()){

        $em = $this->getDoctrine()->getManager();

        $em->persist($category);
        $em->flush();

    }
}

You can set your own Repository defining a new custom mapping service.
This is useful for instance when you have a Personal Translation mapping configuration using a specific translation repository

services:
    app.my_custom_mapper:
        class:     Simettric\DoctrineTranslatableFormBundle\Form\DataMapper
        arguments: ["@doctrine.orm.entity_manager", "AppBundle\Repository\PostTranslationRepository"]

    app.form.post_type:
        class: AppBundle\Form\PostType
        arguments: ["@app.my_custom_mapper"]
        calls:
            - [ setRequiredLocale, [%locale%] ]
            - [ setLocales, [%locales%] ]
        tags:
            - { name: form.type }

If you have configured the Bootstrap Tabs theme in your Twig configuration, you can show your fields with the Boo in the templates with the form_row tag

<div class="form-group">
    {{ form_row(form.name, {label: "name"|trans}) }}
</div>

<div class="form-group">
    {{ form_row(form.description, {label: "description"|trans}) }}
</div>
Copyright (c) 2016 Asier Marqués

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is furnished
to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
simettric_doctrine_translatable_form:  []
  • Merge branch 'master' of https://github.com/Simettric/DoctrineTranslatableFormBundle
    By asiermarques, 1 year ago
  • you have to pass custom attr to child widgets
    By asiermarques, 1 year ago
  • fix versions
    By web-flow, 1 year ago
  • fix versions
    By web-flow, 1 year ago
  • you can set your own repository in the mapping class #5
    By asiermarques, 1 year ago
  • Merge branch 'karatektus-master'
    By asiermarques, 1 year ago
  • resolving conflicts
    By asiermarques, 1 year ago
  • Merge pull request #7 from martijnhartlief/undefined-index
    By web-flow, 1 year ago
  • Merge branch 'master' into undefined-index
    By web-flow, 1 year ago
  • ignore required when is marked with required=false
    By asiermarques, 1 year ago
  • fix when a field has not value and is mapped
    By asiermarques, 1 year ago
  • Translation fallback
    By , 1 year ago
  • Merge pull request #2 from HcErwin/1.0
    By web-flow, 1 year ago
  • Fallback fix for custom Translation class resolving
    By web-flow, 1 year ago
  • BTOTS2-485 Category geeft een 500 error
    By Martijn Hartlief, 1 year ago
  • reset translations so it'll work in Collections
    By Paul Lützner, 1 year ago
  • foo
    By Paul Lützner, 1 year ago
  • namechange
    By Paul Lützner, 1 year ago
  • fooo
    By Paul Lützner, 1 year ago
  • reverse name
    By Paul Lützner, 1 year ago
  • set requirements to symfony 2.8
    By Paul Lützner, 1 year ago
  • Undefined indexes are not checked, in case not all fields are translated
    By Martijn Hartlief, 1 year ago
  • #4 DataMapper tries to map SubmitType
    By web-flow, 1 year ago
  • #4 DataMapper tries to map SubmitType
    By web-flow, 1 year ago
  • Update README.md
    By web-flow, 2 years ago
  • symfony version in composer with tilde operator
    By web-flow, 2 years ago
  • #1 Symfony version to 3.* in composer
    By web-flow, 2 years ago
  • fix in required locale
    By asiermarques, 2 years ago
  • bootstrap tabs component for templates
    By asiermarques, 2 years ago
  • support to not mapped fields in mapper
    By asiermarques, 2 years ago