From 4324b8dfc5cccd679b1da01097b3cdac5d8a2084 Mon Sep 17 00:00:00 2001 From: Yuri Kuznetsov Date: Thu, 10 Jun 2021 12:23:40 +0300 Subject: [PATCH] pdf apply default order for relations --- application/Espo/Core/Htmlizer/Htmlizer.php | 31 +++++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) diff --git a/application/Espo/Core/Htmlizer/Htmlizer.php b/application/Espo/Core/Htmlizer/Htmlizer.php index 470f0b1efd..120a9ed2a1 100644 --- a/application/Espo/Core/Htmlizer/Htmlizer.php +++ b/application/Espo/Core/Htmlizer/Htmlizer.php @@ -160,10 +160,13 @@ class Htmlizer foreach ($relationList as $relation) { $collection = null; + $orderData = $this->getRelationOrder($entity->getEntityType(), $relation); + if ($entity->hasLinkMultipleField($relation)) { $collection = $this->entityManager - ->getRepository($entity->getEntityType()) + ->getRDBRepository($entity->getEntityType()) ->getRelation($entity, $relation) + ->order($orderData) ->find(); } else if ( @@ -181,9 +184,10 @@ class Htmlizer } $collection = $this->entityManager - ->getRepository($entity->getEntityType()) + ->getRDBRepository($entity->getEntityType()) ->getRelation($entity, $relation) ->limit(0, $limit) + ->order($orderData) ->find(); } @@ -710,4 +714,27 @@ class Htmlizer return $this->metadata->get(['entityDefs', $entityType, 'fields', $field, 'type']); } + + private function getRelationOrder(string $entityType, string $relation): array + { + $foreignEntityType = $this->entityManager + ->getDefs() + ->getEntity($entityType) + ->getRelation($relation) + ->getForeignEntityType(); + + $collectionParams = $this->entityManager + ->getDefs() + ->getEntity($foreignEntityType) + ->getParam('collection') ?? []; + + $order = $collectionParams['order'] ?? null; + $orderBy = $collectionParams['orderBy'] ?? null; + + if ($order === null && $orderBy === null) { + return []; + } + + return [[$orderBy, $order]]; + } }