Using TranslatableStringHelper inside CustomFieldChoice, CustomFieldText & CustomFieldTitle

This commit is contained in:
Marc Ducobu 2014-11-17 12:10:14 +01:00
parent 1191cd7f90
commit 43d863ef88
4 changed files with 56 additions and 14 deletions

View File

@ -1,7 +1,7 @@
<?php <?php
/* /*
* Chill is a software for social workers * Chill is a software for social workers
* Copyright (C) 2014, Champs Libres Cooperative SCRLFS, <http://www.champs-libres.coop> * Copyright (C) 2014, Champs Libres Cooperative SCRLFS, <http://www.champs-libres.coop>
* *
* This program is free software: you can redistribute it and/or modify * This program is free software: you can redistribute it and/or modify
@ -29,11 +29,13 @@ use Chill\CustomFieldsBundle\Form\Type\ChoicesListType;
use Chill\CustomFieldsBundle\Form\DataTransformer\CustomFieldDataTransformer; use Chill\CustomFieldsBundle\Form\DataTransformer\CustomFieldDataTransformer;
use Chill\CustomFieldsBundle\Form\Type\ChoiceWithOtherType; use Chill\CustomFieldsBundle\Form\Type\ChoiceWithOtherType;
use Symfony\Bridge\Twig\TwigEngine; use Symfony\Bridge\Twig\TwigEngine;
use Chill\MainBundle\Templating\TranslatableStringHelper;
/** /**
* *
* *
* @author Julien Fastré <julien.fastre@champs-libres.coop> * @author Julien Fastré <julien.fastre@champs-libres.coop>
* @author Marc Ducobu <marc@champs-libes.coop>
*/ */
class CustomFieldChoice implements CustomFieldInterface class CustomFieldChoice implements CustomFieldInterface
{ {
@ -55,16 +57,21 @@ class CustomFieldChoice implements CustomFieldInterface
* @var TwigEngine * @var TwigEngine
*/ */
private $templating; private $templating;
/**
* @var TranslatableStringHelper Helper that find the string in current locale from an array of translation
*/
private $translatableStringHelper;
public function __construct(RequestStack $requestStack, $defaultLocale, TwigEngine $templating) public function __construct(RequestStack $requestStack, $defaultLocale, TwigEngine $templating,
TranslatableStringHelper $translatableStringHelper)
{ {
$this->requestStack = $requestStack; $this->requestStack = $requestStack;
$this->defaultLocale = $defaultLocale; $this->defaultLocale = $defaultLocale;
$this->templating = $templating; $this->templating = $templating;
$this->translatableStringHelper = $translatableStringHelper;
} }
public function buildForm(FormBuilderInterface $builder, CustomField $customField) public function buildForm(FormBuilderInterface $builder, CustomField $customField)
{ {
//prepare choices //prepare choices
@ -72,16 +79,16 @@ class CustomFieldChoice implements CustomFieldInterface
$choices = array(); $choices = array();
foreach($customField->getOptions()[self::CHOICES] as $persistedChoices) { foreach($customField->getOptions()[self::CHOICES] as $persistedChoices) {
if ($persistedChoices['active']){ if ($persistedChoices['active']){
$choices[$persistedChoices['slug']] = $persistedChoices['name'][$locale]; $choices[$persistedChoices['slug']] = $this->translatableStringHelper->localize($persistedChoices['name']);
} }
} }
//prepare $options //prepare $options
$options = array( $options = array(
'multiple' => $customField->getOptions()[self::MULTIPLE], 'multiple' => $customField->getOptions()[self::MULTIPLE],
'choices' => $choices, 'choices' => $choices,
'required' => false, 'required' => false,
'label' => $customField->getName()[$locale] 'label' => $this->translatableStringHelper->localize($customField->getName())
); );
//if allow_other = true //if allow_other = true

View File

@ -1,6 +1,23 @@
<?php <?php
/*
* Chill is a software for social workers
*
* Copyright (C) 2014, Champs Libres Cooperative SCRLFS, <http://www.champs-libres.coop>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
namespace Chill\CustomFieldsBundle\CustomFields; namespace Chill\CustomFieldsBundle\CustomFields;
@ -9,11 +26,11 @@ use Chill\CustomFieldsBundle\Entity\CustomField;
use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\HttpFoundation\RequestStack; use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Bundle\TwigBundle\TwigEngine; use Symfony\Bundle\TwigBundle\TwigEngine;
use Chill\MainBundle\Templating\TranslatableStringHelper;
/** /**
*
*
* @author Julien Fastré <julien.fastre@champs-libres.coop> * @author Julien Fastré <julien.fastre@champs-libres.coop>
* @author Marc Ducobu <marc@champs-libres.coop>
*/ */
class CustomFieldText implements CustomFieldInterface class CustomFieldText implements CustomFieldInterface
{ {
@ -26,11 +43,18 @@ class CustomFieldText implements CustomFieldInterface
*/ */
private $templating; private $templating;
/**
* @var TranslatableStringHelper Helper that find the string in current locale from an array of translation
*/
private $translatableStringHelper;
public function __construct(RequestStack $requestStack, TwigEngine $templating) public function __construct(RequestStack $requestStack, TwigEngine $templating,
TranslatableStringHelper $translatableStringHelper)
{ {
$this->requestStack = $requestStack; $this->requestStack = $requestStack;
$this->templating = $templating; $this->templating = $templating;
$this->translatableStringHelper = $translatableStringHelper;
} }
const MAX_LENGTH = 'maxLength'; const MAX_LENGTH = 'maxLength';
@ -50,7 +74,7 @@ class CustomFieldText implements CustomFieldInterface
: 'textarea'; : 'textarea';
$builder->add($customField->getSlug(), $type, array( $builder->add($customField->getSlug(), $type, array(
'label' => $customField->getName()[$this->requestStack->getCurrentRequest()->getLocale()], 'label' => $this->translatableStringHelper->localize($customField->getName()),
'required' => false 'required' => false
)); ));
} }

View File

@ -26,6 +26,7 @@ use Chill\CustomFieldsBundle\Entity\CustomField;
use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\HttpFoundation\RequestStack; use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Bundle\TwigBundle\TwigEngine; use Symfony\Bundle\TwigBundle\TwigEngine;
use Chill\MainBundle\Templating\TranslatableStringHelper;
class CustomFieldTitle implements CustomFieldInterface class CustomFieldTitle implements CustomFieldInterface
{ {
@ -41,10 +42,17 @@ class CustomFieldTitle implements CustomFieldInterface
*/ */
private $templating; private $templating;
public function __construct(RequestStack $requestStack, TwigEngine $templating) /**
* @var TranslatableStringHelper Helper that find the string in current locale from an array of translation
*/
private $translatableStringHelper;
public function __construct(RequestStack $requestStack, TwigEngine $templating,
TranslatableStringHelper $translatableStringHelper)
{ {
$this->requestStack = $requestStack; $this->requestStack = $requestStack;
$this->templating = $templating; $this->templating = $templating;
$this->translatableStringHelper = $translatableStringHelper;
} }
public function buildForm(FormBuilderInterface $builder, CustomField $customField) public function buildForm(FormBuilderInterface $builder, CustomField $customField)
@ -53,7 +61,7 @@ class CustomFieldTitle implements CustomFieldInterface
'label' => false, 'label' => false,
'attr' => array( 'attr' => array(
'class' => 'no-label', 'class' => 'no-label',
'title' => $customField->getName()[$this->requestStack->getCurrentRequest()->getLocale()], 'title' => $this->translatableStringHelper->localize($customField->getName()),
self::TYPE => $customField->getOptions()[self::TYPE ] self::TYPE => $customField->getOptions()[self::TYPE ]
) )
)); ));

View File

@ -40,6 +40,7 @@ services:
arguments: arguments:
- "@request_stack" - "@request_stack"
- "@templating" - "@templating"
- "@chill.main.helper.translatable_string"
tags: tags:
- { name: 'chill.custom_field', type: 'text' } - { name: 'chill.custom_field', type: 'text' }
@ -49,6 +50,7 @@ services:
- "@request_stack" - "@request_stack"
- %locale% - %locale%
- "@templating" - "@templating"
- "@chill.main.helper.translatable_string"
tags: tags:
- { name: 'chill.custom_field', type: 'choice' } - { name: 'chill.custom_field', type: 'choice' }
@ -69,6 +71,7 @@ services:
arguments: arguments:
- "@request_stack" - "@request_stack"
- "@templating" - "@templating"
- "@chill.main.helper.translatable_string"
tags: tags:
- { name: 'chill.custom_field', type: 'title' } - { name: 'chill.custom_field', type: 'title' }