add option to remove itetables values and/or specifics keys in the twig toArray function

This commit is contained in:
Simon Vieille 2023-07-19 19:36:32 +02:00
parent f9a20716a0
commit e2c9ecb941
Signed by: deblan
GPG key ID: 579388D585F70417
2 changed files with 10 additions and 2 deletions

View file

@ -1,6 +1,6 @@
<div class="row">
<div class="col-md-12 p-3">
{% for item in entity_to_array(entity) %}
{% for item in entity_to_array(entity, false, ['id']) %}
<div class="font-weight-bold">{{ item.name|trans }}</div>
<div class="mb-3">{{ item.value }}</div>
{% endfor %}

View file

@ -33,7 +33,7 @@ class EntityExtension extends AbstractExtension
];
}
public function toArray(EntityInterface $entity): array
public function toArray(EntityInterface $entity, bool $keepIterable = true, array $except = []): array
{
$metaData = $this->entityManager->getClassMetadata(get_class($entity));
$array = [];
@ -42,6 +42,14 @@ class EntityExtension extends AbstractExtension
try {
$value = $this->propertyAccessor->getValue($entity, $field);
if (in_array($field, $except)) {
continue;
}
if (is_iterable($value) && !$keepIterable) {
continue;
}
if (is_object($value)) {
$value = (string) $value;
}