From d4866760110b9637f838615f330c03b48a59a3e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Tue, 24 Feb 2015 22:29:20 +0100 Subject: [PATCH] add 'custom_fields_group_linked_custom_fields' type The type create a choice field with, as choices, the custom fields depending of a customFieldGroup. The type can only be appended to customFieldsGroupType. --- Form/Type/LinkedCustomFieldsType.php | 138 +++++++++++++++++++++++++++ Resources/config/services.yml | 7 ++ 2 files changed, 145 insertions(+) create mode 100644 Form/Type/LinkedCustomFieldsType.php diff --git a/Form/Type/LinkedCustomFieldsType.php b/Form/Type/LinkedCustomFieldsType.php new file mode 100644 index 000000000..129fd508c --- /dev/null +++ b/Form/Type/LinkedCustomFieldsType.php @@ -0,0 +1,138 @@ + + * + * 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 . + */ + +namespace Chill\CustomFieldsBundle\Form\Type; + +use Symfony\Component\Form\AbstractType; +use Symfony\Component\Form\FormBuilderInterface; +use Symfony\Component\Form\FormEvent; +use Symfony\Component\Form\FormEvents; +use Chill\MainBundle\Templating\TranslatableStringHelper; +use Symfony\Component\Form\FormInterface; +use Symfony\Component\Form\Extension\Core\ChoiceList\SimpleChoiceList; + +/** + * This type create a Choice field with custom fields as choices. + * + * This type can only be associated with a customFieldsGroup type. The field + * is populated when the data (a customFieldsGroup entity) is associated with + * the form + * + * @author Julien Fastré + */ +class LinkedCustomFieldsType extends AbstractType +{ + /** + * + * @var TranslatableStringHelper + */ + private $translatableStringHelper; + + /** + * The name for the choice field + * + * Extracted from builder::getName + * + * @var string + */ + private $choiceName = 'choice'; + + /** + * the option of the form. + * + * @internal options are stored at the class level to be reused by appendChoice, after data are setted + * @var array + */ + private $options = array(); + + public function __construct(TranslatableStringHelper $helper) + { + $this->translatableStringHelper = $helper; + } + + + public function buildForm(FormBuilderInterface $builder, array $options) + { + $this->choiceName = $builder->getName(); + $this->options = $options; + + $builder->addEventListener(FormEvents::POST_SET_DATA, + array($this, 'appendChoice')) + ; + } + + public function getParent() + { + return 'choice'; + } + + /** + * append Choice on POST_SET_DATA event + * + * Choices are extracted from custom_field_group (the data associated + * with the root form) + * + * @param FormEvent $event + * @return void + */ + public function appendChoice(FormEvent $event) + { + $rootForm = $this->getRootForm($event->getForm()); + $group = $rootForm->getData(); + + if ($group === NULL) { + return; + } + + $choices = array(); + foreach($group->getCustomFields() as $customFields) { + $choices[$customFields->getSlug()] = + $this->translatableStringHelper + ->localize($customFields->getName()); + } + + $options = array_merge($this->options, array( + 'choice_list' => new SimpleChoiceList($choices), + )); + + $event->getForm()->getParent()->add($this->choiceName, 'choice', + $options); + } + + /** + * Return the root form (i.e. produced from CustomFieldsGroupType::getForm) + * + * @param FormInterface $form + * @return FormInterface + */ + private function getRootForm(FormInterface $form) + { + if ($form->getParent() === NULL) { + return $form; + } else { + return $this->getRootForm($form->getParent()); + } + } + + public function getName() + { + return 'custom_fields_group_linked_custom_fields'; + } + +} diff --git a/Resources/config/services.yml b/Resources/config/services.yml index 2b0dee975..a192b4d6f 100644 --- a/Resources/config/services.yml +++ b/Resources/config/services.yml @@ -48,6 +48,13 @@ services: - "@chill.main.helper.translatable_string" tags: - { name: 'chill.custom_field', type: 'choice' } + + chill.custom_field.custom_fields_group_linked_custom_fields: + class: Chill\CustomFieldsBundle\Form\Type\LinkedCustomFieldsType + arguments: + - "@chill.main.helper.translatable_string" + tags: + - { name: form.type, alias: custom_fields_group_linked_custom_fields } chill.custom_field.custom_fields_title_type: class: Chill\CustomFieldsBundle\Form\Type\CustomFieldsTitleType