Skip to content
This repository has been archived by the owner on Feb 4, 2023. It is now read-only.

[Extensions Buttons] Allow using a template for body format when exporting #749

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Resources/views/datatable/button.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
{% endif %}
{% if button.buttonOptions is defined and button.buttonOptions is not same as(null) %}
{% for key, option in button.buttonOptions %}
{{ key }}: {{ option|json_encode|raw }},
{{ key }}: {{ option|sg_datatables_option_encode|raw }},
{% endfor %}
{% endif %}
},
23 changes: 23 additions & 0 deletions Twig/DatatableTwigExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ public function getFilters()
{
return array(
new Twig_SimpleFilter('sg_datatables_bool_var', array($this, 'boolVar')),
new Twig_SimpleFilter('sg_datatables_option_encode', array($this, 'optionEncode'), ['needs_environment' => true]),
);
}

Expand Down Expand Up @@ -278,4 +279,26 @@ public function boolVar($value)
return 'false';
}
}


/**
* Renders: A json encode, except a template for the body. Useful for export pdf
*
* @param mixed $value
*
* @return string
*/
public function optionEncode(Twig_Environment $twig, $value)
{
$value = json_encode($value);

preg_match('@\{\"template\"\:\".*?\"\}@si', $value, $matches);

foreach ($matches as $match){
$template = json_decode($match, true)["template"];
$value = str_replace($match, $twig->render($template), $value);
}

return $value;
}
}