diff --git a/Controller/CustomFieldsGroupController.php b/Controller/CustomFieldsGroupController.php
index b4bb9dabb..03e813e32 100644
--- a/Controller/CustomFieldsGroupController.php
+++ b/Controller/CustomFieldsGroupController.php
@@ -242,17 +242,27 @@ class CustomFieldsGroupController extends Controller
}
$form = $this->createForm('custom_field', null, array('group' => $entity));
- $form->add('submit', 'submit', array('label' => 'POST'));
+ $form->add('submit_dump', 'submit', array('label' => 'POST AND DUMP'));
+ $form->add('submit_render','submit', array('label' => 'POST AND RENDER'));
$form->handleRequest($request);
+ $this->get('twig.loader')
+ ->addPath(__DIR__.'/../Tests/Fixtures/App/app/Resources/views/',
+ $namespace = 'test');
+
if ($form->isSubmitted()) {
+ if ($form->get('submit_render')->isClicked()) {
+ return $this->render('ChillCustomFieldsBundle:CustomFieldsGroup:render.html.twig', array(
+ 'fields' => $form->getData(),
+ 'customFieldsGroup' => $entity
+ ));
+ }
+
var_dump($form->getData());
var_dump(json_encode($form->getData()));
}
- $this->get('twig.loader')
- ->addPath(__DIR__.'/../Tests/Fixtures/App/app/Resources/views/',
- $namespace = 'test');
+
return $this
->render('@test/CustomField/simple_form_render.html.twig', array(
diff --git a/CustomFields/CustomFieldText.php b/CustomFields/CustomFieldText.php
index 3b150536e..11c029ccb 100644
--- a/CustomFields/CustomFieldText.php
+++ b/CustomFields/CustomFieldText.php
@@ -8,6 +8,7 @@ use Chill\CustomFieldsBundle\CustomFields\CustomFieldInterface;
use Chill\CustomFieldsBundle\Entity\CustomField;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\HttpFoundation\RequestStack;
+use Symfony\Bundle\TwigBundle\TwigEngine;
/**
*
@@ -19,9 +20,17 @@ class CustomFieldText implements CustomFieldInterface
private $requestStack;
- public function __construct(RequestStack $requestStack)
+ /**
+ *
+ * @var TwigEngine
+ */
+ private $templating;
+
+
+ public function __construct(RequestStack $requestStack, TwigEngine $templating)
{
$this->requestStack = $requestStack;
+ $this->templating = $templating;
}
const MAX_LENGTH = 'maxLength';
@@ -47,7 +56,8 @@ class CustomFieldText implements CustomFieldInterface
public function render($value, CustomField $customField)
{
-
+ return $this->templating
+ ->render('ChillCustomFieldsBundle:CustomFieldsRendering:text.html.twig', array('text' => $value));
}
public function serialize($value, CustomField $customField)
diff --git a/Resources/config/services.yml b/Resources/config/services.yml
index 295667a09..a330b4819 100644
--- a/Resources/config/services.yml
+++ b/Resources/config/services.yml
@@ -39,6 +39,7 @@ services:
class: Chill\CustomFieldsBundle\CustomFields\CustomFieldText
arguments:
- "@request_stack"
+ - "@templating"
tags:
- { name: 'chill.custom_field', type: 'text' }
@@ -68,3 +69,16 @@ services:
- "@request_stack"
tags:
- { name: 'chill.custom_field', type: 'title' }
+
+ chill.custom_field.helper:
+ class: Chill\CustomFieldsBundle\Service\CustomFieldsHelper
+ arguments:
+ - "@doctrine.orm.entity_manager"
+ - "@chill.custom_field.provider"
+
+ chill.custom_field.twig.custom_fields_rendering:
+ class: Chill\CustomFieldsBundle\Templating\Twig\CustomFieldRenderingTwig
+ calls:
+ - [setContainer, ["@service_container"]]
+ tags:
+ - { name: twig.extension }
diff --git a/Resources/views/CustomField/render_label.html.twig b/Resources/views/CustomField/render_label.html.twig
new file mode 100644
index 000000000..bf4b186fc
--- /dev/null
+++ b/Resources/views/CustomField/render_label.html.twig
@@ -0,0 +1 @@
+{{ customField.name(app.request.locale) }}
\ 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..a3552f79b
--- /dev/null
+++ b/Resources/views/CustomFieldsGroup/render.html.twig
@@ -0,0 +1,7 @@
+
+ {{ customFieldsGroup.name(app.request.locale) }}
+ {% for customField in customFieldsGroup.customFields %}
+ - {{ chill_custom_field_label(customField) }}
+ - {{ chill_custom_field_widget(fields, customField) }}
+ {% endfor %}
+
\ No newline at end of file
diff --git a/Resources/views/CustomFieldsRendering/text.html.twig b/Resources/views/CustomFieldsRendering/text.html.twig
new file mode 100644
index 000000000..9b2b76cb3
--- /dev/null
+++ b/Resources/views/CustomFieldsRendering/text.html.twig
@@ -0,0 +1 @@
+{{ text }}
\ No newline at end of file
diff --git a/Service/CustomFieldsHelper.php b/Service/CustomFieldsHelper.php
new file mode 100644
index 000000000..ae879e10b
--- /dev/null
+++ b/Service/CustomFieldsHelper.php
@@ -0,0 +1,126 @@
+
+ *
+ */
+class CustomFieldsHelper
+{
+ /**
+ *
+ * @var EntityManagerInterface
+ */
+ private $em;
+
+ /**
+ *
+ * @var CustomFieldProvider
+ */
+ private $provider;
+
+
+ /**
+ *
+ * @var array
+ */
+ private $cache = array();
+
+
+ public function __construct(EntityManagerInterface $em, CustomFieldProvider $provider)
+ {
+ $this->em = $em;
+ $this->provider = $provider;
+ }
+
+ /**
+ *
+ * @param object|string $class
+ */
+ private function _cacheCustomFields($class)
+ {
+ $customFieldsGroups = $this->em->getRepository('ChillCustomFieldsBundle:CustomFieldsGroup')
+ ->findBy(array('entity' => (is_string($class)) ? $class : get_class($class)));
+
+ if (!$customFieldsGroups) {
+ throw CustomFieldsHelperException::customFieldsGroupNotFound((is_string($class)) ? $class : get_class($class));
+ }
+
+ foreach ($customFieldsGroup as $cfGroup) {
+ $this->_cacheCustomFields($cfGroup);
+ }
+ }
+ /**
+ *
+ * @param CustomFieldsGroup $group
+ */
+ private function _cacheCustomFieldsGroup(CustomFieldsGroup $group)
+ {
+ foreach ($group->getCustomFields() as $field) {
+ $this->cache[$group->getEntity()][$field->getSlug()] = $field;
+ }
+ }
+
+ /**
+ *
+ * @param object|string $class
+ * @param string $slug If the slug is null, throw a proper CustomFieldsHelperException
+ * @return CustomField
+ * @throws CustomFieldsHelperException if $slug is null
+ */
+ public function getCustomField($class, $slug = null)
+ {
+ if (!$slug) {
+ throw CustomFieldsHelperException::slugIsMissing();
+ }
+
+ $resolveClass = (is_string($class)) ? $class : get_class($class);
+ if (!$this->cache[$resolveClass][$slug]) {
+ $this->_cacheCustomFields($resolveClass);
+ }
+
+ return $this->cache[$resolveClass][$slug];
+ }
+
+ /**
+ *
+ * @param array $fields
+ * @param object|string $class
+ * @param string $slug
+ * @return mixed|null null if the slug is not recorded
+ */
+ private function getCustomFieldValue(array $fields, $class, $slug)
+ {
+ return (isset($fields[$slug])) ? $this->provider
+ ->getCustomFieldByType($this->getCustomField($class, $slug)->getType())
+ ->deserialize($fields[$slug])
+ : null;
+ }
+
+ /**
+ *
+ * @param array $fields the **raw** array, as stored in the db
+ * @param CustomField|object|string $classOrCustomField the object OR the get_class($object) string OR The CustomField
+ * @param string slug The Slug to render, if CustomField is not Given
+ * @param string $slug the slug you want to render. The html is be safe.
+ * @throws CustomFieldsHelperException if slug is missing
+ */
+ public function renderCustomField(array $fields, $classOrCustomField, $slug = null)
+ {
+ $customField = ($classOrCustomField instanceof CustomField) ? $classOrCustomField : $this->getCustomField($classOrCustomField, $slug);
+ $slug = $customField->getSlug();
+ $rawValue = (isset($fields[$slug])) ? $fields[$slug] : null;
+
+ return $this->provider->getCustomFieldByType($customField->getType())
+ ->render($rawValue, $customField);
+ }
+
+}
\ No newline at end of file
diff --git a/Service/CustomFieldsHelperException.php b/Service/CustomFieldsHelperException.php
new file mode 100644
index 000000000..d1e0c41d2
--- /dev/null
+++ b/Service/CustomFieldsHelperException.php
@@ -0,0 +1,16 @@
+
+ *
+ */
+class CustomFieldRenderingTwig extends \Twig_Extension implements ContainerAwareInterface
+{
+
+ /**
+ *
+ * @var Container
+ */
+ private $container;
+
+ private $defaultParams = array(
+ 'label_layout' => 'ChillCustomFieldsBundle:CustomField:render_label.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_field_widget', array(
+ $this,
+ 'renderWidget'
+ ), array(
+ 'is_safe' => array(
+ 'html'
+ )
+ )),
+ new \Twig_SimpleFunction('chill_custom_field_label', array(
+ $this,
+ 'renderLabel'
+ ), array(
+ 'is_safe' => array(
+ 'html'
+ )
+ ))
+ ];
+ }
+
+
+ /* (non-PHPdoc)
+ * @see Twig_ExtensionInterface::getName()
+ */
+ public function getName()
+ {
+ return 'chill_custom_fields_rendering';
+ }
+
+
+ /**
+ *
+ * @param CustomField|object|string $customFieldOrClass
+ * @param string $slug
+ * @param array $params the parameters for rendering
+ */
+ public function renderLabel($customFieldOrClass, $slug = null, array $params = array())
+ {
+ $resolvedParams = array_merge($this->defaultParams, $params);
+
+ $customField = ($customFieldOrClass instanceof CustomField)
+ ? $customFieldOrClass : $this->container->get('chill.custom_field.provider')
+ ->getCustomField($customFieldOrClass, $slug);
+
+ return $this->container->get('templating')
+ ->render($resolvedParams['label_layout'], array('customField' => $customField));
+ }
+
+ public function renderWidget(array $fields, $customFieldOrClass, $slug = null)
+ {
+ return $this->container->get('chill.custom_field.helper')
+ ->renderCustomField($fields, $customFieldOrClass, $slug);
+ }
+
+
+}
\ No newline at end of file