From 101783b9f1848e25ec517513215243a77332ffee Mon Sep 17 00:00:00 2001 From: Marc Ducobu Date: Wed, 30 Sep 2015 13:32:24 +0200 Subject: [PATCH] Only display active custom fields in form & view --- Form/Type/CustomFieldType.php | 6 +- Resources/config/services.yml | 9 +- .../views/CustomFieldsGroup/render.html.twig | 8 ++ .../Twig/CustomFieldsGroupRenderingTwig.php | 107 ++++++++++++++++++ 4 files changed, 126 insertions(+), 4 deletions(-) create mode 100644 Resources/views/CustomFieldsGroup/render.html.twig create mode 100644 Templating/Twig/CustomFieldsGroupRenderingTwig.php diff --git a/Form/Type/CustomFieldType.php b/Form/Type/CustomFieldType.php index ed710ec59..8a202e369 100644 --- a/Form/Type/CustomFieldType.php +++ b/Form/Type/CustomFieldType.php @@ -46,10 +46,10 @@ class CustomFieldType extends AbstractType public function buildForm(FormBuilderInterface $builder, array $options) { - foreach ($options['group']->getCustomFields() as $cf) { + foreach ($options['group']->getActiveCustomFields() as $cf) { $this->customFieldCompiler - ->getCustomFieldByType($cf->getType()) - ->buildForm($builder, $cf); + ->getCustomFieldByType($cf->getType()) + ->buildForm($builder, $cf); } } diff --git a/Resources/config/services.yml b/Resources/config/services.yml index a192b4d6f..af7b05b91 100644 --- a/Resources/config/services.yml +++ b/Resources/config/services.yml @@ -48,7 +48,7 @@ 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: @@ -82,3 +82,10 @@ services: - [setContainer, ["@service_container"]] tags: - { name: twig.extension } + + chill.custom_field.twig.custom_fields_group_rendering: + class: Chill\CustomFieldsBundle\Templating\Twig\CustomFieldsGroupRenderingTwig + calls: + - [setContainer, ["@service_container"]] + tags: + - { name: twig.extension } \ No newline at end of file diff --git a/Resources/views/CustomFieldsGroup/render.html.twig b/Resources/views/CustomFieldsGroup/render.html.twig new file mode 100644 index 000000000..a63183233 --- /dev/null +++ b/Resources/views/CustomFieldsGroup/render.html.twig @@ -0,0 +1,8 @@ +{% for customField in cFGroup.activeCustomFields %} + {% if customField.type == 'title' %} + {{ chill_custom_field_widget(cFData , customField) }} + {% else %} +
{{ chill_custom_field_label(customField) }}
+
{{ chill_custom_field_widget(cFData , customField) }}
+ {% endif %} +{% endfor %} \ No newline at end of file diff --git a/Templating/Twig/CustomFieldsGroupRenderingTwig.php b/Templating/Twig/CustomFieldsGroupRenderingTwig.php new file mode 100644 index 000000000..af6dbb585 --- /dev/null +++ b/Templating/Twig/CustomFieldsGroupRenderingTwig.php @@ -0,0 +1,107 @@ +, + * + * 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\Templating\Twig; + +use Symfony\Component\DependencyInjection\ContainerAwareInterface; +use Symfony\Component\DependencyInjection\ContainerInterface; +use Symfony\Component\DependencyInjection\Container; +use Chill\CustomFieldsBundle\Entity\CustomField; + +/** + * Add the following Twig Extension : + * * chill_custom_fields_group_widget : to render the value of a custom field + * * group + * + * @author Julien Fastré + * @author Marc Ducobu + */ +class CustomFieldsGroupRenderingTwig extends \Twig_Extension implements ContainerAwareInterface +{ + + /** @var Container $container The container */ + private $container; + + /** @var array $defaultParams The default parameters */ + private $defaultParams = array( + 'layout' => 'ChillCustomFieldsBundle:CustomFieldsGroup:render.html.twig' + ); + + /* + * (non-PHPdoc) + * @see \Symfony\Component\DependencyInjection\ContainerAwareInterface::setContainer() + */ + public function setContainer(ContainerInterface $container = null) + { + $this->container = $container; + } + + /* + * (non-PHPdoc) + * @see Twig_Extension::getFunctions() + */ + public function getFunctions() + { + return [ + new \Twig_SimpleFunction('chill_custom_fields_group_widget', array( + $this, + 'renderWidget' + ), array( + 'is_safe' => array( + 'html' + ) + )), + ]; + } + + /* (non-PHPdoc) + * @see Twig_ExtensionInterface::getName() + */ + public function getName() + { + return 'chill_custom_fields_group_rendering'; + } + + /** + * Twig extension that is used to render the value of a custom field group. + * + * The presentation of the value is influenced by the document type. + * + * @param array $fields The array raw, as stored in the db + * @param CustomFieldsGroud $customFielsGroup The custom field group + * @param string $documentType The type of the document (csv, html) + * @param array $params The parameters for rendering : + * - layout : allow to choose a different layout by default : + * ChillCustomFieldsBundle:CustomFieldsGroup:render.html.twig + * @return string HTML representation of the custom field group value, as described in + * the CustomFieldInterface. Is HTML safe + */ + public function renderWidget(array $fields, $customFielsGroup, $documentType='html', array $params = array()) + { + $resolvedParams = array_merge($this->defaultParams, $params); + + return $this->container->get('templating') + ->render($resolvedParams['layout'], array( + 'cFGroup' => $customFielsGroup, + 'cFData' => $fields)); + } +} \ No newline at end of file