From bd96f364639d5b079f82d67386b16d55c27af601 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Tue, 11 Nov 2014 08:44:06 +0100 Subject: [PATCH] rendering of custom fields refs #277 [ci skip] --- CustomFields/CustomFieldChoice.php | 30 ++++++++++++++++++- Resources/config/services.yml | 1 + .../CustomFieldsRendering/choice.html.twig | 19 ++++++++++++ 3 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 Resources/views/CustomFieldsRendering/choice.html.twig diff --git a/CustomFields/CustomFieldChoice.php b/CustomFields/CustomFieldChoice.php index 6b45db9b0..4e78a1542 100644 --- a/CustomFields/CustomFieldChoice.php +++ b/CustomFields/CustomFieldChoice.php @@ -28,6 +28,7 @@ use Symfony\Component\HttpFoundation\RequestStack; use Chill\CustomFieldsBundle\Form\Type\ChoicesListType; use Chill\CustomFieldsBundle\Form\DataTransformer\CustomFieldDataTransformer; use Chill\CustomFieldsBundle\Form\Type\ChoiceWithOtherType; +use Symfony\Bridge\Twig\TwigEngine; /** * @@ -49,10 +50,17 @@ class CustomFieldChoice implements CustomFieldInterface private $defaultLocale; - public function __construct(RequestStack $requestStack, $defaultLocale) + /** + * + * @var TwigEngine + */ + private $templating; + + public function __construct(RequestStack $requestStack, $defaultLocale, TwigEngine $templating) { $this->requestStack = $requestStack; $this->defaultLocale = $defaultLocale; + $this->templating = $templating; } @@ -146,7 +154,27 @@ class CustomFieldChoice implements CustomFieldInterface public function render($value, CustomField $customField) { + //extract the data. They are under a _choice key if they are stored with allow_other + $data = (isset($value['_choices'])) ? $value['_choices'] : $value; + $selected = (is_array($data)) ? $data : array($data); + + $choices = $customField->getOptions()[self::CHOICES]; + + if (in_array('_other', $selected)){ + $choices[] = array('name' => $value['_other'], 'slug' => '_other'); + } + + return $this->templating + ->render('ChillCustomFieldsBundle:CustomFieldsRendering:choice.html.twig', + array( + 'choices' => $choices, + 'selected' => $selected, + 'multiple' => $customField->getOptions()[self::MULTIPLE], + 'expanded' => $customField->getOptions()[self::EXPANDED], + 'locale' => $this->defaultLocale + ) + ); } public function serialize($value, CustomField $customField) diff --git a/Resources/config/services.yml b/Resources/config/services.yml index a330b4819..1a080ac00 100644 --- a/Resources/config/services.yml +++ b/Resources/config/services.yml @@ -48,6 +48,7 @@ services: arguments: - "@request_stack" - %locale% + - "@templating" tags: - { name: 'chill.custom_field', type: 'choice' } diff --git a/Resources/views/CustomFieldsRendering/choice.html.twig b/Resources/views/CustomFieldsRendering/choice.html.twig new file mode 100644 index 000000000..271905264 --- /dev/null +++ b/Resources/views/CustomFieldsRendering/choice.html.twig @@ -0,0 +1,19 @@ + \ No newline at end of file