chill-bundles/src/Bundle/ChillCustomFieldsBundle/Templating/Twig/CustomFieldsGroupRenderingTwig.php
2021-11-30 11:28:52 +01:00

110 lines
3.2 KiB
PHP

<?php
/**
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace Chill\CustomFieldsBundle\Templating\Twig;
use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Twig\Extension\AbstractExtension;
use Twig\TwigFunction;
/**
* Add the following Twig Extension :
* * chill_custom_fields_group_widget : to render the value of a custom field
* * group.
*/
class CustomFieldsGroupRenderingTwig extends AbstractExtension implements ContainerAwareInterface
{
/**
* @var Container The container
*/
private $container;
/**
* @var array The default parameters
*/
private $defaultParams = [
'layout' => 'ChillCustomFieldsBundle:CustomFieldsGroup:render.html.twig',
'show_empty' => true,
];
/**
* @param bool $showEmptyValues whether the empty values must be rendered
*/
public function __construct($showEmptyValues)
{
$this->defaultParams['show_empty'] = $showEmptyValues;
}
/**
* (non-PHPdoc).
*
* @see Twig_Extension::getFunctions()
*/
public function getFunctions()
{
return [
new TwigFunction('chill_custom_fields_group_widget', [
$this,
'renderWidget',
], [
'is_safe' => [
'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
* - show_empty : force show empty field
*
* @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 = [])
{
$resolvedParams = array_merge($this->defaultParams, $params);
return $this->container->get('templating')
->render($resolvedParams['layout'], [
'cFGroup' => $customFielsGroup,
'cFData' => $fields,
'show_empty' => $resolvedParams['show_empty'], ]);
}
/**
* (non-PHPdoc).
*
* @see \Symfony\Component\DependencyInjection\ContainerAwareInterface::setContainer()
*/
public function setContainer(?ContainerInterface $container = null)
{
$this->container = $container;
}
}