2017 © Pedro Peláez
 

symfony-bundle translatable-form-field

A Symfony2 bundle responsible for translatable form fields in symfony2 / sonata admin.

image

benedicthelfer/translatable-form-field

A Symfony2 bundle responsible for translatable form fields in symfony2 / sonata admin.

  • Wednesday, July 4, 2018
  • by benedict
  • Repository
  • 3 Watchers
  • 1 Stars
  • 59 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 1 Versions
  • 16 % Grown

The README.md

translatable-form-field

This bundle is responsible for translatable form fields in symfony2 / sonata admin., (*1)

Usage:, (*2)

  • add to 'AppKernel'
public function registerBundles()
{
    $bundles = array(
    // ...
    new Bnh\TranslatableFieldBundle\BnhTranslatableFieldBundle()
    );
}
  • config
bnh_translatable_field:
    default_locale: en_GB
    locales: ['de_DE', 'en_GB', 'es_ES', 'fr_FR', 'hu_HU', 'ru_RU', 'sv_SE']
    templating: 'BnhTranslatableFieldBundle:FormType:bnhtranslations.html.twig'
  • entity (ext_translations)
use Gedmo\Mapping\Annotation as Gedmo;
use Gedmo\Translatable\Translatable;
// ...

class YourEntity implements Translatable
{
    /**
     * @Gedmo\Translatable
     * @ORM\Column(...
     */
    private $fieldname;

    public function setfieldname($fieldname)
    {
        $this->fieldname = $fieldname;
        return $this;
    }

    /**
     * @Gedmo\Locale
     */
    private $locale;

    public function setTranslatableLocale($locale)
    {
        $this->locale = $locale;
    }
}
  • entity (for personal translations)
/**
 * @ORM\Entity
 * @Gedmo\TranslationEntity(class="YourEntityTranslation")
 */
class YourEntity
{
    /**
     * @ORM\OneToMany(targetEntity="YourEntityTranslation", mappedBy="object", cascade={"persist", "remove"})
     */
    private $translations;

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

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

    public function addTranslation(YourEntityTranslation $newTranslation)
    {
        if($newTranslation->getContent())
        {
            $found = false;
            foreach($this->translations as $translation)
            {
                if(($translation->getLocale() === $newTranslation->getLocale()) && ($translation->getField() === $newTranslation->getField()))
                {
                    $found = true;
                    $translation->setContent($newTranslation->getContent());
                    break;
                }
            }

            if(!$found)
            {
                $newTranslation->setObject($this);
                $this->translations[] = $newTranslation;
            }
        }
    }

    public function hasTranslation($locale, $fieldName)
    {
        foreach ($this->translations as $translation)
        {
            if(($translation->getLocale() === $locale) && ($translation->getField() === $fieldName))
            {
                return true;
            }
        }

        return false;
    }
}
  • sonata admin page
    protected function configureFormFields(FormMapper $formMapper)
    {
        $formMapper->add('fieldname', 'bnhtranslations');
    }

The Versions

04/07 2018

dev-master

9999999-dev https://github.com/benedicthelfer/translatable-form-field

A Symfony2 bundle responsible for translatable form fields in symfony2 / sonata admin.

  Sources   Download

MIT

The Requires

  • php >=5.3.3

 

translation sonata translatable gedmo translate