Skip to content

Commit

Permalink
entry-show: better display for checkbox tree
Browse files Browse the repository at this point in the history
  • Loading branch information
seballot committed Jul 2, 2024
1 parent 996d3f4 commit 058830e
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 2 deletions.
40 changes: 38 additions & 2 deletions tools/bazar/fields/CheckboxListField.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,50 @@ protected function renderStatic($entry)
{
$keys = $this->getValues($entry);
$values = [];

if (count($values) > 0) {
return '';
}

// List with multi levels
if ($this->optionsTree) {
return $this->render('@bazar/fields/checkbox-tree.twig', [
'treeValues' => $this->filterTree($this->optionsTree, $keys),
]);
}

// List with one level
foreach ($this->getOptions() as $key => $label) {
if (in_array($key, $keys)) {
$values[$key] = $label;
}
}

return (count($values) > 0) ? $this->render('@bazar/fields/checkbox.twig', [
return $this->render('@bazar/fields/checkbox.twig', [
'values' => $values,
]) : '';
]);
}

// Filter the tree to keep only branches where a nodeID is checked
private function filterTree($tree, $checkedValues)
{
$filteredTree = [];

foreach ($tree as $node) {
if (in_array($node['id'], $checkedValues)) {
$filteredNode = $node;
$filteredNode['children'] = $this->filterTree($node['children'], $checkedValues);
$filteredTree[] = $filteredNode;
} else {
$filteredChildren = $this->filterTree($node['children'], $checkedValues);
if (!empty($filteredChildren)) {
$filteredNode = $node;
$filteredNode['children'] = $filteredChildren;
$filteredTree[] = $filteredNode;
}
}
}

return $filteredTree;
}
}
32 changes: 32 additions & 0 deletions tools/bazar/templates/fields/checkbox-tree.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{% extends "@bazar/layouts/field.twig" %}

{% block value_container %}
<span class="BAZ_texte field-checkbox-tree">
{{ _self.renderNodes(treeValues) }}
</span>
{% endblock %}

{% macro renderNodes(nodes) %}

{# Add some classes "leaf", "only-leafs", "one-child" to make customization with CSS easy #}
{% set allChildrenEmpty = true %}
{% for node in nodes %}
{% if node.children is not empty %}
{% set allChildrenEmpty = false %}
{% endif %}
{% endfor %}
<ul class="{{ allChildrenEmpty ? 'only-leafs' : ''}}">
{% for node in nodes %}
<li class="{{ node.children|length == 0 ? 'leaf' : ''}} {{ node.children|length == 1 ? 'one-child' : ''}}">
{{ node.label|raw }}
{% if node.children|length > 0 %}
{{ _self.renderNodes(node.children) }}
{% endif %}
</li>
{% endfor %}
</ul>
{% endmacro %}




0 comments on commit 058830e

Please sign in to comment.