Implement native groupBy for Twig

This commit is contained in:
David Bomba 2025-11-03 08:51:39 +11:00
parent ee7b2d52c5
commit ff280b4c5b
3 changed files with 30 additions and 10 deletions

View File

@ -47,9 +47,9 @@ class LocationData extends AbstractService
return $this; return $this;
} }
public function run(): array public function run(bool $setCountries = true): array
{ {
return [ $location = [
// Business Address (from business location or client default) // Business Address (from business location or client default)
'location_name' => $this->getLocationName(), 'location_name' => $this->getLocationName(),
'address' => $this->getBusinessAddress(), 'address' => $this->getBusinessAddress(),
@ -75,6 +75,13 @@ class LocationData extends AbstractService
'shipping_country_code' => $this->getShippingCountryCode(), 'shipping_country_code' => $this->getShippingCountryCode(),
'shipping_exists' => strlen($this->getShippingAddress1()) > 0, 'shipping_exists' => strlen($this->getShippingAddress1()) > 0,
]; ];
if(!$setCountries){
unset($location['country']);
unset($location['shipping_country']);
}
return $location;
} }
private function getLocationName(): string private function getLocationName(): string

File diff suppressed because one or more lines are too long

View File

@ -149,8 +149,18 @@ class TemplateService
$this->twig->addFilter($filter); $this->twig->addFilter($filter);
$filter = new \Twig\TwigFilter('groupBy', \Closure::fromCallable(function (?iterable $items, ?string $property) {
if ($items === null || $property === null) {
return [];
}
return collect($items)->groupBy($property)->toArray();
}));
$this->twig->addFilter($filter);
$allowedTags = ['if', 'for', 'set', 'filter']; $allowedTags = ['if', 'for', 'set', 'filter'];
$allowedFilters = ['capitalize', 'abs', 'date_modify', 'keys', 'join', 'reduce', 'format_date','json_decode','date_modify','trim','round','format_spellout_number','split', 'reduce','replace', 'escape', 'e', 'reverse', 'shuffle', 'slice', 'batch', 'title', 'sort', 'split', 'upper', 'lower', 'capitalize', 'filter', 'length', 'merge','format_currency', 'format_number','format_percent_number','map', 'join', 'first', 'date', 'sum', 'number_format','nl2br','striptags','markdown_to_html']; $allowedFilters = ['groupBy','capitalize', 'abs', 'date_modify', 'keys', 'join', 'reduce', 'format_date','json_decode','date_modify','trim','round','format_spellout_number','split', 'reduce','replace', 'escape', 'e', 'reverse', 'shuffle', 'slice', 'batch', 'title', 'sort', 'split', 'upper', 'lower', 'capitalize', 'filter', 'length', 'merge','format_currency', 'format_number','format_percent_number','map', 'join', 'first', 'date', 'sum', 'number_format','nl2br','striptags','markdown_to_html'];
$allowedFunctions = ['range', 'cycle', 'constant', 'date','img','t']; $allowedFunctions = ['range', 'cycle', 'constant', 'date','img','t'];
$allowedProperties = ['type_id']; $allowedProperties = ['type_id'];
// $allowedMethods = ['img','t']; // $allowedMethods = ['img','t'];
@ -1007,6 +1017,7 @@ class TemplateService
'address1' => $entity->client->address1 ?: '', 'address1' => $entity->client->address1 ?: '',
'address2' => $entity->client->address2 ?: '', 'address2' => $entity->client->address2 ?: '',
'phone' => $entity->client->phone ?: '', 'phone' => $entity->client->phone ?: '',
'group' => $entity->client->group_settings ? $entity->client->group_settings->name : '',
'city' => $entity->client->city ?: '', 'city' => $entity->client->city ?: '',
'state' => $entity->client->state ?: '', 'state' => $entity->client->state ?: '',
'postal_code' => $entity->client->postal_code ?: '', 'postal_code' => $entity->client->postal_code ?: '',
@ -1024,6 +1035,7 @@ class TemplateService
'address' => $entity->client->present()->address(), 'address' => $entity->client->present()->address(),
'shipping_address' => $entity->client->present()->shipping_address(), 'shipping_address' => $entity->client->present()->shipping_address(),
'locale' => substr($entity->client->locale(), 0, 2), 'locale' => substr($entity->client->locale(), 0, 2),
'location' => $entity->service()->location(false),
] : []; ] : [];
} }
@ -1224,6 +1236,7 @@ class TemplateService
return collect($purchase_orders)->map(function ($purchase_order) { return collect($purchase_orders)->map(function ($purchase_order) {
/** @var PurchaseOrder $purchase_order */
return [ return [
'vendor' => $purchase_order->vendor ? [ 'vendor' => $purchase_order->vendor ? [
'name' => $purchase_order->vendor->present()->name(), 'name' => $purchase_order->vendor->present()->name(),